社区编辑申请
注册/登录
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 来源: 鸿蒙社区
相关推荐

2022-06-20 22:37:25

Linux操作系统命令

2022-06-17 09:47:04

Linux命令

2022-06-15 16:16:21

分布式数据库鸿蒙

2022-05-24 15:06:57

AbilityeTS FA鸿蒙

2022-04-28 15:38:42

WebViewJavaUI

2022-06-13 12:43:13

Java模块

2022-06-01 17:47:24

运维监控系统

2022-05-28 16:14:13

手机更新APP

2022-04-24 15:26:38

服务卡鸿蒙

2022-04-13 11:24:18

ETS开发HarmonyOS鸿蒙

2022-04-07 15:28:16

HarmonyOS鸿蒙操作系统

2022-03-02 15:59:37

HarmonyOS操作系统鸿蒙

2022-02-25 14:57:33

harmonyOSjava心形动画

2022-04-15 14:31:02

鸿蒙操作系统

2022-06-06 10:20:59

CPUCPU 使用率CPU 负载

2022-06-10 09:00:53

前端项目个JSON

2022-06-10 07:45:09

CentOS国产操作系统

2022-06-20 09:01:23

2022-06-21 14:30:16

Vim自定义Linux

2022-06-17 09:08:27

代码Python内置库

同话题下的热门内容

HarmonyOS - HDC命令与ADB命令使用对比OHOS构建自定义服务实战啃论文俱乐部—数据密集型应用内存压缩HarmonyOS - 自定义组件之计时器基于OpenHarmony3.1的购物车应用的实现HarmonyOS - 方舟开发框架ArkUI 流光按钮效果OpenHarmony3.1-Ace-Formcomponent源码解析OpenHarmony HiSysEvent打点调用实践(L2)

编辑推荐

HarmonyOS 2.0鸿蒙第二期开发者Beta公测申请指南HarmonyOS LYEVK-3861开发板播放《蜜雪冰城》鸿蒙HarmonyOS分布式软总线:构建低时延、高带宽的多设备虚拟网络华为HarmonyOS的强势突围: 直面物联网迷宫的蓄力进击鸿蒙HarmonyOS2.0发布会现场回忆录
我收藏的内容
点赞
收藏

51CTO技术栈公众号