您的位置:首页 > 运维架构 > Shell

ADB 使用教程

2016-01-05 16:54 351 查看
Android Debug Bridge(ADB)是一个多用途的命令行工具,通过它用户可以与模拟器或者安卓设备进行通信。 ADB采用的是Client-server模型来进行数据交换,其主要有三个部分组成:
客户端(client)程序在开发机器上运行。用户可以通过一个ADB shell命令来激活客户端程序;另外,通过ADT plugin和DDMS工具也可以创建ADB客户端;
服务端(Server)程序作为后台应用在开发机器上运行,它用于管理客户端与运行在模拟器或者设备上的ADB daemon(守护进程)之间的通信;
守护进程(daemon)作为后台进程在模拟器或者设备上运行;
当用户开始一个ADB客户端程序时,客户端首先会检查是否已经有一个ADB服务进程在运行,如果没有,它会启动一个服务进程;服务进程启动后,会与一个本地TCP端口5037进行绑定,并监听从ADB客户端发送过来的命令(所有的客户端都使用端口5037与服务进程进行通信)。

接着,服务端进程会配置所有正在运行的模拟器或者设备的链接:通过扫描5555 - 5585(模拟器/设备使用的端口范围)之间的奇数端口,服务端进程可以找到相应的模拟器/设备,接着找到相应的ADB守护进程,并对该端口进行配置。注意,每个模拟器/设备都需要一对连续的端口 - 奇数端口号用于ADB连接,偶数端口号用于控制台连接。

如何开启ADB调试

为使用ADB,需要通过USB来连接设备,因此首先必须启用 USB debugging模式,其设置路径一般为 
settings > developer Options > USB debugging。

对于Android4.2及以上的版本,developer options默认是隐藏状态。需要到 settings > about phone(有些手机可能是device)中,点击 Build Number 7次,即可让该选择可见。 

ADB 命令一览

 SYNTAX
adb [ -d | -e | -s <serialNumber> ] <command>
[align=justify]类型[/align]
[align=justify]命令[/align]
[align=justify]作用[/align]
[align=justify]例子[/align]
[align=center]目标设备[/align]
[align=center]-d[/align]
[align=justify]将ADB命令导向唯一链接的USB设备;如果有多于一个设备可用,则返回错误[/align]
[align=justify] [/align]
[align=center]-e[/align]
[align=justify]将ADB命令导向唯一可用的模拟器;如果有多于一个个模拟器可用,则返回错误[/align]
[align=justify] [/align]
[align=center]-s<serialNumber>[/align]
[align=justify]用模拟器或者设备分配的序列号进行命令传输[/align]
[align=justify]adb  –s  emulator-5555   install hellworld.apk[/align]
[align=center]一般[/align]
[align=center]devices[/align]
打印输出所有可用的模拟器/设备,输出的格式[serialNumber]
[state]
[align=justify]adb devices[/align]
[align=center]help[/align]
[align=justify]打印输出所有adb命令[/align]
[align=justify] [/align]
[align=center]version[/align]
[align=justify]Adb版本号[/align]
[align=justify] [/align]
[align=center]调试[/align]
[align=center]logcat [option] [filter-specs][/align]
[align=justify]输出log数据[/align]
[align=justify] adb logcat -b radio > /sdcard/ril.log[/align]
[align=center]bugreport[/align]
[align=left]Bug reporting输出dumpsys,dumpstate,logcat[/align]
 adb bugreport > bugreport.txt
[align=left]// output data to current dir of host[/align]
[align=center]jdwp[/align]
[align=justify]输出设备上的JDWP进程[/align]
[align=justify] [/align]
[align=center]数据[/align]
[align=center]install <path-to-apk>[/align]
[align=justify]安装给定路径的APK到模拟器/设备上[/align]
[align=justify]adb intall /sdcard/helloworld.apk[/align]
[align=center]uninstall [options] <pakage>[/align]
[align=justify]从模拟器/设备上移除APK[/align]
[align=justify]adb uninstall com.test.app[/align]
[align=center]pull <remote> <local>[/align]
[align=justify]从模拟器/设备上复制指定的文件到开发机上[/align]
[align=justify]adb  pull  /sdcard/demo.apk e:\myfilefolder\[/align]
[align=center]push<local> <remote>[/align]
[align=justify]从开发机上复制文件到模拟器/设备上[/align]
[align=justify]adb push d:\test.apk  /sdcard[/align]
[align=center]服务[/align]
[align=center]start-server[/align]
[align=justify]如果ADB服务端进程没有启动,启动一个进程[/align]
[align=justify] [/align]
[align=center]kill-server[/align]
[align=justify]终止ADB服务端进程[/align]
[align=justify] [/align]
[align=center]Shell[/align]
[align=center]shell[/align]
[align=justify]创建一个远程Shell进程[/align]
[align=justify] [/align]
[align=center]shell [shell-command][/align]
[align=justify]发送一个shell命令到模拟器/设备上[/align]
[align=justify] [/align]
[align=center]端口与网络[/align]
[align=center]forward<local> <remote>[/align]
[align=justify]将本地端口的Socket连接转移到模拟器/设备上的指定端口[/align]
[align=justify]adb forward tcp:8000 tcp:9000 [/align]
[align=justify]// setup forwarding of host port 8000 to emulator/device port 9000[/align]
[align=justify]  [/align]
 

常用ADB命令使用方法

[align=justify][/align]
[align=justify]Log[/align]
[align=justify]
[/align]
[align=justify]logcat [-bcdfgnrsv] <filter-spec>[/align]
adb logcat -c  #clear(flushes) the entire log and exits
adb logcat -d  #dumps the log to the screen and exits
adb logcat -b radio -f /sdcard/ril.log #输出Radio log到指定文件(default main)
adb logcat -g  #prints the size of the specified log buffer and exits
adb logcat -v brief  #display priority/tag/PID of the process(default)
adb logcat -v process  #display PID only
dumpsys
adb shell dumpsys   #输出system data
dumpstate
adb shell dumpstate


[align=justify]注意,对于有输出的命令,默认的输出对象是stdout,通过重定向的方式,我们可以输出到指定的文件:[/align]
[align=justify]adb shell dumpstate > /sdcard/state.log[/align]

ADB Debugging

devices

[align=justify]
adb devices  # print a list of attached emulator/device
[/align]
forward <local> <remote>,示例:
adb forward tcp:8000 tcp:9000 #set up forwarding of host 8000 to emulator/device port 9000
kill-server
adb kill-server # kill the server if it's running(terminate adb.exe process)



Package Manager

install [-lrtsd] <file>, 括号的参数含义可用help命令查看
adb install -s myapp.apk #install the package on SD card

uninstall <package-name>
adb uninstall com.my.testapp
adb uninstall -k com.my.test.app  # keep the data and cache directories around after removal



[align=justify]shell pm list packages [-fdes3iu][/align]
adb shell pm list packages -s #filter to only show system packages
adb shell pm list packages -3 #filter to only show 3rd party packages
adb shell pm list packages -u #also show uninstalled packages

[align=justify]shell pm path <package>[/align]
[align=justify]
adb shell pm path com.android.phone
[/align]


File Manager

[align=justify]pull <remote> <local>[/align]
adb pull /sdcard/demo.mp4 #copy file to adb tools directory
adb pull /sdcard/demo.mp4 e:\ #copy file to driver e:\

[align=justify]push <local> <remote>[/align]
adb push test.apk /sdcard   #copy adb tools directory to sdcard
adb push d:\test.apk /sdcard

[align=justify]shell ls [options] <directory>[/align]
adb shell ls -s #print size of each file
adb shell ls -n #list numeric UIDs and GIDs
adb shell ls -R  #list subdirectories recursively



System

root

[align=justify]
adb root #restarts the adbd daemon with root permissions
shell getprop[/align]
adb shell getprop #list all the system property via property service
adb shell getprop ro.build.version.sdk
adb shell getprop ro.chipname

[align=justify]如果要获取某一个关键词的property,可按照如下方法操作:[/align]
adb shell
getprop | grep ril // 获得RIL相关的属性值

shell setprop <key> <value> // 重置system property值
adb shell ro.secure 1

参考资料:

ADB Shell
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android adb shell debug 调试