ADB使用技巧

adb全称Android Debug Bridge,主要用作调试Android。

1. ADB基础命令

1.1 ADB帮助

  1 adb help
  2 Android Debug Bridge version 1.0.41
  3 Version 33.0.1-8253317
  4 
  5 global options:
  6  -a                       listen on all network interfaces, not just localhost
  7  -d                       use USB device (error if multiple devices connected)
  8  -e                       use TCP/IP device (error if multiple TCP/IP devices available)
  9  -s SERIAL                use device with given serial (overrides $ANDROID_SERIAL)
 10  -t ID                    use device with given transport id
 11  -H                       name of adb server host [default=localhost]
 12  -P                       port of adb server [default=5037]
 13  -L SOCKET                listen on given socket for adb server [default=tcp:localhost:5037]
 14  --one-device SERIAL|USB  only allowed with 'start-server' or 'server nodaemon', server will only connect to one USB device, specified by a serial number or USB device address.
 15 
 16 general commands:
 17  devices [-l]             list connected devices (-l for long output)
 18  help                     show this help message
 19  version                  show version num
 20 
 21 networking:
 22  connect HOST[:PORT]      connect to a device via TCP/IP [default port=5555]
 23  disconnect [HOST[:PORT]]
 24      disconnect from given TCP/IP device [default port=5555], or all
 25  pair HOST[:PORT] [PAIRING CODE]
 26      pair with a device for secure TCP/IP communication
 27  forward --list           list all forward socket connections
 28  forward [--no-rebind] LOCAL REMOTE
 29      forward socket connection using:
 30        tcp:<port> (<local> may be "tcp:0" to pick any open port)
 31        localabstract:<unix domain socket name>
 32        localreserved:<unix domain socket name>
 33        localfilesystem:<unix domain socket name>
 34        jdwp:<process pid> (remote only)
 35        vsock:<CID>:<port> (remote only)
 36        acceptfd:<fd> (listen only)
 37  forward --remove LOCAL   remove specific forward socket connection
 38  forward --remove-all     remove all forward socket connections
 39  ppp TTY [PARAMETER...]   run PPP over USB
 40  reverse --list           list all reverse socket connections from device
 41  reverse [--no-rebind] REMOTE LOCAL
 42      reverse socket connection using:
 43        tcp:<port> (<remote> may be "tcp:0" to pick any open port)
 44        localabstract:<unix domain socket name>
 45        localreserved:<unix domain socket name>
 46        localfilesystem:<unix domain socket name>
 47  reverse --remove REMOTE  remove specific reverse socket connection
 48  reverse --remove-all     remove all reverse socket connections from device
 49  mdns check               check if mdns discovery is available
 50  mdns services            list all discovered services
 51 
 52 file transfer:
 53  push [--sync] [-z ALGORITHM] [-Z] LOCAL... REMOTE
 54      copy local files/directories to device
 55      --sync: only push files that are newer on the host than the device
 56      -n: dry run: push files to device without storing to the filesystem
 57      -z: enable compression with a specified algorithm (any/none/brotli/lz4/zstd)
 58      -Z: disable compression
 59  pull [-a] [-z ALGORITHM] [-Z] REMOTE... LOCAL
 60      copy files/dirs from device
 61      -a: preserve file timestamp and mode
 62      -z: enable compression with a specified algorithm (any/none/brotli/lz4/zstd)
 63      -Z: disable compression
 64  sync [-l] [-z ALGORITHM] [-Z] [all|data|odm|oem|product|system|system_ext|vendor]
 65      sync a local build from $ANDROID_PRODUCT_OUT to the device (default all)
 66      -n: dry run: push files to device without storing to the filesystem
 67      -l: list files that would be copied, but don't copy them
 68      -z: enable compression with a specified algorithm (any/none/brotli/lz4/zstd)
 69      -Z: disable compression
 70 
 71 shell:
 72  shell [-e ESCAPE] [-n] [-Tt] [-x] [COMMAND...]
 73      run remote shell command (interactive shell if no command given)
 74      -e: choose escape character, or "none"; default '~'
 75      -n: don't read from stdin
 76      -T: disable pty allocation
 77      -t: allocate a pty if on a tty (-tt: force pty allocation)
 78      -x: disable remote exit codes and stdout/stderr separation
 79  emu COMMAND              run emulator console command
 80 
 81 app installation (see also `adb shell cmd package help`):
 82  install [-lrtsdg] [--instant] PACKAGE
 83      push a single package to the device and install it
 84  install-multiple [-lrtsdpg] [--instant] PACKAGE...
 85      push multiple APKs to the device for a single package and install them
 86  install-multi-package [-lrtsdpg] [--instant] PACKAGE...
 87      push one or more packages to the device and install them atomically
 88      -r: replace existing application
 89      -t: allow test packages
 90      -d: allow version code downgrade (debuggable packages only)
 91      -p: partial application install (install-multiple only)
 92      -g: grant all runtime permissions
 93      --abi ABI: override platform's default ABI
 94      --instant: cause the app to be installed as an ephemeral install app
 95      --no-streaming: always push APK to device and invoke Package Manager as separate steps
 96      --streaming: force streaming APK directly into Package Manager
 97      --fastdeploy: use fast deploy
 98      --no-fastdeploy: prevent use of fast deploy
 99      --force-agent: force update of deployment agent when using fast deploy
100      --date-check-agent: update deployment agent when local version is newer and using fast deploy
101      --version-check-agent: update deployment agent when local version has different version code and using fast deploy
102      (See also `adb shell pm help` for more options.)
103  uninstall [-k] PACKAGE
104      remove this app package from the device
105      '-k': keep the data and cache directories
106 
107 debugging:
108  bugreport [PATH]
109      write bugreport to given PATH [default=bugreport.zip];
110      if PATH is a directory, the bug report is saved in that directory.
111      devices that don't support zipped bug reports output to stdout.
112  jdwp                     list pids of processes hosting a JDWP transport
113  logcat                   show device log (logcat --help for more)
114 
115 security:
116  disable-verity           disable dm-verity checking on userdebug builds
117  enable-verity            re-enable dm-verity checking on userdebug builds
118  keygen FILE
119      generate adb public/private key; private key stored in FILE,
120 
121 scripting:
122  wait-for[-TRANSPORT]-STATE...
123      wait for device to be in a given state
124      STATE: device, recovery, rescue, sideload, bootloader, or disconnect
125      TRANSPORT: usb, local, or any [default=any]
126  get-state                print offline | bootloader | device
127  get-serialno             print <serial-number>
128  get-devpath              print <device-path>
129  remount [-R]
130       remount partitions read-write. if a reboot is required, -R will
131       will automatically reboot the device.
132  reboot [bootloader|recovery|sideload|sideload-auto-reboot]
133      reboot the device; defaults to booting system image but
134      supports bootloader and recovery too. sideload reboots
135      into recovery and automatically starts sideload mode,
136      sideload-auto-reboot is the same but reboots after sideloading.
137  sideload OTAPACKAGE      sideload the given full OTA package
138  root                     restart adbd with root permissions
139  unroot                   restart adbd without root permissions
140  usb                      restart adbd listening on USB
141  tcpip PORT               restart adbd listening on TCP on PORT
142 
143 internal debugging:
144  start-server             ensure that there is a server running
145  kill-server              kill the server if it is running
146  reconnect                kick connection from host side to force reconnect
147  reconnect device         kick connection from device side to force reconnect
148  reconnect offline        reset offline/unauthorized devices to force reconnect
149 
150 usb:
151  attach                   attach a detached USB device
152  detach                   detach from a USB device to allow use by other processes
153 environment variables:
154  $ADB_TRACE
155      comma-separated list of debug info to log:
156      all,adb,sockets,packets,rwx,usb,sync,sysdeps,transport,jdwp
157  $ADB_VENDOR_KEYS         colon-separated list of keys (files or directories)
158  $ANDROID_SERIAL          serial number to connect to (see -s)
159  $ANDROID_LOG_TAGS        tags to be used by logcat (see logcat --help)
160  $ADB_LOCAL_TRANSPORT_MAX_PORT max emulator scan port (default 5585, 16 emus)
161  $ADB_MDNS_AUTO_CONNECT   comma-separated list of mdns services to allow auto-connect (default adb-tls-connect)
adb help

1.2 ADB服务

# 查看设备
$ adb devices

# 关闭服务
$ adb kill-server

# 启动服务
$ adb start-server

1.3 安装/卸载

# 安装应用程序
# adb install [-l|-r|-t|-s|-d|-p|-g] <PACKAGE>
#     -l // 将应用安装到保护目录.
#     -r // 覆盖安装,保持原程序数据.
#     -t // 允许安装AndroidManifest.xml里application指定android:testOnly="true"的应用.
#     -s // 将应用安装到sdcard
#     -d // 允许降级覆盖安装
#     -p // 安装部分应用
#     -g // 授予所有运行时权限
$ adb install test.apk
$ adb install -l test.apk
# 卸载应用程序
# adb uninstall [-k] <PACKAGE>
#     -k // 卸载,保留数据和缓存目录
$ adb uninstall com.xxx.xxx
$ adb uninstall -k com.xxx.xxx

1.4 文件传输

# 复制本地文件/目录到设备
# adb push [--sync] [-z ALGORITHM] [-Z] <LOCAL...> <REMOTE>
#     --sync // 只推送主机上比设备上更新的文件
#     -n // 试运行:将文件推入设备而不存储到文件系统
#     -z // 启用指定算法的压缩(any/none/brotli/lz4/zstd)
#     -Z // 禁用压缩
$ adb push test.apk /sdcard
# 复制设备文件/目录到主机
# adb pull [-a] [-z ALGORITHM] [-Z] <REMOTE...> <LOCAL>
#     -a // 保留文件时间戳和模式
#     -z // 启用指定算法的压缩(any/none/brotli/lz4/zstd)
#     -Z // 禁用压缩
$ adb pull /sdcard/test.apk ./apkdir
# 从$ANDROID_PRODUCT_OUT同步一个本地构建到设备(默认全部)
# adb sync [-l] [-z ALGORITHM] [-Z] [all|data|odm|oem|product|system|system_ext|vendor]
#     -n // 试运行:将文件推入设备而不存储到文件系统
#     -l // 列出要复制的文件,但不要复制它们
#     -z // 启用指定算法的压缩(any/none/brotli/lz4/zstd)
#     -Z // 禁用压缩
$ adb sync vendor

1.5 其他

# 重新安装分区读写
# adb remount [-R]
#     -R // 如果需要重新启动,-R将自动重新启动设备

# 重新启动设备;默认为启动系统映像,但也支持启动bootloader和recovery
# adb reboot [bootloader|recovery|sideload|sideload-auto-reboot]
#     default // 重启系统
#     bootloader // 重启到bootloader
#     recovery // 重启到recovery模式
#     sideload // 重新启动到recovery,并自动启动sideload模式
#     sideload-auto-reboot // sideloading后重启

# sideload给定的完整OTA包
# adb sideload <OTAPACKAGE>
# 重新启动具有root权限的adbd
# adb root

# 在没有root权限的情况下重启adbd
# adb unroot

# 重启adbd监听USB
# adb usb

2. ADB网络调试

Android 11及以上版本,打开wireless debugging,进行无线连接调试。

Android 10及以下版本,操作如下:

# 1.主机端对设备端执行
$ adb tcpip 5555
# 2.主机端通过网络链接设备端
$ adb connect <device_ip_address>:5555

3. ADB Log

3.1 logcat

# 显示Android log
$ adb logcat

# 清除log
$ adb logcat -c

# 保存log
$ adb logcat > data.log

# 过滤log
$ adb logcat | grep xxx

3.2 dmesg

# 显示kernel log
$ adb shell dmesg

3.3 dumpsys

# 用户系统诊断,提供系统服务状态信息
# adb shell dumpsys [-l|power|activity|cpuinfo|window|meminfo|package|...]
#     -l // 列出所支持的dump
#     power // 查询和power manager service信息
#     activity // 查询AMS信息
#     cpuinfo // 查询CPU信息
#     window // 查询WMS信息
#     meminfo // 查询内存信息
#     package // 查询PMS信息
$ adb shell dumpsys power

3.4 bugreport

# 包含系统服务、错误日志和系统消息日志的诊断输出
$ adb bugreport

3.5 jdwp

# 列出承载JDWP传输的进程PID
$ adb jdwp

4. ADB Shell

4.1 基础命令

ls、cd、catrmcpmkdir、...

使用方式1:

$ adb shell ls

使用方式2:

$ adb shell
> ls
> exit

4.2 input信息

# KEYCODE_BACK
$ adb shell input keyevent 4

键值信息可参考代码:

/frameworks/base/core/java/android/view/KeyEvent.java

4.3 屏幕信息

# 获取分辨率
$ adb shell vm size

# 设置分辨率
$ adb shell vm size 1920x1080

# 还原分辨率
$ adb shell vm size reset

# 获取屏幕密度
$ adb shell vm density

# 设置屏幕密度
$ adb shell vm density 441

# 还原屏幕密度
$ adb shell vm density reset

4.4 录屏

$ adb shell screenrecord --bit-rate 2000000 --size 1920x1080 /data/scr1.mp4

4.5 属性操作

# 获取属性
$ adb shell getprop ro.build.version.release

# 设置属性
$ adb shell setprop ro.duershow.ota.productid 1094

4.6 设置操作

# /data/system/users/0/settings_global.xml
# adb shell settings get global <key> <default value>
# adb shell settings put global <key> <value>

# /data/system/users/0/settings_system.xml
# adb shell settings get system <key> <default value>
# adb shell settings put system <key> <value>

# /data/system/users/0/settings_secure.xml
# adb shell settings get secure <key> <default value>
# adb shell settings put secure <key> <value>

# 获取当前亮度
$ adb shell settings get system screen_brightness

# 设置当前亮度
$ adb shell settings put system screen_brightness 255

4.7 AM/PM

# 拨打电话10010
$ adb shell am start -a android.intent.action.CALL -d tel:10010

# 列出所有安装包
$ adb shell pm list package

 

posted @ 2022-09-28 11:38  this毛豆  阅读(243)  评论(0编辑  收藏  举报