ADB 使用教程

来源:互联网 发布:psp数据损坏 编辑:程序博客网 时间:2024/05/16 11:14
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 命令一览



 SYNTAXadb [ -d | -e | -s <serialNumber> ] <command>

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

常用ADB命令使用方法



Log

logcat [-bcdfgnrsv] <filter-spec>
adb logcat -c  #clear(flushes) the entire log and exitsadb logcat -d  #dumps the log to the screen and exitsadb logcat -b radio -f /sdcard/ril.log #输出Radio log到指定文件(default main)adb logcat -g  #prints the size of the specified log buffer and exitsadb 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

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


ADB Debugging

devices

adb devices  # print a list of attached emulator/device
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
shell pm list packages [-fdes3iu]
adb shell pm list packages -s #filter to only show system packagesadb shell pm list packages -3 #filter to only show 3rd party packagesadb shell pm list packages -u #also show uninstalled packages
shell pm path <package>
adb shell pm path com.android.phone


File Manager

pull <remote> <local>
adb pull /sdcard/demo.mp4 #copy file to adb tools directoryadb pull /sdcard/demo.mp4 e:\ #copy file to driver e:\
push <local> <remote>
adb push test.apk /sdcard   #copy adb tools directory to sdcardadb push d:\test.apk /sdcard
shell ls [options] <directory>
adb shell ls -s #print size of each fileadb shell ls -n #list numeric UIDs and GIDsadb shell ls -R  #list subdirectories recursively


System

root

adb root #restarts the adbd daemon with root permissions
shell getprop
adb shell getprop #list all the system property via property serviceadb shell getprop ro.build.version.sdkadb shell getprop ro.chipname
如果要获取某一个关键词的property,可按照如下方法操作:
  • adb shell
  • getprop | grep ril // 获得RIL相关的属性值

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

参考资料:

ADB Shell



0 0
原创粉丝点击