HarmonyOS - HDC命令与ADB命令使用对比

系统 OpenHarmony
HDC,即HarmonyOS Device Connector,是HarmonyOS为开发人员提供的用于调试的命令行工具,通过该工具可以在windows/linux/mac系统上与真实设备或者模拟器进行交互。

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​

前言

作为多年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

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服务。如下图操作:

#夏日挑战赛# HarmonyOS - HDC命令与ADB命令使用对比-开源基础软件社区

(1)获取UUID

脚本:uid.bat 已放置于下方资源处,如有需要自行下载。

hdc shell bm get -u > uuid.log

将该指令生成uuid.bat脚本,通过脚本获取设备UUID。

#夏日挑战赛# HarmonyOS - HDC命令与ADB命令使用对比-开源基础软件社区

(2)获取hilog日志

脚本:hilog.bat 已放置于下方资源处,如有需要自行下载。

hdc hilog

#夏日挑战赛# HarmonyOS - HDC命令与ADB命令使用对比-开源基础软件社区

(3)抓取log并保存

脚本:hilogs.bat 已放置于下方资源处,如有需要自行下载。

hdc shell hilogcat >log.log

总结

  • HDC命令及ADB命令都可以通过脚本方式获取或查看对应数据。
  • 前端同学可按需自己编写脚本获取自己所需数据。

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​​。

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2013-07-03 10:19:52

Android开发adb shell命令

2010-01-27 10:07:11

Android adb

2016-12-07 18:42:31

Androidadb shell命令

2017-09-10 16:41:32

ADB命令程序员

2012-05-10 08:55:11

Linuxuniq

2009-12-25 16:27:25

Linux命令

2024-03-26 15:21:43

2009-08-03 12:02:13

linux at命令linux at命令详使用案例

2023-04-05 08:33:10

2010-04-28 10:04:15

unixware

2019-11-25 15:46:11

LinuxVim命令

2010-02-03 16:53:58

CentOS yum

2010-03-05 15:05:46

Ubuntu网络配置

2013-02-21 17:54:12

PowerShell入PowerShell命

2011-02-18 10:19:47

SELinux命令管理

2011-03-15 15:47:25

iptables安装命令

2010-07-16 09:32:14

Telnet服务

2011-04-01 17:16:03

zabbix参数命令

2012-05-09 10:52:37

Linux监控命令

2021-04-06 09:46:15

Linuxanacron命令系统运维
点赞
收藏

51CTO技术栈公众号