你好,游客 登录
背景:
阅读新闻

adb命令详解 -

[日期:2013-04-14] 来源:  作者: [字体: ]

ADB是一个功能强大的命令行工具。通过它可以直接和模拟器或真机进行交互。它是一个具有客户端和服务器端的程序,它主要由三个部分组成:
1:客户端,它运行在你的开发机上,你可以通过执行adb命令来唤起一个客户端。其他的工具例如:ADT插件以及DDMS也可以创建一个ADB客户端。
2:服务端,它运行在你的后台进程,这个服务管理客户端和运行在你模拟起或真机上的守护进程。
3:守护进程,它运行在每台模拟器或真机的后台进程。

当你启动一个adb客户端的时候,客户端会首先检测是否已经有adb 服务进程运行,如果没有运行,那么它会首先启动一个服务进程,当服务进程启动后,服务进程绑定本地的5037端口,并且监听所有来自adb 客户端的命令,所有的adb客户端通过5037与adb 服务端进行交互。

服务端之后与所有运行的模拟器或真机建立链接,它通过一组范围从5555 到5585的奇数的端口检索到所有的模拟器或真机。每一台模拟器或真机需要获取一对连续的端口,偶数端口用于控制台的链接,奇数端口用于adb的链接。

当然,如果你用Eclipse开发,并且也安装了ADT插件,那么你没必要通过adb命令行与模拟器或真机交互,ADT插件已经完美的将adb整合进了Eclipse IDE.当然在此还是提倡多使用adb命令行,这样的话比较方便自定义一些debug方法,同时也可以更好的理解其工作原理。

adb命令参数详解:

 

  1 root@ithouge-OptiPlex-780:~# adb --help
  2 Android Debug Bridge version 1.0.29
  3 
  4  -d                            - directs command to the only connected USB device
  5                                  returns an error if more than one USB device is present.
  6  -e                            - directs command to the only running emulator.
  7                                  returns an error if more than one emulator is running.
  8  -s             - directs command to the USB device or emulator with
  9                                  the given serial number. Overrides ANDROID_SERIAL
 10                                  environment variable.
 11  -p      - simple product name like 'sooner', or
 12                                  a relative/absolute path to a product
 13                                  out directory like 'out/target/product/sooner'.
 14                                  If -p is not specified, the ANDROID_PRODUCT_OUT
 15                                  environment variable is used, which must
 16                                  be an absolute path.
 17  devices                       - list all connected devices
 18  connect [:]       - connect to a device via TCP/IP
 19                                  Port 5555 is used by default if no port number is specified.
 20  disconnect [[:]]  - disconnect from a TCP/IP device.
 21                                  Port 5555 is used by default if no port number is specified.
 22                                  Using this command with no additional arguments
 23                                  will disconnect from all connected TCP/IP devices.
 24 
 25 device commands:
 26   adb push      - copy file/dir to device
 27   adb pull  []  - copy file/dir from device
 28   adb sync [  ]     - copy host->device only if changed
 29                                  (-l means list but don't copy)
 30                                  (see 'adb help all')
 31   adb shell                    - run remote shell interactively
 32   adb shell           - run remote shell command
 33   adb emu             - run emulator console command
 34   adb logcat [  ] - View device log
 35   adb forward   - forward socket connections
 36                                  forward specs are one of:
 37                                    tcp:
 38                                    localabstract:
 39                                    localreserved:
 40                                    localfilesystem:
 41                                    dev:
 42                                    jdwp: (remote only)
 43   adb jdwp                     - list PIDs of processes hosting a JDWP transport
 44   adb install [-l] [-r] [-s] [--algo  --key  --iv ]
 45                                - push this package file to the device and install it
 46                                  ('-l' means forward-lock the app)
 47                                  ('-r' means reinstall the app, keeping its data)
 48                                  ('-s' means install on SD card instead of internal storage)
 49                                  ('--algo', '--key', and '--iv' mean the file is encrypted already)
 50   adb uninstall [-k]  - remove this app package from the device
 51                                  ('-k' means keep the data and cache directories)
 52   adb bugreport                - return all information from the device
 53                                  that should be included in a bug report.
 54 
 55   adb backup [-f ] [-apk|-noapk] [-shared|-noshared] [-all] [-system|-nosystem] []
 56                                - write an archive of the device's data to .
 57                                  If no -f option is supplied then the data is written
 58                                  to "backup.ab" in the current directory.
 59                                  (-apk|-noapk enable/disable backup of the .apks themselves
 60                                     in the archive; the default is noapk.)
 61                                  (-shared|-noshared enable/disable backup of the device's
 62                                     shared storage / SD card contents; the default is noshared.)
 63                                  (-all means to back up all installed applications)
 64                                  (-system|-nosystem toggles whether -all automatically includes
 65                                     system applications; the default is to include system apps)
 66                                  ( is the list of applications to be backed up.  If
 67                                     the -all or -shared flags are passed, then the package
 68                                     list is optional.  Applications explicitly given on the
 69                                     command line will be included even if -nosystem would
 70                                     ordinarily cause them to be omitted.)
 71 
 72   adb restore            - restore device contents from the  backup archive
 73 
 74   adb help                     - show this help message
 75   adb version                  - show version num
 76 
 77 scripting:
 78   adb wait-for-device          - block until device is online
 79   adb start-server             - ensure that there is a server running
 80   adb kill-server              - kill the server if it is running
 81   adb get-state                - prints: offline | bootloader | device
 82   adb get-serialno             - prints:
 83   adb status-window            - continuously print device status for a specified device
 84   adb remount                  - remounts the /system partition on the device read-write
 85   adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program
 86   adb reboot-bootloader        - reboots the device into the bootloader
 87   adb root                     - restarts the adbd daemon with root permissions
 88   adb usb                      - restarts the adbd daemon listening on USB
 89   adb tcpip              - restarts the adbd daemon listening on TCP on the specified port
 90 networking:
 91   adb ppp  [parameters]   - Run PPP over USB.
 92  Note: you should not automatically start a PPP connection.
 93   refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1
 94  [parameters] - Eg. defaultroute debug dump local notty usepeerdns
 95 
 96 adb sync notes: adb sync [  ]
 97    can be interpreted in several ways:
 98 
 99   - If  is not specified, both /system and /data partitions will be updated.
100 
101   - If it is "system" or "data", only the corresponding partition
102     is updated.
103 
104 environmental variables:
105   ADB_TRACE                    - Print debug information. A comma separated list of the following values
106                                  1 or all, adb, sockets, packets, rwx, usb, sync, sysdeps, transport, jdwp
107   ANDROID_SERIAL               - The serial number to connect to. -s takes priority over this if given.
108   ANDROID_LOG_TAGS             - When used with the logcat option, only these debug tags are printed.

//查看所有当前通过USB设备连接的设备

root@ithouge-OptiPlex-780:~# adb devices
List of devices attached
emulator-5554    device
0288424641407417    device
HT9CRP810308    device

//指定某一个模拟器或真机

root@ithouge-OptiPlex-780:~/图片# adb -s emulator-5554 shell
#

//打印日志

root@ithouge-OptiPlex-780:~# adb logcat

//上传一个文件

root@ithouge-OptiPlex-780:~/图片# adb push ./sina_login.png /mnt/sdcard/DCIM/Camera/sina_login.png
129 KB/s (5676 bytes in 0.042s)

//上传一个目录

root@ithouge-OptiPlex-780:~/图片# adb push ./emo /mnt/sdcard/DCIM/Camera/emo
push: ./emo/a1e6.png -> /mnt/sdcard/DCIM/Camera/emo/a1e6.png
push: ./emo/a1d7.png -> /mnt/sdcard/DCIM/Camera/emo/a1d7.png
push: ./emo/a1c2.png -> /mnt/sdcard/DCIM/Camera/emo/a1c2.png
push: ./emo/a1b3.png -> /mnt/sdcard/DCIM/Camera/emo/a1b3.png
push: ./emo/a1e2.png -> /mnt/sdcard/DCIM/Camera/emo/a1e2.png
push: ./emo/a1b2.png -> /mnt/sdcard/DCIM/Camera/emo/a1b2.png
push: ./emo/a1e3.png -> /mnt/sdcard/DCIM/Camera/emo/a1e3.png
push: ./emo/a1a1.png -> /mnt/sdcard/DCIM/Camera/emo/a1a1.png
push: ./emo/a1e4.png -> /mnt/sdcard/DCIM/Camera/emo/a1e4.png
push: ./emo/a1b4.png -> /mnt/sdcard/DCIM/Camera/emo/a1b4.png
push: ./emo/a1e5.png -> /mnt/sdcard/DCIM/Camera/emo/a1e5.png
push: ./emo/a1c7.png -> /mnt/sdcard/DCIM/Camera/emo/a1c7.png
push: ./emo/a1d2.png -> /mnt/sdcard/DCIM/Camera/emo/a1d2.png
push: ./emo/a1c6.png -> /mnt/sdcard/DCIM/Camera/emo/a1c6.png
push: ./emo/a1a7.png -> /mnt/sdcard/DCIM/Camera/emo/a1a7.png
push: ./emo/a1d3.png -> /mnt/sdcard/DCIM/Camera/emo/a1d3.png
push: ./emo/a1c5.png -> /mnt/sdcard/DCIM/Camera/emo/a1c5.png
push: ./emo/a1c4.png -> /mnt/sdcard/DCIM/Camera/emo/a1c4.png
push: ./emo/a1c3.png -> /mnt/sdcard/DCIM/Camera/emo/a1c3.png
push: ./emo/a1d4.png -> /mnt/sdcard/DCIM/Camera/emo/a1d4.png
push: ./emo/a1c1.png -> /mnt/sdcard/DCIM/Camera/emo/a1c1.png
push: ./emo/a1b7.png -> /mnt/sdcard/DCIM/Camera/emo/a1b7.png
push: ./emo/a1a4.png -> /mnt/sdcard/DCIM/Camera/emo/a1a4.png
push: ./emo/a1d6.png -> /mnt/sdcard/DCIM/Camera/emo/a1d6.png
push: ./emo/a1b6.png -> /mnt/sdcard/DCIM/Camera/emo/a1b6.png
push: ./emo/a1d5.png -> /mnt/sdcard/DCIM/Camera/emo/a1d5.png
push: ./emo/a1b1.png -> /mnt/sdcard/DCIM/Camera/emo/a1b1.png
push: ./emo/a1a3.png -> /mnt/sdcard/DCIM/Camera/emo/a1a3.png
push: ./emo/a1b5.png -> /mnt/sdcard/DCIM/Camera/emo/a1b5.png
push: ./emo/a1e7.png -> /mnt/sdcard/DCIM/Camera/emo/a1e7.png
push: ./emo/a1d1.png -> /mnt/sdcard/DCIM/Camera/emo/a1d1.png
push: ./emo/a1e1.png -> /mnt/sdcard/DCIM/Camera/emo/a1e1.png
push: ./emo/a1a6.png -> /mnt/sdcard/DCIM/Camera/emo/a1a6.png
push: ./emo/a1a5.png -> /mnt/sdcard/DCIM/Camera/emo/a1a5.png
push: ./emo/a1a2.png -> /mnt/sdcard/DCIM/Camera/emo/a1a2.png
35 files pushed. 0 files skipped.
130 KB/s (222845 bytes in 1.671s)

//下载一个文件

root@ithouge-OptiPlex-780:~/图片# adb pull /mnt/sdcard/DCIM/Camera/IMG_20121130_102210.jpg ./IMG_20121130_102210.jpg
280 KB/s (24741 bytes in 0.086s)

//下载一个目录,这里不用设置递归参数-R,而且空目录没有下载下来

root@ithouge-OptiPlex-780:~/图片# adb pull /mnt/sdcard/GigaTalk ./GigaTalk
pull: building file list...
pull: /mnt/sdcard/GigaTalk/589f6f99f0613ef6a8a901a2db7d533a/avatars/a1a5fc19-d490-48f7-ac19-7837c15b7a24 -> ./GigaTalk/589f6f99f0613ef6a8a901a2db7d533a/avatars/a1a5fc19-d490-48f7-ac19-7837c15b7a24
pull: /mnt/sdcard/GigaTalk/589f6f99f0613ef6a8a901a2db7d533a/avatars/cce75fea-0c50-4f07-8ec8-039ff811b625 -> ./GigaTalk/589f6f99f0613ef6a8a901a2db7d533a/avatars/cce75fea-0c50-4f07-8ec8-039ff811b625
pull: /mnt/sdcard/GigaTalk/589f6f99f0613ef6a8a901a2db7d533a/avatars/1189208a-f353-4227-b038-50cd9c6622bb -> ./GigaTalk/589f6f99f0613ef6a8a901a2db7d533a/avatars/1189208a-f353-4227-b038-50cd9c6622bb
pull: /mnt/sdcard/GigaTalk/589f6f99f0613ef6a8a901a2db7d533a/avatars/8615019451733 -> ./GigaTalk/589f6f99f0613ef6a8a901a2db7d533a/avatars/8615019451733
4 files pulled. 0 files skipped.
37 KB/s (32886 bytes in 0.846s)

//安装APK

root@ithouge-OptiPlex-780:/home/ithouge/APK/Bigram/2.5.6# adb install BigramPaid.apk
1872 KB/s (30541229 bytes in 15.930s)
    pkg: /data/local/tmp/BigramPaid.apk
Failure [INSTALL_FAILED_INSUFFICIENT_STORAGE]

//指定安装到SD卡

root@ithouge-OptiPlex-780:/home/ithouge/APK/Bigram/2.5.6# adb install -s BigramPaid.apk
2262 KB/s (30541229 bytes in 13.180s)
    pkg: /sdcard/tmp/BigramPaid.apk
Success

//再次安装,提示错误信息:已安装

root@ithouge-OptiPlex-780:/home/ithouge/APK/Bigram/2.5.6# adb install -s BigramPaid.apk
2322 KB/s (30541229 bytes in 12.839s)
    pkg: /sdcard/tmp/BigramPaid.apk
Failure [INSTALL_FAILED_ALREADY_EXISTS]

//重新安装,保留原有数据,允许共用包名及数字签名

root@ithouge-OptiPlex-780:/home/ithouge/APK/Bigram/2.5.6# adb install -r BigramPaid.apk
2526 KB/s (30541229 bytes in 11.806s)
    pkg: /data/local/tmp/BigramPaid.apk

以下是安装APK过程中常见的一些错误信息。

int INSTALL_FAILED_ALREADY_EXISTS 	              	程序已经存在
int INSTALL_FAILED_CONFLICTING_PROVIDER 	  	存在同名的内容提供者
int INSTALL_FAILED_DEXOPT 	 			dex优化验证失败
int INSTALL_FAILED_DUPLICATE_PACKAGE 	 		已存在同名程序
int INSTALL_FAILED_INSUFFICIENT_STORAGE 	 	没有足够的存储空间
int INSTALL_FAILED_INVALID_APK 	 			无效的APK
int INSTALL_FAILED_INVALID_URI 	 			无效的链接
int INSTALL_FAILED_MISSING_SHARED_LIBRARY 	 	需求的共享库已丢失
int INSTALL_FAILED_NO_SHARED_USER 	 		要求的共享用户不存在
int INSTALL_FAILED_OLDER_SDK 				系统版本过旧
int INSTALL_FAILED_REPLACE_COULDNT_DELETE
int INSTALL_FAILED_SHARED_USER_INCOMPATIBLE 	 	需求的共享用户签名错误
int INSTALL_FAILED_UPDATE_INCOMPATIBLE 	 		版本不能共存
int INSTALL_PARSE_FAILED_BAD_MANIFEST 	 		错误的MANIFEST配置文件
int INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME 	 	错误的包名
int INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID 	 	错误的共享用户
int INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING 	 	证书编码无效
int INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES	证书不一致
int INSTALL_PARSE_FAILED_MANIFEST_EMPTY 	 	MANIFEST配置文件为空
int INSTALL_PARSE_FAILED_MANIFEST_MALFORMED 	 	MANIFEST配置文件非法
int INSTALL_PARSE_FAILED_NOT_APK 	 		不是有效的APK文件
int INSTALL_PARSE_FAILED_NO_CERTIFICATES 	 	无认证证书
int INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION 	 	异常
int INSTALL_SUCCEEDED 	 				安装成功
int REPLACE_EXISTING_PACKAGE 			 	包名已存在

//'-k' 保留数据以及缓存目录

root@ithouge-OptiPlex-780:/home/ithouge/APK/Bigram/2.5.6# adb uninstall com.gigabud.bigram
Success

//重新加载/system 分区

root@ithouge-OptiPlex-780:/home/ithouge/APK/Bigram/2.5.6# adb remount
remount succeeded

 






收藏 推荐 打印 | 录入:admin | 阅读:
相关新闻