Android SDK中的必會工具-ADB

前面的內(nèi)容請參見Android應(yīng)用自動化測試-提綱耕魄。 這篇我們將介紹在Android SDK中一個非常關(guān)鍵的工具桌粉,ADB宣脉∨缡校可以說ADB是Android開發(fā)、自動化測試的基礎(chǔ)救鲤。


ADB即Android Debug Bridge久窟,android調(diào)試橋,是開發(fā)設(shè)備(PC)和android設(shè)備之間的連接通道本缠,通過它斥扛,開發(fā)者可以在PC端實現(xiàn)對設(shè)備的連接、控制和一些基本操作。

ADB的組成如圖

在PC端:
ADB由Client和Server兩部分組成稀颁,client即我們運行ADB的命令行程序芬失,用于接收我們的操作指定,并和server端通過5037端口進行通信匾灶,server端啟動后和5037端口綁定棱烂,與client端交互并建立和設(shè)備端的adbd通信,一般設(shè)備端是5555端口阶女,PC端則使用5554端口颊糜。多個不同設(shè)備會成對分配(5556:5557...)
在Device端:
則運行了adbd,即adb在設(shè)備上的守護進程秃踩,隨時接收來自PC端的指令衬鱼。模擬器是默認開啟的,真機在我們開啟了開發(fā)者的調(diào)試模式以后憔杨,也就是啟動了adbd進程鸟赫。

ADB工具的作用非常多,在命令行中運行adb help可以得到以下輸出消别,列出了adb支持的使用命令清單抛蚤。

c:>adb help
Android Debug Bridge version 1.0.36
Revision 0e9850346394-android

-a                            - directs adb to listen on all interfaces for a connection
-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 <specific device>          - directs command to the device or emulator with the given
                                serial number or qualifier. Overrides ANDROID_SERIAL environment variable.
-p <product name or path>     - 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 mustbe an absolute path.
-H                              - Name of adb server host (default: localhost)
-P                              - Port of adb server (default: 5037)
devices [-l]                   - list all connected devices
                             ('-l' will also list device qualifiers)
connect <host>[:<port>]       - connect to a device via TCP/IP
                             Port 5555 is used by default if no port number is specified.
 disconnect [<host>[:<port>]]  - 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 <local>... <remote>
                           - copy files/dirs to device
adb pull [-a] <remote>... <local>
                           - copy files/dirs from device
                             (-a preserves file timestamp and mode)
adb sync [ <directory> ]     - copy host->device only if changed
                             (-l means list but don't copy)
adb shell [-e escape] [-n] [-Tt] [-x] [command]
                           - run remote shell command (interactive shell if no command given)
                             (-e: choose escape character, or "none"; default '~')
                             (-n: don't read from stdin)
                             (-T: disable PTY allocation)
                             (-t: force PTY allocation)
                             (-x: disable remote exit codes and stdout/stderr separation)
adb emu <command>            - run emulator console command
adb logcat [ <filter-spec> ] - View device log
adb forward --list           - list all forward socket connections.
                             the format is a list of lines with the following format:
                                <serial> " " <local> " " <remote> "\n"
adb forward <local> <remote> - forward socket connections
                             forward specs are one of:
                               tcp:<port>
                               localabstract:<unix domain socket name>
                               localreserved:<unix domain socket name>
                               localfilesystem:<unix domain socket name>
                               dev:<character device name>
                               jdwp:<process pid> (remote only)
  adb forward --no-rebind <local> <remote>
                           - same as 'adb forward <local> <remote>' but fails
                             if <local> is already forwarded
  adb forward --remove <local> - remove a specific forward socket connection
  adb forward --remove-all     - remove all forward socket connections
  adb reverse --list           - list all reverse socket connections from device
  adb reverse <remote> <local> - reverse socket connections
                             reverse specs are one of:
                               tcp:<port>
                               localabstract:<unix domain socket name>
                               localreserved:<unix domain socket name>
                               localfilesystem:<unix domain socket name>
  adb reverse --no-rebind <remote> <local>
                           - same as 'adb reverse <remote> <local>' but fails
                             if <remote> is already reversed.
  adb reverse --remove <remote>
                           - remove a specific reversed socket connection
  adb reverse --remove-all     - remove all reversed socket connections from device
  adb jdwp                     - list PIDs of processes hosting a JDWP transport
adb install [-lrtsdg] <file>
                           - push this package file to the device and install it
                             (-l: forward lock application)
                             (-r: replace existing application)
                             (-t: allow test packages)
                             (-s: install application on sdcard)
                             (-d: allow version code downgrade (debuggable packages only))
                             (-g: grant all runtime permissions)
adb install-multiple [-lrtsdpg] <file...>
                           - push this package file to the device and install it
                             (-l: forward lock application)
                             (-r: replace existing application)
                             (-t: allow test packages)
                             (-s: install application on sdcard)
                             (-d: allow version code downgrade (debuggable packages only))
                             (-p: partial application install)
                             (-g: grant all runtime permissions)
adb uninstall [-k] <package> - remove this app package from the device
                             ('-k' means keep the data and cache directories)
adb bugreport [<path>]       - return all information from the device that should be included in a       zipped bug report.
                             If <path> is a file, the bug report will be saved as that file.
                             If <path> is a directory, the bug report will be saved in that directory with the name provided by the device.
                             If <path> is omitted, the bug report will be saved in the current directory with the name provided by the device.
                             NOTE: if the device does not support zipped bug reports, the bug report will be output on stdout.
adb backup [-f <file>] [-apk|-noapk] [-obb|-noobb] [-shared|-noshared] [-all] [-system|-nosystem] [<packages...>]
                           - write an archive of the device's data to <file>.
                             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.)
                             (-obb|-noobb enable/disable backup of any installed apk expansion
                                (aka .obb) files associated with each application; the default
                                is noobb.)
                             (-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)
                             (<packages...> 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 <file>           - restore device contents from the <file> backup archive
adb disable-verity           - disable dm-verity checking on USERDEBUG builds
adb enable-verity            - re-enable dm-verity checking on USERDEBUG builds
adb keygen <file>            - generate adb public/private key. The private key is stored in <file>,
                             and the public key is stored in <file>.pub. Any existing files
                             are overwritten.
adb help                     - show this help message
adb version                  - show version num

scripting:

adb wait-for[-<transport>]-<state>
                           - wait for device to be in the given state:
                             device, recovery, sideload, or bootloader
                             Transport is: usb, local or any [default=any]
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: <serial-number>
adb get-devpath              - prints: <device-path>
adb remount                  - remounts the /system, /vendor (if present) and /oem (if present) partitions on the device read-write
adb reboot [bootloader|recovery]
                           - reboots the device, optionally into the bootloader or recovery program.
adb reboot sideload          - reboots the device into the sideload mode in recovery program (adb root required).
adb reboot sideload-auto-reboot
                           - reboots into the sideload mode, then reboots automatically after the sideload regardless of the result.
adb sideload <file>          - sideloads the given package
adb root                     - restarts the adbd daemon with root permissions
adb unroot                   - restarts the adbd daemon without root permissions
adb usb                      - restarts the adbd daemon listening on USB
adb tcpip <port>             - restarts the adbd daemon listening on TCP on the specified port

networking:

adb ppp <tty> [parameters]   - Run PPP over USB.
Note: you should not automatically start a PPP connection.
<tty> 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 [ <directory> ]
<localdir> can be interpreted in several ways:
- If <directory> is not specified, /system, /vendor (if present), /oem (if present) and /data partitions will be updated.
- If it is "system", "vendor", "oem" or "data", only the corresponding partition is updated.

internal debugging:

adb reconnect                  Kick current connection from host side and make it reconnect.
adb reconnect device           Kick current connection from device side and make it reconnect.

environment 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.

其中有一些我們經(jīng)常需要使用的命令,在此重點說明一下:

adb devices

adb devices用于列出當(dāng)前連接到adb server的所有設(shè)備清單寻狂。

c:\>adb devices
List of devices attached
emulator-5554   device

其中 emulator-5554是DeviceID岁经,device則是設(shè)備狀態(tài)。
一般有以下狀態(tài):

offline — 實例未連接到 adb server或不響應(yīng)荆虱。
device — 實例現(xiàn)在已連接到 adb server蒿偎。
no device — 未連接模擬器/設(shè)備。

當(dāng)連接了多個設(shè)備時怀读,我們可以使用-s參數(shù)來指定需要操作的設(shè)備诉位,-s所帶參數(shù)即deviceid。另外如果多個連接的設(shè)備中只有一個通過USB連接的真機菜枷,可以使用-d參數(shù)苍糠,如果只有一個模擬器,可以使用-e參數(shù)(無需帶deviceid)

adb install/uninstall

adb可以方便地向設(shè)備上進行應(yīng)用的安裝和卸載操作啤誊。

adb install [apkpath]      給定apk文件的路徑岳瞭,執(zhí)行即可
adb uninstall packagename  uninstall操作需要給出應(yīng)用的包名

packagename如何獲得? 一般可以通過adb shell pm list packages列出確定蚊锹。
通過adb shell進入/data/data目錄瞳筏,也可以看到所有的包

adb push/pull

adb push/pull則是文件操作
adb push [pc file path] [device file path]-- 將PC端文件上載到設(shè)備端
adb pull [device file path] [pc file path]--將設(shè)備端文件下載到PC端

adb forword

adb forward用于端口轉(zhuǎn)發(fā)∧道ィ可以使用 forward 命令設(shè)置將對特定主機端口的請求轉(zhuǎn)發(fā)到模擬器/設(shè)備實例上的其他端口姚炕。

adb forward tcp:6100 tcp:7100

表示將PC端的6100端口的通信轉(zhuǎn)發(fā)到設(shè)備端的7100端口上

adb kill-server/start-server

當(dāng)adb server進程出現(xiàn)異常時,我們可以會用adb kill-server 命令來停止adb server進程。
adb start-server則用來啟動adb server進程柱宦。但其實執(zhí)行任何adb命令些椒,在server沒有運行時,都會啟動adb server

adb connect/disconnect

當(dāng)我們的設(shè)備在無線wifi環(huán)境下時掸刊,我們可以通過adb connect命令來連接指定ip的設(shè)備免糕。默認設(shè)備端口是5555
adb disconnect則用于斷開已經(jīng)建立的連接
比如genymotion模擬器,就是默認模擬的這種連接方式

adb logcat

adb logcat用于將logcat日志信息輸出到屏幕忧侧。logcat的作用我們在后文再詳細說明

adb shell

adb shell則是可以直接進入設(shè)備并執(zhí)行大量的系統(tǒng)指令石窑。進入adb shell后,即可執(zhí)行很多類linux的系統(tǒng)指令苍柏,如:
操作Activity manager的am命令尼斧,可以直接從命令行啟動一個應(yīng)用的Activity。

adb shell am start -a android.intent.action.VIEW

利用包管理器pm來卸載應(yīng)用

adb shell pm uninstall com.example.MyApp

進行屏幕截圖

adb shell screencap /sdcard/screen.png

錄制操作視頻

adb shell screenrecord /sdcard/demo.mp4

更多用法可參考: 官網(wǎng)說明

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末试吁,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子楼咳,更是在濱河造成了極大的恐慌熄捍,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件母怜,死亡現(xiàn)場離奇詭異余耽,居然都是意外死亡,警方通過查閱死者的電腦和手機苹熏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門碟贾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人轨域,你說我怎么就攤上這事袱耽。” “怎么了干发?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵朱巨,是天一觀的道長。 經(jīng)常有香客問我枉长,道長冀续,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任必峰,我火速辦了婚禮洪唐,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘吼蚁。我一直安慰自己凭需,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著功炮,像睡著了一般溅潜。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上薪伏,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天滚澜,我揣著相機與錄音,去河邊找鬼嫁怀。 笑死设捐,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的塘淑。 我是一名探鬼主播萝招,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼存捺!你這毒婦竟也來了槐沼?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤捌治,失蹤者是張志新(化名)和其女友劉穎岗钩,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體肖油,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡兼吓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了森枪。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片视搏。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖县袱,靈堂內(nèi)的尸體忽然破棺而出浑娜,到底是詐尸還是另有隱情,我是刑警寧澤显拳,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布棚愤,位于F島的核電站,受9級特大地震影響杂数,放射性物質(zhì)發(fā)生泄漏宛畦。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一揍移、第九天 我趴在偏房一處隱蔽的房頂上張望次和。 院中可真熱鬧,春花似錦那伐、人聲如沸踏施。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽畅形。三九已至养距,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間日熬,已是汗流浹背棍厌。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留竖席,地道東北人耘纱。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像毕荐,于是被迫代替她去往敵國和親束析。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容