1. 什么是 adb ?
Android Debug Bridge,我们一般简称为 adb,主要存放在 sdk 安装目录下的 platform-tools 文件夹中,它是一个非常强大的命令行工具,通过这个工具你能够与你的 Android 设备进行交互。
2. 为什么要使用 adb ?
- 可以使用电脑直接控制手机
- 可以输出日志
- 可以进行随机性操作测试, 找到潜在性bug
3. 如何使用 adb ?
- 安装: 首先要下载 adb 到电脑上.
- 之前提过, adb 是 SDK 目录下的 platform-tools 文件夹内的一个文件, 所以, 如果你已经安装了 SDK, 那么你直接可以找到对应目录下的 adb 文件.
- 如果你没有安装过 SDK , 那么你可以点击[这里]直接下载 adb 包.
- 下载完成后, 对 adb.zip 进行解压, 解压后 adb.exe 所在路径为
D:/ADB
- 配置: 解压完成后, 我们要对adb 进行环境变量的配置.
- 将 adb 所在的路径(
D:/ADB
)复制到剪贴板, 然后回到桌面 - 找到
我的电脑
(或计算机
), 点击右键, 选择属性
- 点击左侧的
[高级系统设置]
- 点击下方的
[环境变量]
- 在
系统变量(S)
信息展示区, 找到并双击Path
- 点击右上角
[新建]
, 将复制的路径粘贴进去, 点击[确定]、[确定]、[确定]
- 现在就配置完成了, 可以进行下一步的启动操作了. . .
- 将 adb 所在的路径(
- 启动: 要启动 adb 命令, 需要使用
doc
窗口.-
win
+R
, 输入cmd
, 点击确定, 唤起doc
窗口 - 输入
adb version
, 按回车(Enter)
, 检查 adb 的环境变量
是否配置成功 - 如果显示 adb 的版本信息, 如:
Android Debug Bridge version 1.0.32
, 则说明配置成功 - 现在 adb 已经启动成功, 可以进行下一步操作了. . .
-
4. 常用的 adb 指令有哪些?
-
adb pull
从手机(远端)
下载(pull)
文件到电脑端(本地)
C:\Users\adminyang>adb pull /sdcard/xbrowser-release.apk D:\download
--> 指令格式: 'adb pull [设备目录/文件名] [PC端目录]'
'注意, PC端目录的写法'('Windows'):
C盘: ' C:\ ' 或 ' C:/ ' C盘和D盘实际上都是根目录, 根目录写法特殊, 要用 '\' or '/' 结尾
D盘: ' D:\ ' 或 ' D:/ '
download: ' D:\download ' 或 ' D:/download ' 除C盘D盘等根目录, 其他都以'文件夹名'结尾
other: ' D:\download\other ' 或 ' D:/download/other '
'手机设备的目录写法'('Linux'):
根目录: ' / ' 根目录, 只有一个' / '且只有这一种写法, 称为(正)斜杠
sdcard: ' /sdcard/ ' 其他目录的写法, 都是以'文件夹名/'结尾, 只用' / '
-
adb push
从电脑端(本地)
向手机(远端)
发送(push)
文件
C:\Users\adminyang>adb push D:\download\xbrowser-release.apk /sdcard/
--> 指令格式: 'adb push [PC端目录\文件名] [设备目录/]'
'手机设备的目录写法'('Linux'):
根目录: ' / ' 根目录, 只有一个' / '且只有这一种写法, 称为(正)斜杠
sdcard: ' /sdcard/ ' 其他目录的写法, 都是以'文件夹名/'结尾, 只用' / '
-
adb devices
查看当前已连接的手机终端数(含模拟器) -
adb connect
连接安卓模拟器(以夜神
模拟器为例)
C:\Users\adminyang>adb connect 127.0.0.1:62001
'注意': 127.0.0.1:62001 <本地IP地址>[:端口号]
-
adb logcat > log.txt
将错误日志导出到log.txt
文档中, 文档名可更改
- '导出日志的步骤': 1. adb devices 2. adb logcat > log.txt 3. 复现bug的步骤 4. Ctrl+C
- '^C' 使用Ctrl+C, 结束日志导出操作, 完成日志抓取, 具体操作如下:
C:\Users\adminyang>adb devices
List of devices attached
127.0.0.1:62001 device
C:\Users\adminyang>adb logcat > log.txt
^C
C:\Users\adminyang>
-
adb shell
进入设备(手机或模拟器)
C:\Users\adminyang>adb shell
root@shamu:/ #
注意: '#'表示管理员, '$'表示普通用户
-
adb shell dumpsys activity | find "mFocusedActivity"
抓取应用的<包名>
C:\Users\adminyang>adb shell dumpsys activity | find "mFocusedActivity"
mFocusedActivity: ActivityRecord{2673a12a u0 com.android.browser/.BrowserActivity t38}
注意: 'com.android.browser'就是我们要的'包名'
-
adb shell monkey
随机测试, 包含各种随机操作, 可以进行相关的参数配置
adb shell monkey -p com.android.browser -v -v --throttle 300 60000 > D:\L1.txt
'注意':
'-p 包名' 表示对这个应用包进行 monkey 测试
'-v -v' 表示测试报告的详细程度是[较详细]
'--throttle 300' 表示间隔300毫秒, 执行一次事件(activity)
'60000' 表示共执行6万个事件(activity)
'> D:/L1.txt' 表示将报告导出到文档 L1.txt 中
5. 案例:
- 绿色
指令
字符, 需要熟记使用方法. - 绿色汉字, 是对应
命令
的功能介绍.
C:\Users\adminyang>adb
Android Debug Bridge version 1.0.32
-a - directs adb to listen on all interfaces for a connection
` 指示adb监听连接的所有接口`
-d - directs command to the only connected USB device
returns an error if more than one USB device is present.
` 直接命令到唯一连接的USB设备`
` 如果存在多个USB设备,则返回一个错误。`
-e - directs command to the only running emulator.
returns an error if more than one emulator is running.
` 将命令指向唯一运行的模拟器。`
` 如果运行多个模拟器,则返回一个错误。`
-s <specific device> - directs command to the device or emulator with the given
serial number or qualifier. Overrides ANDROID_SERIAL
environment variable.
` 使用给定的命令将命令定向到设备或模拟器`
` 序号或限定符。覆盖ANDROID_SERIAL`
` 环境变量。`
`-p` <product name or path> - simple product name like 'sooner', or
a relative/absolute path to a product
out directory like 'out/target/product/sooner'.
If -p is not specified, the ANDROID_PRODUCT_OUT
environment variable is used, which must
be an absolute path.
` 简单的产品名称,如“sooner”或“or”`
` 产品的相对/绝对路径`
` out目录,例如“out/target/product/sooner”。`
` 如果没有指定-p,则使用ANDROID_PRODUCT_OUT`
` 必须使用环境变量`
` 做一条绝对的道路。`
-H - Name of adb server host (default: localhost)
` adb服务器主机名称(默认:localhost)`
-P - Port of adb server (default: 5037)
` adb服务器端口(默认:5037) `
`devices` [-l] - list all connected devices
('-l' will also list device qualifiers)
` 列出所有已连接的设备`
` ('-l'也会列出设备限定符)`
`connect` <host>[:<port>] - connect to a device via TCP/IP
Port 5555 is used by default if no port number is specified.
` 通过TCP/IP连接到设备`
` 如果没有指定端口号,则默认使用端口5555。`
`disconnect`[<host>[:<port>]] - disconnect from a TCP/IP device.
Port 5555 is used by default if no port number is specified.
Using this command with no additional arguments
will disconnect from all connected TCP/IP devices.
` 断开与TCP/IP设备的连接。`
` 如果没有指定端口号,则默认使用端口5555。`
` 使用此命令时不附加任何参数`
` 将断开与所有已连接的TCP/IP设备的连接。`
` device commands:`
`adb push` [-p] <local> <remote>
- copy file/dir to device
('-p' to display the transfer progress)
` 复制文件/目录到设备`
` ('-p'显示传送进度)`
`adb pull` [-p] [-a] <remote> [<local>]
- copy file/dir from device
('-p' to display the transfer progress)
('-a' means copy timestamp and mode)
` 从设备中复制文件/目录`
` ('-p'显示传送进度)`
` (“-a”表示复制时间戳和模式)`
adb sync [ <directory> ] - copy host->device only if changed
(-l means list but donot copy)
(see 'adb help all')
`adb shell` - run remote shell interactively
` 交互式地运行远程shell`
`adb shell` <command> - run remote shell command
`adb shell dumpsys activity | find "mFocusedActivity"`
` 查询当前运行中的程序的包名, 例如: `
`
C:\Users\adminyang>adb shell dumpsys activity | find "mFocusedActivity"
mFocusedActivity: ActivityRecord{2e56b4a4 u0 com.mmbox.xbrowser/.BrowserActivity t22}
`
` 上面案例中, 包名就是 com.mmbox.xbrowser 也就是 u0 和 / 之间的字符串`
adb emu <command> - run emulator console command
`adb logcat`[ <filter-spec> ] - View device log
` 查看设备日志`
` 导出日志: adb logcat > log01.txt`
adb forward --list - list all forward socket connections.
the format is a list of lines with the following format:
<serial> " " <local> " " <remote> "\n"
adb forward <local> <remote> - forward socket connections
forward specs are one of:
tcp:<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)
adb forward --no-rebind <local> <remote>
- same as 'adb forward <local> <remote>' but fails
if <local> is already forwarded
adb forward --remove <local> - remove a specific forward socket connection
adb forward --remove-all - remove all forward socket connections
adb reverse --list - list all reverse socket connections from device
adb reverse <remote> <local> - reverse socket connections
reverse specs are one of:
tcp:<port>
localabstract:<unix domain socket name>
localreserved:<unix domain socket name>
localfilesystem:<unix domain socket name>
adb reverse --norebind <remote> <local>
- same as 'adb reverse <remote> <local>' but fails
if <remote> is already reversed.
adb reverse --remove <remote>
- remove a specific reversed socket connection
adb reverse --remove-all - remove all reversed socket connections from device
adb jdwp - list PIDs of processes hosting a JDWP transport
`adb install`[-lrtsd] <file>
adb install-multiple [-lrtsdp] <file...>
- push this package file to the device and install it
(-l: forward lock application)
(-r: replace existing application)
(-t: allow test packages)
(-s: install application on sdcard)
(-d: allow version code downgrade)
(-p: partial application install)
` 将此包文件推到设备并安装它`
` (-l:转发锁应用)`
` (-r:替换现有的应用程序)`
` (-t:允许测试包)`
` (-s:在sdcard上安装应用程序)`
` (-d:允许降级版本代码)`
` (-p:部分应用安装)`
`adb uninstall`[-k] <package> - remove this app package from the device
('-k' means keep the data and cache directories)
` 从设备上删除这个app包`
` (“-k”表示保存数据和缓存目录)`
adb bugreport - return all information from the device
that should be included in a bug report.
` 从设备返回所有信息`
` 这应该包含在bug报告中`
adb backup [-f <file>] [-apk|-noapk] [-obb|-noobb] [-shared|-noshared] [-all] [-system|-nosystem] [<packages...>]
- write an archive of the `device's` data to <file>.
If no -f option is supplied then the data is written
to "backup.ab" in the current directory.
(-apk|-noapk enable/disable backup of the .apks themselves
in the archive; the default is noapk.)
(-obb|-noobb enable/disable backup of any installed apk expansion
(aka .obb) files associated with each application; the default
is noobb.)
(-shared|-noshared enable/disable backup of the `device's`
shared storage / SD card contents; the default is noshared.)
(-all means to back up all installed applications)
(-system|-nosystem toggles whether -all automatically includes
system applications; the default is to include system apps)
(<packages...> is the list of applications to be backed up. If
the -all or -shared flags are passed, then the package
list is optional. Applications explicitly given on the
command line will be included even if -nosystem would
ordinarily cause them to be omitted.)
adb restore <file> - restore device contents from the <file> backup archive
adb disable-verity - disable dm-verity checking on USERDEBUG builds
adb keygen <file> - generate adb public/private key. The private key is stored in <file>,
and the public key is stored in <file>.pub. Any existing files
are overwritten.
`adb help` - show this help message
`查看帮助信息`
`adb version` - show version num
`查看adb版本号`
scripting:
adb wait-for-device - block until device is online
adb start-server - ensure that there is a server running
adb kill-server - kill the server if it is running
adb get-state - prints : offline | bootloader | device
adb get-serialno - prints : <serial-number>
adb get-devpath - prints : <device-path>
adb status-window - continuously print device status for a specified device
adb remount - remounts the /system and /vendor (if present) partitions on the device read-write
adb reboot `[bootloader|recovery]`
- reboots the device, optionally into the bootloader or recovery program
adb reboot-bootloader - reboots the device into the bootloader
adb root - restarts the adbd daemon with root permissions
adb usb - restarts the adbd daemon listening on USB
adb tcpip <port> - restarts the adbd daemon listening on TCP on the specified port
networking:
adb ppp <tty> [parameters] - Run PPP over USB.
Note: you should not automatically start a PPP connection.
<tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1
[parameters] - Eg. defaultroute debug dump local notty usepeerdns
adb sync notes: adb sync [ <directory> ]
<localdir> can be interpreted in several ways:
- If <directory> is not specified, /system, /vendor (if present), and /data partitions will be updated.
- If it is "system", "vendor" or "data", only the corresponding partition
is updated.
environmental variables:
ADB_TRACE - Print debug information. A comma separated list of the following values
1 or all, adb, sockets, packets, rwx, usb, sync, sysdeps, transport, jdwp
ANDROID_SERIAL - The serial number to connect to. -s takes priority over this if given.
ANDROID_LOG_TAGS - When used with the logcat option, only these debug tags are printed.
`Writer: John(家和万事亨), 2019/5/8.`