前面的內(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)說明