Android SDK中的必会工具-ADB

前面的内容请参见Android应用自动化测试-提纲。 这篇我们将介绍在Android SDK中一个非常关键的工具,ADB。可以说ADB是Android开发、自动化测试的基础。


ADB即Android Debug Bridge,android调试桥,是开发设备(PC)和android设备之间的连接通道,通过它,开发者可以在PC端实现对设备的连接、控制和一些基本操作。

ADB的组成如图

在PC端:
ADB由Client和Server两部分组成,client即我们运行ADB的命令行程序,用于接收我们的操作指定,并和server端通过5037端口进行通信,server端启动后和5037端口绑定,与client端交互并建立和设备端的adbd通信,一般设备端是5555端口,PC端则使用5554端口。多个不同设备会成对分配(5556:5557...)
在Device端:
则运行了adbd,即adb在设备上的守护进程,随时接收来自PC端的指令。模拟器是默认开启的,真机在我们开启了开发者的调试模式以后,也就是启动了adbd进程。

ADB工具的作用非常多,在命令行中运行adb help可以得到以下输出,列出了adb支持的使用命令清单。

c:>adb help
Android Debug Bridge version 1.0.36
Revision 0e9850346394-android

-a                            - directs adb to listen on all interfaces for a connection
-d                            - directs command to the only connected USB device
                                returns an error if more than one USB device is present.
-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.
-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 mustbe an absolute path.
-H                              - Name of adb server host (default: localhost)
-P                              - Port of adb server (default: 5037)
devices [-l]                   - list all connected devices
                             ('-l' will also list device qualifiers)
connect <host>[:<port>]       - connect to a device via TCP/IP
                             Port 5555 is used by default if no port number is specified.
 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.

device commands:

adb push <local>... <remote>
                           - copy files/dirs to device
adb pull [-a] <remote>... <local>
                           - copy files/dirs from device
                             (-a preserves file timestamp and mode)
adb sync [ <directory> ]     - copy host->device only if changed
                             (-l means list but don't copy)
adb 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: force PTY allocation)
                             (-x: disable remote exit codes and stdout/stderr separation)
adb emu <command>            - run emulator console command
adb logcat [ <filter-spec> ] - View device log
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 --no-rebind <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 [-lrtsdg] <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 (debuggable packages only))
                             (-g: grant all runtime permissions)
adb install-multiple [-lrtsdpg] <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 (debuggable packages only))
                             (-p: partial application install)
                             (-g: grant all runtime permissions)
adb uninstall [-k] <package> - remove this app package from the device
                             ('-k' means keep the data and cache directories)
adb bugreport [<path>]       - return all information from the device that should be included in a       zipped bug report.
                             If <path> is a file, the bug report will be saved as that file.
                             If <path> is a directory, the bug report will be saved in that directory with the name provided by the device.
                             If <path> is omitted, the bug report will be saved in the current directory with the name provided by the device.
                             NOTE: if the device does not support zipped bug reports, the bug report will be output on stdout.
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 enable-verity            - re-enable 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

scripting:

adb wait-for[-<transport>]-<state>
                           - wait for device to be in the given state:
                             device, recovery, sideload, or bootloader
                             Transport is: usb, local or any [default=any]
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 remount                  - remounts the /system, /vendor (if present) and /oem (if present) partitions on the device read-write
adb reboot [bootloader|recovery]
                           - reboots the device, optionally into the bootloader or recovery program.
adb reboot sideload          - reboots the device into the sideload mode in recovery program (adb root required).
adb reboot sideload-auto-reboot
                           - reboots into the sideload mode, then reboots automatically after the sideload regardless of the result.
adb sideload <file>          - sideloads the given package
adb root                     - restarts the adbd daemon with root permissions
adb unroot                   - restarts the adbd daemon without 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), /oem (if present) and /data partitions will be updated.
- If it is "system", "vendor", "oem" or "data", only the corresponding partition is updated.

internal debugging:

adb reconnect                  Kick current connection from host side and make it reconnect.
adb reconnect device           Kick current connection from device side and make it reconnect.

environment 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.

其中有一些我们经常需要使用的命令,在此重点说明一下:

adb devices

adb devices用于列出当前连接到adb server的所有设备清单。

c:\>adb devices
List of devices attached
emulator-5554   device

其中 emulator-5554是DeviceID,device则是设备状态。
一般有以下状态:

offline — 实例未连接到 adb server或不响应。
device — 实例现在已连接到 adb server。
no device — 未连接模拟器/设备。

当连接了多个设备时,我们可以使用-s参数来指定需要操作的设备,-s所带参数即deviceid。另外如果多个连接的设备中只有一个通过USB连接的真机,可以使用-d参数,如果只有一个模拟器,可以使用-e参数(无需带deviceid)

adb install/uninstall

adb可以方便地向设备上进行应用的安装和卸载操作。

adb install [apkpath]      给定apk文件的路径,执行即可
adb uninstall packagename  uninstall操作需要给出应用的包名

packagename如何获得? 一般可以通过adb shell pm list packages列出确定。
通过adb shell进入/data/data目录,也可以看到所有的包

adb push/pull

adb push/pull则是文件操作
adb push [pc file path] [device file path]-- 将PC端文件上载到设备端
adb pull [device file path] [pc file path]--将设备端文件下载到PC端

adb forword

adb forward用于端口转发。可以使用 forward 命令设置将对特定主机端口的请求转发到模拟器/设备实例上的其他端口。

adb forward tcp:6100 tcp:7100

表示将PC端的6100端口的通信转发到设备端的7100端口上

adb kill-server/start-server

当adb server进程出现异常时,我们可以会用adb kill-server 命令来停止adb server进程。
adb start-server则用来启动adb server进程。但其实执行任何adb命令,在server没有运行时,都会启动adb server

adb connect/disconnect

当我们的设备在无线wifi环境下时,我们可以通过adb connect命令来连接指定ip的设备。默认设备端口是5555
adb disconnect则用于断开已经建立的连接
比如genymotion模拟器,就是默认模拟的这种连接方式

adb logcat

adb logcat用于将logcat日志信息输出到屏幕。logcat的作用我们在后文再详细说明

adb shell

adb shell则是可以直接进入设备并执行大量的系统指令。进入adb shell后,即可执行很多类linux的系统指令,如:
操作Activity manager的am命令,可以直接从命令行启动一个应用的Activity。

adb shell am start -a android.intent.action.VIEW

利用包管理器pm来卸载应用

adb shell pm uninstall com.example.MyApp

进行屏幕截图

adb shell screencap /sdcard/screen.png

录制操作视频

adb shell screenrecord /sdcard/demo.mp4

更多用法可参考: 官网说明

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容