HarmonyOS - HDC命令與ADB命令使用對比
??想了解更多關(guān)于開源的內(nèi)容,請訪問:??
前言
作為多年Android習(xí)慣使用ADB命令,對于HarmonyOS HDC命令如何使用?兩者之間區(qū)別及原子化服務(wù)開發(fā)過程中HDC命令與ADB命令使用技巧,請移步下文講解。
一、HDC與ADB介紹
HDC介紹
HDC,即HarmonyOS Device Connector,是HarmonyOS為開發(fā)人員提供的用于調(diào)試的命令行工具,通過該工具可以在windows/linux/mac系統(tǒng)上與真實設(shè)備或者模擬器進行交互。
HDC通過HarmonyOS SDK獲取,存放于sdk的toolchains目錄下,使用時將sdk的toolchains目錄添加到環(huán)境變量。
- 通過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
- 詳細(xì)命令介紹請參考:??hdc使用指導(dǎo)??。
ADB介紹
ADB,即 Android Debug Bridge,是一種功能多樣的命令行工具,可讓您與設(shè)備進行通信。adb 命令可用于執(zhí)行各種設(shè)備操作(例如安裝和調(diào)試應(yīng)用),并提供對 Unix shell(可用來在設(shè)備上運行各種命令)的訪問權(quán)限。它是一種客戶端-服務(wù)器程序,包括以下三個組件:
- 客戶端:用于發(fā)送命令??蛻舳嗽陂_發(fā)機器上運行。您可以通過發(fā)出 adb 命令從命令行終端調(diào)用客戶端。
- 守護程序 (adbd):用于在設(shè)備上運行命令。守護程序在每個設(shè)備上作為后臺進程運行。
- 服務(wù)器:用于管理客戶端與守護程序之間的通信。服務(wù)器在開發(fā)機器上作為后臺進程運行。
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)
- 詳細(xì)命令介紹請參考:??官方文檔??。
二、HDC與ADB常用命令對比
HDC | ADB | 說明 |
hdc -h | adb --help | 查看幫助 |
hdc -v | adb --version | 查看版本 |
hdc list targets | adb devices | 查看連接設(shè)備 |
hdc kill | adb kill-server | 結(jié)束服務(wù) |
hdc kill -r | adb start-server | 啟動服務(wù) |
hdc app install [安裝包路徑] | adb install [安裝包路徑] | 安裝應(yīng)用 |
hdc app uninstall package | adb uninstall package | 卸載應(yīng)用 |
hdc hilog | adb logcat | 抓取log |
hdc shell hilogcat >log.log | adb shell logcat >log.log | 抓取log并保存 |
hdc shell reboot | adb reboot | 重啟設(shè)備 |
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 | 發(fā)送文件 |
hdc shell screencap filename | adb shell screencap filename | 截屏 |
hdc shell screenrecord filename | adb shell screenrecord filename | 錄屏 |
三、日常開發(fā)常用命令
日常FA開發(fā)中以上命令的使用對于前端同學(xué)來說比較陌生,命令不容易記住。如:獲取UUID,命令記不住需要往期的賦能資料,最終才能獲取到UUID。針對命令記不住的問題可通過提供簡單腳本的方式,提供給前端使用。
注意:HDC服務(wù)和ADB服務(wù)不能共存。在執(zhí)行腳本時,如果獲取的數(shù)據(jù)為空,則需要確認(rèn)手機設(shè)備使用的是hdc命令還是ADB服務(wù)。
確認(rèn)方式:通過hdc list targets命令獲取設(shè)備列表,如果獲取到設(shè)備列表則當(dāng)前使用的為hdc服務(wù),否則為adb服務(wù)??赏ㄟ^adb kill-server結(jié)束adb服務(wù),再通過usb連接設(shè)備,使用hdc list targets命令獲取設(shè)備列表,如果獲取到列表則表示當(dāng)前服務(wù)使用的是hdc服務(wù)。如下圖操作:
(1)獲取UUID
腳本:uid.bat 已放置于下方資源處,如有需要自行下載。
hdc shell bm get -u > uuid.log
將該指令生成uuid.bat腳本,通過腳本獲取設(shè)備UUID。
(2)獲取hilog日志
腳本:hilog.bat 已放置于下方資源處,如有需要自行下載。
hdc hilog
(3)抓取log并保存
腳本:hilogs.bat 已放置于下方資源處,如有需要自行下載。
hdc shell hilogcat >log.log
總結(jié)
- HDC命令及ADB命令都可以通過腳本方式獲取或查看對應(yīng)數(shù)據(jù)。
- 前端同學(xué)可按需自己編寫腳本獲取自己所需數(shù)據(jù)。