安卓adb工具

adb介绍

ADB是android sdk里的一个工具,用这个工具可以直接操作管理android模拟器或者真实的andriod设备。 ADB是一个客户端-服务器端程序,其中客户端是你用来操作的电脑,服务器端是android设备。

它的主要功能有:

  • 运行设备的shell(命令行)
  • 管理模拟器或设备的端口映射
  • 计算机和设备之间上传/下载文件
  • 将本地apk软件安装至模拟器或android设备

adb是Android Debug Bridge的缩写,它是一个C/S架构的工具。
Client端运行在PC上,可以通过它对Android进行安装卸载和调试。Eclipse中的ADT, SDK Tools里的DDMS、Monitor等工具,也用了adb与Android设备交互。

adb一般在Android SDK中,比如我的SDK目录如下:

1
2
3
4
5
6
7
8
9
10
localhost:platform-tools niuxinli$ pwd
/Users/niuxinli/Library/Android/sdk/platform-tools
localhost:platform-tools niuxinli$ ls
NOTICE.txt etc1tool package.xml
adb fastboot source.properties
api hprof-conv sqlite3
dmtracedump lib systrace
localhost:platform-tools niuxinli$ ./adb devices
List of devices attached
emulator-5554 device

可以把adb配置到path中。

adb与设备连接方式

adb可以通过usb或者wifi与设备连接,如果启动了模拟器,也可以通过adb连接。
比如我现在通过usb连了一个真机,还启动了一个模拟器

1
2
3
4
5
6
7
localhost:platform-tools niuxinli$ ./adb devices
List of devices attached
ce53cf4 device
emulator-5554 device

localhost:platform-tools niuxinli$ ./adb shell
error: more than one device/emulator

提示多余一个设备,所以用usb只能使用一个设备。我们可以通过usb连接在设备上启动一个server,然后就可以通过wifi连接了。
首先它们需要位于同一个wifi,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#先用usb连接
localhost:platform-tools niuxinli$ ./adb devices
List of devices attached
ce53cf4 device
#在设备上建立一个server,端口为5555
localhost:platform-tools niuxinli$ ./adb tcpip 5555
#连接
localhost:platform-tools niuxinli$ ./adb connect 192.168.43.1
connected to 192.168.43.1:5555
#发现有两个设备
localhost:platform-tools niuxinli$ ./adb devices
List of devices attached
192.168.43.1:5555 device
ce53cf4 device
localhost:platform-tools niuxinli$ ./adb shell
error: more than one device/emulator

这时候把设备从usb取出就可以了。
通过

1
adb kill-server

可以把设备里的server关了。

adb功能

所有adb shell xxx的命令,都可以先通过adb shell进入shell,再执行xxx命令,就跟linux是一样的。

1
2
3
4
5
6
7
localhost:platform-tools niuxinli$ ./adb shell
QK1707:/ $ pwd
/
QK1707:/ $ ls /mnt
ls: /mnt/media_rw: Permission denied
ls: /mnt/asec: Permission denied
appfuse expand obb runtime sdcard secure user

应用包管理

adb install

1
2
3
4
5
6
7
adb install test.apk
adb install -l test.apk #forward lock application
adb install -r test.apk #replace existing application
adb install -t test.apk #allow test packages
adb install -s test.apk #install application on sdcard
adb install -d test.apk #allow version code downgrade
adb install -p test.apk #partial application install

adb uninstall

1
2
adb uninstall com.test.app
adb uninstall -k com.test.app #Keep the data and cache directories around after package removal.

adb shell pm list packages

1
2
3
4
5
6
7
8
9
adb shell pm list packages
adb shell pm list packages -f #See their associated file.
adb shell pm list packages -d #Filter to only show disabled packages.
adb shell pm list packages -e #Filter to only show enabled packages.
adb shell pm list packages -s #Filter to only show system packages.
adb shell pm list packages -3 #Filter to only show third party packages.
adb shell pm list packages -i #See the installer for the packages.
adb shell pm list packages -u #Also include uninstalled packages.
adb shell pm list packages --user <USER_ID> #The user space to query.

adb shell pm path

给出指定package的apk路径

1
adb shell pm path com.android.phone

adb shell pm clear

删除所有相关的数据、缓存

1
adb shell pm clear com.test.abc

文件管理

adb pull

1
adb pull /sdcard/demo.mp3

把文件拿到当前目录

1
adb pull /scard/demo.mp3 /test

把文件拿到目录/test

同样还有把本地文件放到设备上

1
2
adb push test.apk /sdcard
adb push d:\test.apk /sdcard

其它的ls,cd,mv等都跟linux一样。

打印日志

1
adb logcat

截屏

adb shell screencap

1
2
adb shell screencap /sdcard/screen.png
adb pull /sdcard/screen.png

adb shell screenrecord

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
recording the display of devices running Android 4.4 (API level 19) and higher.

adb shell screenrecord [options] <filename>
adb shell screenrecord /sdcard/demo.mp4

(press Ctrl-C to stop recording)

download the file from the device

adb pull /sdcard/demo.mp4

Notes: Stop the screen recording by pressing Ctrl-C, otherwise the recording stops automatically at three minutes or the time limit set by --time-limit.
adb shell screenrecord --size <WIDTHxHEIGHT>
Sets the video size: 1280x720. The default value is the device's native display resolution (if supported), 1280x720 if not. For best results, use a size supported by your device's Advanced Video Coding (AVC) encoder.

adb shell screenrecord --bit-rate <RATE>
Sets the video bit rate for the video, in megabits per second. The default value is 4Mbps. You can increase the bit rate to improve video quality, but doing so results in larger movie files. The following example sets the recording bit rate to 5Mbps: adb shell screenrecord --bit-rate 5000000 /sdcard/demo.mp4

adb shell screenrecord --time-limit <TIME>
Sets the maximum recording time, in seconds. The default and maximum value is 180 (3 minutes).

adb shell screenrecord --rotate
Rotates the output 90 degrees. This feature is experimental.

adb shell screenrecord --verbose
Displays log information on the command-line screen. If you do not set this option, the utility does not display any information while running.

dump

dumpstate

1
2
adb shell dumpstate
adb shell dumpstate > state.logs #dumps state to a file

dumpsys

1
2
3
4
5
6
7
8
9
10
11
12
13
14
dumps system data

adb shell dumpsys [options]
adb shell dumpsys
adb shell dumpsys meminfo

adb shell dumpsys battery
Notes: A mobile device with Developer Options enabled running Android 5.0 or higher.
adb shell dumpsys batterystats collects battery data from your device
Notes: Battery Historian converts that data into an HTML visualization. STEP 1 adb shell dumpsys batterystats > batterystats.txt STEP 2 python historian.py batterystats.txt > batterystats.html
adb shell dumpsys batterystats --reset erases old collection data
adb shell dumpsys activity

adb shell dumpsys gfxinfo com.android.phone measuring com.android.phone ui performance