ADB命令簡介
ADB是一個功能強大的命令行工具。通過它可以直接和模擬器或真機進行交互。它是一個具有客戶端和服務器端的程序。
它主要由三個部分組成:
- 客戶端,它運行在你的開發機上,你可以通過執行adb命令來喚起一個客戶端。其他的工具例如:ADT插件以及DDMS也可以創建一個ADB客戶端。
- 服務端,它運行在你的后台進程,這個服務管理客戶端和運行在你模擬起或真機上的守護進程。
- 守護進程,它運行在每台模擬器或真機的后台進程。
當你啟動一個adb客戶端的時候,客戶端會首先檢測是否已經有adb服務進程運行,如果沒有運行,那么它會首先啟動一個服務進程,當服務進程啟動后,服務進程綁定本地的5037端口,並且監聽所有來自adb 客戶端的命令,所有的adb客戶端通過5037與adb 服務端進行交互。
服務端運行之后與所有運行的模擬器或真機建立鏈接,它通過一組范圍從5555 到5585的奇數的端口檢索到所有的模擬器或真機。每一台模擬器或真機需要獲取一對連續的端口,偶數端口用於控制台的鏈接,奇數端口用於adb的鏈接。
當然,如果你用Eclipse開發,並且也安裝了ADT插件,那么你沒必要通過adb命令行與模擬器或真機交互,ADT插件已經完美的將adb整合進了Eclipse IDE.當然還是提倡多使用adb命令行,這樣的話比較方便自定義debug方法,同時也可以更好的理解其工作原理。
Adb命令列表
下列表格列出了adb支持的命令,並對它們的意義和使用方法做了說明.
Category | Command | Description | Comments |
---|---|---|---|
Options | -d |
僅僅通過USB接口來管理abd. | 如果不只是用USB接口來管理則返回錯誤. |
-e |
僅僅通過模擬器實例來管理adb. | 如果不是僅僅通過模擬器實例管理則返回錯誤. | |
-s <serialNumber> |
通過模擬器/設備的允許的命令號碼來發送命令來管理adb (比如: "emulator-5556"). | 如果沒有指定號碼,則會報錯. | |
General | devices |
查看所有連接模擬器/設備的設施的清單. | 查看 Querying for Emulator/Device Instances獲取更多相關信息. |
help |
查看adb所支持的所有命令。. | ||
version |
查看adb的版本序列號. | ||
Debug | logcat [<option>] [<filter-specs>] |
將日志數據輸出到屏幕上. | |
bugreport |
查看bug的報告,如dumpsys ,dumpstate ,和logcat 信息。 |
||
jdwp |
查看指定的設施的可用的JDWP信息. | 可以用 forward jdwp:<pid> 端口映射信息來連接指定的JDWP進程.例如: adb forward tcp:8000 jdwp:472 jdb -attach localhost:8000 |
|
Data | install <path-to-apk> |
安裝Android為(可以模擬器/設施的數據文件.apk指定完整的路徑). | |
pull <remote> <local> |
將指定的文件從模擬器/設施的拷貝到電腦上. | ||
push <local> <remote> |
將指定的文件從電腦上拷貝到模擬器/設備中. | ||
Ports and Networking | forward <local> <remote> |
用本地指定的端口通過socket方法遠程連接模擬器/設施 | 端口需要描述下列信息:
|
ppp <tty> [parm]... |
通過USB運行ppp:
需要提醒你的不能自動啟動PDP連接. |
||
Scripting | get-serialno |
查看adb實例的序列號. | 查看 Querying for Emulator/Device Instances可以獲得更多信息. |
get-state |
查看模擬器/設施的當前狀態. | ||
wait-for-device |
如果設備不聯機就不讓執行,--也就是實例狀態是 device 時. |
你可以提前把命令轉載在adb的命令器中,在命令器中的命令在模擬器/設備連接之前是不會執行其它命令的. 示例如下: adb wait-for-device shell getprop需要提醒的是這些命令在所有的系統啟動啟動起來之前是不會啟動adb的 所以在所有的系統啟動起來之前你也不能執行其它的命令. 比如:運用 install 的時候就需要Android包,這些包只有系統完全啟動。例如: adb wait-for-device install <app>.apk上面的命令只有連接上了模擬器/設備連接上了adb服務才會被執行,而在Android系統完全啟動前執行就會有錯誤發生. |
|
Server | start-server |
選擇服務是否啟動adb服務進程. | |
kill-server |
終止adb服務進程. | ||
Shell | shell |
通過遠程shell命令來控制模擬器/設備實例. | 查看 獲取更多信息 for more information. |
shell [<shellCommand>] |
連接模擬器/設施執行shell命令,執行完畢后退出遠程shell端l. |
adb命令參數詳解

# adb --help Android Debug Bridge version 1.0.29 -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 - directs command to the USB device or emulator with the given serial number. Overrides ANDROID_SERIAL environment variable. -p - 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 must be an absolute path. devices - list all connected devices connect [:] - connect to a device via TCP/IP Port 5555 is used by default if no port number is specified. disconnect [[:]] - 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 - copy file/dir to device adb pull [] - copy file/dir from device adb sync [ ] - copy host->device only if changed (-l means list but don't copy) (see 'adb help all') adb shell - run remote shell interactively adb shell - run remote shell command adb emu - run emulator console command adb logcat [ ] - View device log adb forward - forward socket connections forward specs are one of: tcp: localabstract: localreserved: localfilesystem: dev: jdwp: (remote only) adb jdwp - list PIDs of processes hosting a JDWP transport adb install [-l] [-r] [-s] [--algo --key --iv ] - push this package file to the device and install it ('-l' means forward-lock the app) ('-r' means reinstall the app, keeping its data) ('-s' means install on SD card instead of internal storage) ('--algo', '--key', and '--iv' mean the file is encrypted already) adb uninstall [-k] - remove this app package from the device ('-k' means keep the data and cache directories) adb bugreport - return all information from the device that should be included in a bug report. adb backup [-f ] [-apk|-noapk] [-shared|-noshared] [-all] [-system|-nosystem] [] - write an archive of the device's data to . 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.) (-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) ( 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 - restore device contents from the backup archive adb help - show this help message adb version - show version num scripting: adb wait-for-device - block until device is online 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: adb status-window - continuously print device status for a specified device adb remount - remounts the /system partition on the device read-write adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program adb reboot-bootloader - reboots the device into the bootloader adb root - restarts the adbd daemon with root permissions adb usb - restarts the adbd daemon listening on USB adb tcpip - restarts the adbd daemon listening on TCP on the specified port networking: adb ppp [parameters] - Run PPP over USB. Note: you should not automatically start a PPP connection. 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 [ ] can be interpreted in several ways: - If is not specified, both /system and /data partitions will be updated. - If it is "system" or "data", only the corresponding partition is updated. environmental 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.
查看所有當前通過USB設備連接的設備

# adb devices List of devices attached emulator-5554 device 0288424641407417 device HT9CRP810308 device
指定某一個模擬器或真機
# adb -s emulator-5554 shell
打印日志
# adb logcat
上傳一個文件
# adb push ./sina_login.png /mnt/sdcard/DCIM/Camera/sina_login.png
上傳一個目錄
# adb push ./emo /mnt/sdcard/DCIM/Camera/emo
下載一個文件
# adb pull /mnt/sdcard/DCIM/Camera/IMG_20121130_102210.jpg ./IMG_20121130_102210.jpg 280 KB/s (24741 bytes in 0.086s)
下載一個目錄,這里不用設置遞歸參數-R,而且空目錄沒有下載下來
# adb pull /mnt/sdcard/GigaTalk ./GigaTalk
安裝APK
# adb install BigramPaid.apk 1872 KB/s (30541229 bytes in 15.930s) pkg: /data/local/tmp/BigramPaid.apk
指定安裝到SD卡
# adb install -s BigramPaid.apk 2262 KB/s (30541229 bytes in 13.180s) pkg: /sdcard/tmp/BigramPaid.apk Success
再次安裝,提示錯誤信息:已安裝

# adb install -s BigramPaid.apk 2322 KB/s (30541229 bytes in 12.839s) pkg: /sdcard/tmp/BigramPaid.apk Failure [INSTALL_FAILED_ALREADY_EXISTS]
重新安裝,保留原有數據,允許共用包名及數字簽名
# adb install -r BigramPaid.apk 2526 KB/s (30541229 bytes in 11.806s) pkg: /data/local/tmp/BigramPaid.apk
保留數據以及緩存目錄
# adb uninstall com.gigabud.bigram
重新加載/system 分區
# adb remount
remount succeeded
安裝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 //包名已存在
啟用logcat日志
Android日志系統提供了記錄和查看系統調試信息的功能。日志都是從各種軟件和一些系統的緩沖區中記錄下來的,緩沖區可以通過 logcat
命令來查看和使用.
Logcat命令列表
Option | Description |
---|---|
-b <buffer> |
加載一個可使用的日志緩沖區供查看,比如event 和radio . 默認值是main 。具體查看Viewing Alternative Log Buffers. |
-c |
清楚屏幕上的日志. |
-d |
輸出日志到屏幕上. |
-f <filename> |
指定輸出日志信息的<filename> ,默認是stdout . |
-g |
輸出指定的日志緩沖區,輸出后退出. |
-n <count> |
設置日志的最大數目<count> .,默認值是4,需要和 -r 選項一起使用。 |
-r <kbytes> |
每<kbytes> 時輸出日志,默認值為16,需要和-f 選項一起使用. |
-s |
設置默認的過濾級別為silent. |
-v <format> |
設置日志輸入格式,默認的是brief 格式,要知道更多的支持的格式,參看Controlling Log Output Format . |
使用logcat命令
你可以用 logcat
命令來查看系統日志緩沖區的內容
[adb] logcat [<option>] ... [<filter-spec>] ...
請查看Listing of logcat Command Options ,它對logcat命令有詳細的描述 .
你也可以在你的電腦或運行在模擬器/設備上的遠程adb shell端來使用logcat
命令,也可以在你的電腦上查看日志輸出。
$ adb logcat //logcat
過濾日志輸出
每一個輸出的Android日志信息都有一個標簽和它的優先級.
- 日志的標簽是系統部件原始信息的一個簡要的標志。(比如:“View”就是查看系統的標簽).
- 優先級有下列集中,是按照從低到高順利排列的:
V
— Verbose (lowest priority)D
— DebugI
— InfoW
— WarningE
— ErrorF
— FatalS
— Silent (highest priority, on which nothing is ever printed)
在運行logcat的時候在前兩列的信息中你就可以看到 logcat
的標簽列表和優先級別,它是這樣標出的:<priority>/<tag>
.
下面是一個logcat輸出的例子,它的優先級就似乎I,標簽就是ActivityManage:
I/ActivityManager( 585): Starting activity: Intent { action=android.intent.action...}
為了讓日志輸出能體現管理的級別,你還可以用過濾器來控制日志輸出,過濾器可以幫助你描述系統的標簽等級.
過濾器語句按照下面的格式描tag:priority ...
, tag
表示是標簽, priority
是表示標簽的報告的最低等級.
從上面的tag的信息中可以得到日志的優先級. 你可以在過濾器中多次寫tag:priority
.
這些說明都只到空白結束。
下面有一個列子,例子表示支持所有的日志信息,除了那些標簽為”ActivityManager”和優先級為”Info”以上的和標簽為” MyApp”和優先級為” Debug”以上的。 小等級,優先權報告為tag.
adb logcat ActivityManager:I MyApp:D *:S
上面表達式的最后的元素 *:S
,,是設置所有的標簽為"silent",所有日志只顯示有"View" and "MyApp"的,用 *:S
的另一個用處是能夠確保日志輸出的時候是按照過濾器的說明限制的,也讓過濾器也作為一項輸出到日志中.
下面的過濾語句指顯示優先級為warning或更高的日志信息:
adb logcat *:W
控制日志輸出格式
日志信息包括了許多元數據域包括標簽和優先級。
修改日志的輸出格式,可以顯示出特定的元數據域。可以通過 -v
選項得到格式化輸出日志的相關信息。
brief
— Display priority/tag and PID of originating process (the default format).process
— Display PID only.tag
— Display the priority/tag only.thread
— Display process:thread and priority/tag only.raw
— Display the raw log message, with no other metadata fields.time
— Display the date, invocation time, priority/tag, and PID of the originating process.long
— Display all metadata fields and separate messages with a blank lines.
當啟動了logcat
,你可以通過-v
選項來指定輸出格式
[adb] logcat [-v <format>]
下面是用 thread
來產生的日志格式:
adb logcat -v thread
需要注意的是你只能-v
選項來規定輸出格式 option.
查看可用日志緩沖區
Android日志系統有循環緩沖區,並不是所有的日志系統都有默認循環緩沖區。
為了得到日志信息,你需要通過-b
選項來啟動logcat
。如果要使用循環緩沖區,你需要查看剩余的循環緩沖期
radio
— 查看緩沖區的相關的信息.events
— 查看和事件相關的的緩沖區.main
— 查看主要的日志緩沖區
-b
選項使用方法:
[adb] logcat [-b <buffer>]
下面的例子表示怎么查看日志緩沖區包含radio 和 telephony信息:
adb logcat -b radio
不完全匯總
(1) 顯示全部日志信息:adb logcat
(2) 顯示某一TAG的日志信息:adb logcat -s TAG名稱
(3) 顯示某一TAG的某一級別的日志信息:adb logcat TAG名稱:級別.....TAG名稱:級別 *:S
注:日志級別(V-verbose,D-debug,I-info,W-warning,E-error,F-fatal,S-silent),*:S(確保日志輸出的時候是按照過濾器的說明限制)
(4) 顯示某一級別以上的全部日志信息:adb logcat *:級別
(5) 以某種格式顯示日志信息:adb logcat -v 格式
注:日志格式(brief,process,tag,thread,raw,time,long)
(6) 顯示緩沖區中的日志信息:adb logcat -b 緩沖區類型
注:緩沖區類型(radio-無線緩沖區,events-事件緩沖區,main-主緩沖區,默認)
(7) 清理已經存在的日志:adb logcat -c
(8) 將日志顯示在控制台后退出:adb logcat -d
(9) 將日志輸出到文件:adb logcat -f 文件名
參考文章
http://www.cnblogs.com/playing/archive/2010/09/19/1830799.html
http://www.cnblogs.com/ithouge/archive/2012/12/09/ithouge_android_adb.html