级别:★☆☆☆☆
标签:「adb 常用命令」「adb」
作者: ITWYW
审校: aTaller团队
前言
笔者最近在做 Flutter 应用的时候,使用的安卓手机测试。用到了部分 adb 知识,笔者将在本文中分享 adb 常用命令。
一、adb 简介
adb 的全称为 Android Debug Bridge,就是起到调试桥的作用。通过 adb 我们可以在 Eclipse 中方便通过 DDMS 来调试 Android 程序,说白了就是 debug 工具。adb 的工作方式比较特殊,采用监听 Socket TCP 5554 等端口的方式让 IDE 和 Qemu 通讯,默认情况下 adb 会 daemon 相关的网络端口,所以当我们运行 Eclipse 时 adb 进程就会自动运行。
adb 是 android sdk 里的一个工具, 用这个工具可以直接操作管理 android 模拟器或者真实的 android 设备(如360手机). 它的主要功能有:
- 运行设备的 shell (命令行)
- 管理模拟器或设备的端口映射
- 计算机和设备之间上传/下载文件
- 将本地 apk 软件安装至模拟器或 android 设备
ADB 是一个 客户端-服务器端 程序, 其中客户端是你用来操作的电脑, 服务器端是 android 设备。
引自 360 百科 adb
当我们输入 adb 或 adb help (用于查看 adb 帮助内容)时会输出如下内容。
adb 或 adb help 查看 adb 使用方式
列出来的内容比较多,笔者挑选了几个命令在下文做了简单说明。
wangyongwangdeiMac:~ wangyongwang$ adb
Android Debug Bridge version 1.0.41
Version 30.0.1-6435xx6
Installed as /Users/wangyongwang/Library/Android/sdk/platform-tools/adb
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] 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: ( may be "tcp:0" to pick any open port)
localabstract:
localreserved:
localfilesystem:
dev:
jdwp: (remote only)
acceptfd: (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: ( may be "tcp:0" to pick any open port)
localabstract:
localreserved:
localfilesystem:
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
--local-agent: locate agent files from local source build (instead of SDK location)
(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
get-devpath print
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
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)
wangyongwangdeiMac:~ wangyongwang$
adb version
wangyongwangdeiMac:~ wangyongwang$ adb version
Android Debug Bridge version 1.0.41
Version 30.0.1-643xxx6
Installed as /Users/wangyongwang/Library/Android/sdk/platform-tools/adb
adb devices
wangyongwangdeiMac:~ wangyongwang$ adb devices
List of devices attached
W4XUT19A1500xxx8 device
adb shell pm list packages
wangyongwangdeiMac:~ wangyongwang$ adb shell pm list packages
package:com.huawei.scenepack
adb install
adb install xxx.apk
adb uninstall 包名
adb uninstall 包名
adb shell screencap /sdcard/screen.png
adb shell screencap /sdcard/screen.png
adb push
wangyongwangdeiMac:~ wangyongwang$ adb -s W4XUT19A1500xxx8 push ./网址.txt /sdcard/1/
./网址.txt: 1 file pushed, 0 skipped. 0.0 MB/s (154 bytes in 0.014s)
adb pull
wangyongwangdeiMac:~ wangyongwang$ adb -s W4XUT19A1500xxx8 pull /sdcard/1/网址.txt
/sdcard/1/网址.txt: 1 file pulled, 0...kipped. 0.0 MB/s (154 bytes in 0.005s)
adb bugreport
adb bugreport 使用用于生成 bug 文件的命令。生成的文件可以结合着上述的 adb pull 使用,把生成出来的安卓系统认为是出现了的bug日志文件,导出到电脑端,进行查看。
wangyongwangdeiMac:~ wangyongwang$ adb bugreport
[ 0%] generating bugreport-SPN-AL00-HUAWEISPN-AL00-2020-06-11-22-05-48.zip
adb bugreport 目录
adb bugreport 目录 可以把安卓系统认为是出现了的bug日志文件,以zip包的方式存放到指定目录下。
如果上述命令遇到了问题,可以使用如下命令
在终端中执行如下操作:
adb bugreport
wangyongwang$ adb pull /data/user_de/0/com.android.shell/files/bugreports/bugreport-SPN-AL00-HUAWEISPN-AL00-2020-06-11-22-05-48.zip ./
/data/user_de/0/com.android.shell/file...d. 31.6 MB/s (2522694 bytes in 0.076s)
上述命令可以把生成的bugreport文件存放到电脑的当前目录,方便我们进行分析。
二、CPU
cpu 信息
adb shell dumpsys cpuinfo
wangyongwangdeiMac:~ wangyongwang$ adb shell dumpsys cpuinfo
Load: 42.96 / 43.08 / 42.84
CPU usage from 349122ms to 146876ms ago (2020-06-09 20:40:52.356 to 2020-06-09 20:44:14.602):
16% 11190/com.huawei.appmarket: 11% user + 4.9% kernel / faults: 58855 minor 200 major
adb shell dumpsys cpuinfo |grep 包名
wangyongwangdeiMac:~ wangyongwang$ adb shell dumpsys cpuinfo |grep com.xxx.xxx
3.5% 9190/com.xxx.xxx: 0.9% user + 2.6% kernel / faults: 18 minor
0.2% 9276/com.xxx.xxx:xyz: 0.1% user + 0% kernel / faults: 17 minor
adb shell top
^[[24;80RTasks: 608 total, 1 running, 564 sleeping, 0 stopped, 0 zombie
Tasks: 608 total, 1 running, 564 sleeping, 0 stopped, 0 zombie
Tasks: 608 total, 1 running, 564 sleeping, 0 stopped, 0 zombie
Tasks: 608 total, 2 running, 563 sleeping, 0 stopped, 0 zombie
Tasks: 609 total, 1 running, 565 sleeping, 0 stopped, 0 zombie
Tasks: 609 total, 1 running, 565 sleeping, 0 stopped, 0 zombie
Tasks: 609 total, 1 running, 565 sleeping, 0 stopped, 0 zombie
Tasks: 609 total, 2 running, 564 sleeping, 0 stopped, 0 zombie
Mem: 5855228k total, 5552676k used, 302552k free, 8116k buffers
Swap: 2293756k total, 349792k used, 1943964k free, 2365044k cached
800%cpu 7%user 2%nice 20%sys 753%idle 0%iow 7%irq 10%sirq 0%host
PID USER PR NI VIRT RES SHR S[%CPU] %MEM TIME+ ARGS
1139 root -2 0 0 0 0 R 9.6 0.0 2:41.70 [hisi_hcc]
1096 root 0 -20 0 0 0 S 6.0 0.0 2:03.60 [wlan_bus_rx/sd+
11723 shell 20 0 12M 2.9M 1.6M R 5.3 0.0 0:01.15 top
1269 system 20 0 4.1G 283M 183M S 3.6 4.9 16:54.13 system_server
上边的 800% CPU 是因为手机是8核的,后边依次次有 user 占用 7% 的CPU,sys 占用了 20% 的 CPU,及753%的空闲 CPU。
三、adb shell
adb shell 会进入 shell 环境
退出 adb exit
130|HWSPN:/sdcard $ exit
wangyongwangdeiMac:~ wangyongwang$
进入到shell的环境下后,我们就可以使用Linux的命令去操作手机。
比如
ls:查看当前目录有哪些文件夹,哪些文件
cd 目录:进入到某个目录
cd .. 返回到上一级的目录
rmdir 文件夹名称:删除某个文件夹
四、参考学习网址
- adb
- adb
- Android 调试桥 (adb)
- Why is the “top” command showing a CPU usage of 799%?
- ADB 常用命令
- adb 常用命令
- Android学习第一天-adb常用命令
- adb shell top 命令详解
- Android 性能测试实践 (三) Cpu
- 阿里巴巴 Android 性能测试工具 mobileperf 开源 (天猫精灵 Android 性能测试-线下篇)
- ADB—查看设备信息
关注我们的途径有:
aTaller()
aTaller(掘金)
aTaller(微信公众号)
推荐文章:
Flutte 开发小技巧
Flutter 常用 Widget 介绍
Flutter 图片加载
Flutter 混合栈复用原理
Flutter Platform Channel 使用与源码分析
Flutter Platform View 使用及原理简析
奇舞周刊