作者:路鎖
本文正在參加星光計劃3.0–夏日挑戰賽
前言
作為多年Android習慣使用ADB指令,對于HarmonyOS HDC指令如何使用?兩者之間差別及原子化服務開發過程中HDC指令與ADB指令使用技巧,請移步下文講解。
一、HDC與ADB介紹
HDC介紹
HDC,即HarmonyOS Device Connector,是HarmonyOS為開發人員提供的用于調試的指令行工具,通過該工具可以在windows/linux/mac系統上與真實裝置或者模拟器進行互動。
HDC通過HarmonyOS SDK擷取,存放于sdk的toolchains目錄下,使用時将sdk的toolchains目錄添加到環境變量。
- 通過hdc -help檢視hdc指令總表
C:\Users\lsyt>hdc -help
Usage: hdc [options] <command> [arguments]
Options:
-t UTID Specify the target device with UTID(unique target id)
-s SOCKSPEC Specify the socket to bind for server
-h Display this usage info
-v Display the version info
Commands:
list targets [-v] List the connected targets, use -v option to get
more details
help Display this usage info
version Display the version info
tmode <usb|port PORT> Set the daemon working mode to usb or tcp port
tconn HOST[:PORT] [-remove] Connect to the target which is working under tcp port mode.
Remove the connection if the -remove option is specified.
fport list [-b] List the relayed connections, use -b option for backward mode.
fport LOCAL REMOTE [-b] Relay socket LOCAL to REMOTE
socket can be specified as the list below:
tcp:PORT use tcp port
jdwp:<process pid> (for REMOTE only)
fport rm [LOCAL] [-b] Remove the given LOCAL socket connection,
remove all if no LOCAL argument
file send LOCAL... REMOTE
Copy LOCAL files to target under REMOTE directory
file recv REMOTE... LOCAL
Copy REMOTE files to LOCAL directory
file sync [all|data|...|system|vendor]
Sync files from local location to the target.
shell [-nTtx] [COMMAND...]
Start a shell and run COMMAND under the target
-n: do not read from stdin
-T: disable pseudo-terminal utiity(PTY) allocation
-t: force PTY allocation
-x: disable remote exit codes and stdout/stderr separation
app install [-rdg] PACKAGE
Install the given application PACKAGE to the target
-r: Replace existing application
-d: Allow version downgrade (debug compatible only)
-g: Grant all the permissions
app install-multiple [-rdg] PACKAGE1 [...PACKAGEn]
app install-multiple --hap HAP-PATH (for HAPs only)
Install multiple HAPs(/APKs) of one PACKAGE to the target.
the following options are for APKs only:
-r: Replace existing application
-d: Allow version downgrade (debug compatible only)
-g: Grant all the permissions
the option --hap is for HAPs only:
--hap: Indicate the .hap files to be installed are located under HAP-PATH dir.
app uninstall [-k] PACKAGE
Uninstall the PACKAGE app from the target
-k: Do not remove user data
listpid Display debuggable application processes list
hilog Tail the logs from the target
target mount
Mount the partitions in read-write mode
target boot
Boot the target; to see the targets list, try list targets command
smode [off] Grant the daemon root permissions, to revoke with the off argument
kill Kill the buddy process
reset Reset the connections to target
- 詳細指令介紹請參考:hdc使用指導
ADB介紹
ADB,即 Android Debug Bridge,是一種功能多樣的指令行工具,可讓您與裝置進行通信。adb 指令可用于執行各種裝置操作(例如安裝和調試應用),并提供對 Unix shell(可用來在裝置上運作各種指令)的通路權限。它是一種用戶端-伺服器程式,包括以下三個元件:
- 用戶端:用于發送指令。用戶端在開發機器上運作。您可以通過發出 adb 指令從指令行終端調用用戶端。
- 守護程式 (adbd):用于在裝置上運作指令。守護程式在每個裝置上作為背景程序運作。
- 伺服器:用于管理用戶端與守護程式之間的通信。伺服器在開發機器上作為背景程序運作。
adb
包含在 Android SDK 平台工具軟體包中。您可以使用 SDK 管理器下載下傳此軟體包,該管理器會将其安裝在
android_sdk/platform-tools/
下。或者,如果您需要獨立的 Android SDK 平台工具軟體包,也可以點選此處進行下載下傳。
- 通過adb -help檢視hdc指令總表
C:\Users\lsyt>adb --help
Android Debug Bridge version 1.0.41
Version 31.0.3-7562133
Installed as C:\DATA\AndroidSdk\platform-tools_r31.0.3-windows\platform-tools\adb.exe
global options:
-a listen on all network interfaces, not just localhost
-d use USB device (error if multiple devices connected)
-e use TCP/IP device (error if multiple TCP/IP devices available)
-s SERIAL use device with given serial (overrides $ANDROID_SERIAL)
-t ID use device with given transport id
-H name of adb server host [default=localhost]
-P port of adb server [default=5037]
-L SOCKET listen on given socket for adb server [default=tcp:localhost:5037]
general commands:
devices [-l] list connected devices (-l for long output)
help show this help message
version show version num
networking:
connect HOST[:PORT] connect to a device via TCP/IP [default port=5555]
disconnect [HOST[:PORT]]
disconnect from given TCP/IP device [default port=5555], or all
pair HOST[:PORT] [PAIRING CODE]
pair with a device for secure TCP/IP communication
forward --list list all forward socket connections
forward [--no-rebind] LOCAL REMOTE
forward socket connection using:
tcp:<port> (<local> may be "tcp:0" to pick any open port)
localabstract:<unix domain socket name>
localreserved:<unix domain socket name>
localfilesystem:<unix domain socket name>
dev:<character device name>
jdwp:<process pid> (remote only)
vsock:<CID>:<port> (remote only)
acceptfd:<fd> (listen only)
forward --remove LOCAL remove specific forward socket connection
forward --remove-all remove all forward socket connections
ppp TTY [PARAMETER...] run PPP over USB
reverse --list list all reverse socket connections from device
reverse [--no-rebind] REMOTE LOCAL
reverse socket connection using:
tcp:<port> (<remote> may be "tcp:0" to pick any open port)
localabstract:<unix domain socket name>
localreserved:<unix domain socket name>
localfilesystem:<unix domain socket name>
reverse --remove REMOTE remove specific reverse socket connection
reverse --remove-all remove all reverse socket connections from device
mdns check check if mdns discovery is available
mdns services list all discovered services
file transfer:
push [--sync] [-z ALGORITHM] [-Z] LOCAL... REMOTE
copy local files/directories to device
--sync: only push files that are newer on the host than the device
-n: dry run: push files to device without storing to the filesystem
-z: enable compression with a specified algorithm (any, none, brotli)
-Z: disable compression
pull [-a] [-z ALGORITHM] [-Z] REMOTE... LOCAL
copy files/dirs from device
-a: preserve file timestamp and mode
-z: enable compression with a specified algorithm (any, none, brotli)
-Z: disable compression
sync [-l] [-z ALGORITHM] [-Z] [all|data|odm|oem|product|system|system_ext|vendor]
sync a local build from $ANDROID_PRODUCT_OUT to the device (default all)
-n: dry run: push files to device without storing to the filesystem
-l: list files that would be copied, but don't copy them
-z: enable compression with a specified algorithm (any, none, brotli)
-Z: disable compression
shell:
shell [-e ESCAPE] [-n] [-Tt] [-x] [COMMAND...]
run remote shell command (interactive shell if no command given)
-e: choose escape character, or "none"; default '~'
-n: don't read from stdin
-T: disable pty allocation
-t: allocate a pty if on a tty (-tt: force pty allocation)
-x: disable remote exit codes and stdout/stderr separation
emu COMMAND run emulator console command
app installation (see also `adb shell cmd package help`):
install [-lrtsdg] [--instant] PACKAGE
push a single package to the device and install it
install-multiple [-lrtsdpg] [--instant] PACKAGE...
push multiple APKs to the device for a single package and install them
install-multi-package [-lrtsdpg] [--instant] PACKAGE...
push one or more packages to the device and install them atomically
-r: replace existing application
-t: allow test packages
-d: allow version code downgrade (debuggable packages only)
-p: partial application install (install-multiple only)
-g: grant all runtime permissions
--abi ABI: override platform's default ABI
--instant: cause the app to be installed as an ephemeral install app
--no-streaming: always push APK to device and invoke Package Manager as separate steps
--streaming: force streaming APK directly into Package Manager
--fastdeploy: use fast deploy
--no-fastdeploy: prevent use of fast deploy
--force-agent: force update of deployment agent when using fast deploy
--date-check-agent: update deployment agent when local version is newer and using fast deploy
--version-check-agent: update deployment agent when local version has different version code and using fast deploy
(See also `adb shell pm help` for more options.)
uninstall [-k] PACKAGE
remove this app package from the device
'-k': keep the data and cache directories
debugging:
bugreport [PATH]
write bugreport to given PATH [default=bugreport.zip];
if PATH is a directory, the bug report is saved in that directory.
devices that don't support zipped bug reports output to stdout.
jdwp list pids of processes hosting a JDWP transport
logcat show device log (logcat --help for more)
security:
disable-verity disable dm-verity checking on userdebug builds
enable-verity re-enable dm-verity checking on userdebug builds
keygen FILE
generate adb public/private key; private key stored in FILE,
scripting:
wait-for[-TRANSPORT]-STATE...
wait for device to be in a given state
STATE: device, recovery, rescue, sideload, bootloader, or disconnect
TRANSPORT: usb, local, or any [default=any]
get-state print offline | bootloader | device
get-serialno print <serial-number>
get-devpath print <device-path>
remount [-R]
remount partitions read-write. if a reboot is required, -R will
will automatically reboot the device.
reboot [bootloader|recovery|sideload|sideload-auto-reboot]
reboot the device; defaults to booting system image but
supports bootloader and recovery too. sideload reboots
into recovery and automatically starts sideload mode,
sideload-auto-reboot is the same but reboots after sideloading.
sideload OTAPACKAGE sideload the given full OTA package
root restart adbd with root permissions
unroot restart adbd without root permissions
usb restart adbd listening on USB
tcpip PORT restart adbd listening on TCP on PORT
internal debugging:
start-server ensure that there is a server running
kill-server kill the server if it is running
reconnect kick connection from host side to force reconnect
reconnect device kick connection from device side to force reconnect
reconnect offline reset offline/unauthorized devices to force reconnect
usb:
attach attach a detached USB device
detach detach from a USB device to allow use by other processes
environment variables:
$ADB_TRACE
comma-separated list of debug info to log:
all,adb,sockets,packets,rwx,usb,sync,sysdeps,transport,jdwp
$ADB_VENDOR_KEYS colon-separated list of keys (files or directories)
$ANDROID_SERIAL serial number to connect to (see -s)
$ANDROID_LOG_TAGS tags to be used by logcat (see logcat --help)
$ADB_LOCAL_TRANSPORT_MAX_PORT max emulator scan port (default 5585, 16 emus)
$ADB_MDNS_AUTO_CONNECT comma-separated list of mdns services to allow auto-connect (default adb-tls-connect)
- 詳細指令介紹請參考:官方文檔
二、HDC與ADB常用指令對比
HDC | ADB | 說明 |
---|---|---|
hdc -h | adb --help | 檢視幫助 |
hdc -v | adb --version | 檢視版本 |
hdc list targets | adb devices | 檢視連接配接裝置 |
hdc kill | adb kill-server | 結束服務 |
hdc kill -r | adb start-server | 啟動服務 |
hdc app install [安裝包路徑] | adb install [安裝包路徑] | 安裝應用 |
hdc app uninstall package | adb uninstall package | 解除安裝應用 |
hdc hilog | adb logcat | 抓取log |
hdc shell hilogcat >log.log | adb shell logcat >log.log | 抓取log并儲存 |
hdc shell reboot | adb reboot | 重新開機裝置 |
hdc shell bm get -u | adb shell bm get -u | 擷取UUID |
hdc file recv REMOTE... LOCAL | adb pull REMOTE... LOCAL | 接收檔案<br />REMOTE:手機<br />LOCAL:PC |
hdc file send LOCAL... REMOTE | adb push LOCAL... REMOTE | 發送檔案 |
hdc shell screencap filename | adb shell screencap filename | 截屏 |
hdc shell screenrecord filename | adb shell screenrecord filename | 錄屏 |
三、日常開發常用指令
日常FA開發中以上指令的使用對于前端同學來說比較陌生,指令不容易記住。如:擷取UUID,指令記不住需要往期的賦能資料,最終才能擷取到UUID。針對指令記不住的問題可通過提供簡單腳本的方式,提供給前端使用。
注意:HDC服務和ADB服務不能共存。在執行腳本時,如果擷取的資料為空,則需要确認手機裝置使用的是hdc指令還是ADB服務。
确認方式:通過hdc list targets指令擷取裝置清單,如果擷取到裝置清單則目前使用的為hdc服務,否則為adb服務。可通過adb kill-server結束adb服務,再通過usb連接配接裝置,使用hdc list targets指令擷取裝置清單,如果擷取到清單則表示目前服務使用的是hdc服務。如下圖操作:
3.1 擷取UUID
腳本:uid.bat 已放置于下方資源處,如有需要自行下載下傳
hdc shell bm get -u > uuid.log
将該指令生成uuid.bat腳本,通過腳本擷取裝置UUID。
3.2 擷取hilog日志
腳本:hilog.bat 已放置于下方資源處,如有需要自行下載下傳
hdc hilog
3.3 抓取log并儲存
腳本:hilogs.bat 已放置于下方資源處,如有需要自行下載下傳
hdc shell hilogcat >log.log
總結
- HDC指令及ADB指令都可以通過腳本方式擷取或檢視對應資料;
- 前端同學可按需自己編寫腳本擷取自己所需資料。
更多原創内容請關注:中軟國際 HarmonyOS 技術團隊
入門到精通、技巧到案例,系統化分享HarmonyOS開發技術,歡迎投稿和訂閱,讓我們一起攜手前行共建鴻蒙生态。
附件連結:
hilogs.zip(https://ost.51cto.com/resource/2096 )
uid.zip(https://ost.51cto.com/resource/2095 )
hilog.zip(https://ost.51cto.com/resource/2094 )