移動(dòng)端的Android測(cè)試總是少不了一些adb命令的操作盯蝴,稍微總結(jié)一下自己經(jīng)常用到的命令夹孔。
例如:adb無(wú)法安裝debug包墨榄,提示Failure [INSTALL_FAILED_TEST_ONLY]锹雏,怎么辦瞳步?莫慌击儡,加個(gè)-t參數(shù)即可
$ adb install -t debug.apk
這一點(diǎn)在adb的說(shuō)明中有明確的提示, -t 即表示允許test package
$ adb
......
app installation:
install [-lrtsdg] PACKAGE
install-multiple [-lrtsdpg] PACKAGE...
push package(s) to the device and install them
-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 (install-multiple only)
-g: grant all runtime permissions
設(shè)備信息相關(guān)
1塔沃、設(shè)備名
$ adb devices
List of devices attached
* daemon not running. starting it now at tcp:5037 *
* daemon started successfully *
b33aa57c device
如果連接的是模擬器的話,需要先connect一下阳谍,例如:
夜神模擬器連接adb:adb connect 127.0.0.1:62001
海馬玩模擬器連接adb:adb connect 127.0.0.1:53001
2蛀柴、獲取設(shè)備的一些信息:$ adb shell cat /system/build.prop
# 例如
$ adb shell cat /system/build.prop |grep ro.product.model
ro.product.model=MI 5
$ adb shell cat /system/build.prop |grep ro.build.version.release
ro.build.version.release=6.0.1
$ adb shell cat /system/build.prop |grep ro.product.cpu.abi
# ro.product.cpu.abi and ro.product.cpu.abi2 are obsolete,
# use ro.product.cpu.abilist instead.
ro.product.cpu.abi=arm64-v8a
ro.product.cpu.abilist=arm64-v8a,armeabi-v7a,armeabi
ro.product.cpu.abilist32=armeabi-v7a,armeabi
ro.product.cpu.abilist64=arm64-v8a
常用方法
1、獲取當(dāng)前最上層activity
$ adb shell dumpsys activity | grep "mFocusedActivity"
mFocusedActivity: ActivityRecord{5bef6d8 u0 com.miui.home/.launcher.Launcher t1803}
2矫夯、通過(guò)adb獲取設(shè)備IMEI:iphonesubinfo
$ adb shell service call iphonesubinfo 1 | awk -F "'" '{print $2}' | sed '1 d' | tr -d '.' | awk '{print}' ORS=
872429031076264
3鸽疾、自動(dòng)化中可能應(yīng)用到輸入法設(shè)置
# 列出當(dāng)前安裝的輸入法
$ adb shell ime list
# 當(dāng)前默認(rèn)的輸入法
$ adb shell settings get secure default_input_method
# 設(shè)置輸入法
$ adb shell ime set com.google.android.inputmethod.pinyin/.PinyinIME
4、截圖训貌、截視頻
# 截圖
$ adb shell /system/bin/screencap -p /sdcard/screenshot.png
# 錄制是視頻
$ adb shell screenrecord /sdcard/demo.mp4
5制肮、獲取分辨率
$ adb shell wm size
Physical size: 1080x1920
$ adb shell dumpsys window displays |head -n 3
WINDOW MANAGER DISPLAY CONTENTS (dumpsys window displays)
Display: mDisplayId=0
init=1080x1920 480dpi cur=1080x1920 app=1080x1920 rng=1080x1020-1920x1860
6、獲取幀數(shù)據(jù)递沪,這部分的詳細(xì)內(nèi)容可以看我的另一篇分析:
Android FPS流暢度測(cè)試
$ adb shell dumpsys gfxinfo PackageName
$ adb shell dumpsys gfxinfo PackageName framestats
7豺鼻、簡(jiǎn)單的廣播操作:
廣播 - 改變wifi狀態(tài)
$ adb shell am broadcast -a android.net.wifi.WIFI_STATE_CHANGED -ei disable
廣播 - 改變電量,level代表電量
$ adb shell am broadcast -a android.intent.action.BATTERY_CHANGED --ei plugged 0 --ei temperature 2 --ei status 3 --ei level 4
廣播 - 刷新media庫(kù)單個(gè)文件款慨。有時(shí)候我們push圖片等到設(shè)備中不會(huì)立即被識(shí)別儒飒,這時(shí)候需要手動(dòng)刷新一下
$ adb shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:///storage/emulated/0/DCIM/Camera/yourpic.jpg
eg:....SCANNER_SCAN_FILE -d file:///storage/emulated/0/DCIM/Camera/yourpic.jpg
廣播 - 刷新media庫(kù)文件夾。有時(shí)候我們push圖片等到設(shè)備中不會(huì)立即被識(shí)別檩奠,這時(shí)候需要手動(dòng)刷新一下
$ adb shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d yourPath
eg:....SCANNER_SCAN_FILE -d file:///storage/emulated/0/DCIM/Camera/
8约素、有的MIUI開(kāi)發(fā)板獲得完整root權(quán)限。
這個(gè)至少要在Android 6.0以上笆凌,較新adb版本圣猎。背景是,MIUI開(kāi)發(fā)板已經(jīng)獲得了root權(quán)限乞而,但還是不能對(duì)system目錄進(jìn)行讀寫(xiě)送悔。這時(shí)候可以試試security中的disable-verity,作用是disable dm-verity checking on userdebug builds爪模。
$ adb root
$ adb disable-verity
$ adb reboot
# 重新開(kāi)啟也很簡(jiǎn)單
$ adb enable-verity
9欠啤、APK相關(guān)
# 安裝、卸載
$ adb install PackageName
$ adb uninstall PackageName
# 查看安裝包信息
$ aapt dump badging PackageName
# 強(qiáng)制停止某個(gè)應(yīng)用
$ adb shell am force-top PackageName
# 清除某個(gè)應(yīng)用的數(shù)據(jù)(相當(dāng)于新安裝)
$ adb shell pm clear PackageName
# 啟動(dòng)應(yīng)用
$ adb shell am start -D -n packageName/activity
10屋灌、動(dòng)態(tài)調(diào)試需要用到forward
# 使用IDA進(jìn)行動(dòng)態(tài)調(diào)試洁段,需要端口轉(zhuǎn)發(fā)
$ adb forward tcp:23946 tcp:23946
# smali動(dòng)態(tài)調(diào)試
$ adb forward tcp:8800 jdwp:21683
11、改變手機(jī)網(wǎng)絡(luò)狀態(tài):adb shell svc
$ adb shell svc
Available commands:
help Show information about the subcommands
power Control the power manager
data Control mobile data connectivity
wifi Control the Wi-Fi manager
usb Control Usb state
nfc Control NFC functions
這里用wifi作為例子:其他的書(shū)輸入help即可查看
$ adb shell svc wifi help
Control the Wi-Fi manager
usage: svc wifi [enable|disable]
Turn Wi-Fi on or off.
# 使用方法
$ adb root
$ adb shell svc wifi disable/enable
12共郭、跑跑monkey
#示例
$ adb shell monkey -p PackageName --throttle 600 --pct-trackball 44 --pct-touch 44 --pct-appswitch 2 --pct-motion 10 -v 1000
adb的命令還有很多祠丝,光是dumpsys疾呻、am和pm就有很多可講的。它們都是有具體的應(yīng)用場(chǎng)景写半,后面會(huì)在單獨(dú)的文章中進(jìn)行介紹岸蜗。
官網(wǎng)的介紹:
https://developer.android.com/studio/command-line/adb.html