ADB常用指令及使用場(chǎng)景

adb簡(jiǎn)介
大家好,我是賣女孩的小火柴~
adb全稱Android Debug Bridge(安卓調(diào)試橋)點(diǎn)擊下載adb?, 用作PC端連接到移動(dòng)端 , 方便日常的開發(fā)和調(diào)試.
adb功能十分強(qiáng)大 , 本文僅針對(duì)EasyClick自動(dòng)化測(cè)試工具 , 列出了一部分常用指令?查看視頻講解
配置環(huán)境變量
設(shè)置adb變量值
我的電腦 -> 右擊屬性 -> 高級(jí)系統(tǒng)設(shè)置 -> 系統(tǒng)變量 -> 新建 -> 變量名 -> 變量值 -> 完成步驟
設(shè)置path變量值
選中path -> 編輯 -> 新建 -> %變量名% -> 確定 -> 全部完成
adb常用指令
adb start-server
啟動(dòng)ADB服務(wù)
adb kill-server
關(guān)閉ADB服務(wù)
adb devices
列出所有設(shè)備
adb connect 192.168.100.1:5555
connect 表示連接到設(shè)備
192.168.100.1:5555 表示設(shè)備地址(address)和端口號(hào)(dport)adb disconnect
斷開所有連接
adb reboot
重啟設(shè)備
adb reboot recovery
重啟設(shè)備到Recovery
adb reboot bootloader
也就是常說(shuō)的BL鎖
adb shell wm size
size 表示獲取當(dāng)前設(shè)備屏幕的分辨率
adb shell wm size 1080x1920
size 1080x1920 表示設(shè)定屏幕分辨率為1080x1920
注意 : 1080x1920中的x,是字母x(艾克斯)adb shell wm size reset
size reset 表示重置當(dāng)前設(shè)備的屏幕分辨率恢復(fù)到默認(rèn)
adb shell wm density
wm density 表示獲取當(dāng)前屏幕密度
adb shell wm density 300
wm density 300 表示設(shè)置當(dāng)前設(shè)備屏幕密度為300
adb shell wm density reset
wm density reset 表示重置當(dāng)前設(shè)備的屏幕密度恢復(fù)到默認(rèn)
adb shell
進(jìn)入shell模式
exit
退出shell模式
su
切換到Root用戶
adb -s emulator-5554 shell
-s 表示指定某個(gè)設(shè)備
emulator-5554 表示設(shè)備名稱ls
列出當(dāng)前目錄下的所有可見文件及文件夾
ls -a
列出當(dāng)前目錄下的所有文件和文件夾(包括隱藏的文件/文件夾,其中-a表示的意義是all,所有全部)
ls -l dir
列出指定目錄的文件/文件夾屬性
clear
清空歷史記錄
cd dir
進(jìn)入到指定目錄
cd ..或者 cd ../../..
返回上一級(jí)目錄或者多級(jí)目錄
cd a;cd b;cd c;cd d
同時(shí)執(zhí)行多個(gè)命令,用英文分號(hào)";"隔開
adb install -g -r .\easyclick.apk
install 表示安裝
-g 表示賦予所有權(quán)限
-r 表示覆蓋安裝
.\wechat.apk 表示需要安裝的apk路徑支持相對(duì)和絕對(duì)路徑adb shell pm list package -3
pm 表示Package Manager(包管理器)
list 表示列表,列出
package 表示包
-3 表示第三方應(yīng)用(即用戶自行安裝的應(yīng)用)
-s 表示系統(tǒng)應(yīng)用
-3可省,列出設(shè)備所有包名adb shell pm path com.gibb.easyclick
pm 表示Package Manager(包管理器)
path 表示安裝路徑
com.gibb.easyclick 表示包名,此指令的執(zhí)行結(jié)果,即顯示出微信安裝包所在的位置路徑adb shell pm clear com.gibb.easyclick
pm 表示Package Manager(包管理器)
clear 表示清空
com.gibb.easyclick 表示包名,此指令的執(zhí)行結(jié)果,即清空微信所有數(shù)據(jù),恢復(fù)到第一次安裝的狀態(tài)
此指令等同于清空應(yīng)用詳情界面中的存儲(chǔ)(Storage)-數(shù)據(jù)(Data)adb shell dumpsys window
dumpsys window 表示將當(dāng)前界面轉(zhuǎn)存成靜態(tài)文件
adb shell dumpsys window|findStr mCurrentFocus
dumpsys window 表示將當(dāng)前界面轉(zhuǎn)存成靜態(tài)文件
findStr mCurrentFocus 表示查找屬性
獲取當(dāng)前界面的PackageName(包名)和ActivityName(活動(dòng)名)adb shell pm uninstall com.gibb.easyclick
pm 表示Package Manager(包管理器)
uninstall 表示反向安裝,即卸載
com.gibb.easyclick 表示需要卸載的包名,此指令的執(zhí)行結(jié)果,即卸載微信adb shell pm uninstall --user 0 com.gibb.easyclick
pm 表示Package Manager(包管理器)
uninstall 表示反向安裝,即卸載
--user 0 表示管理員權(quán)限
com.gibb.easyclick 表示需要卸載的包名
此指令可以卸載大部分系統(tǒng)自帶的應(yīng)用,適用于強(qiáng)迫癥,極端完美主義玩家.需要謹(jǐn)慎使用,卸載后盡量不要關(guān)機(jī)或者重啟設(shè)備,否則可能會(huì)出現(xiàn)無(wú)法開機(jī)的情況.或者開機(jī)時(shí)間很長(zhǎng)很長(zhǎng)(其實(shí)就是系統(tǒng)發(fā)現(xiàn)缺少了自帶應(yīng)用,努力恢復(fù)的一個(gè)過(guò)程),有變磚的可能性adb shell am force-stop com.gibb.easyclick
am 表示Activity Manager(活動(dòng)管理器)
force-stop 表示強(qiáng)制停止
com.gibb.easyclick 表示需要強(qiáng)制停止的包名
此指令等同于應(yīng)用詳情界面中的強(qiáng)制停止adb shell monkey -p com.gibb.easyclick 1
monkey 表示壓力測(cè)試
-p 表示指定包名
com.gibb.easyclick 表示需要測(cè)試的包名
1 表示時(shí)間,單位毫秒
大部分情況用來(lái)啟動(dòng)某個(gè)已知包名的應(yīng)用,參考來(lái)源于Stack Overflowadb shell mkdir test
mkdir 表示創(chuàng)建文件夾
test 表示創(chuàng)建新文件夾的名稱adb shell touch test.txt
touch 表示創(chuàng)建新文件,替代方案cat >test.txt
test.txt 表示創(chuàng)建新文件的名稱adb shell cat test.txt
cat 表示打開文件
test.txt 表示要打開的文件adb shell cat >test.txt
創(chuàng)建一個(gè)test.txt的文件,并輸入內(nèi)容,ctrl+d退出編輯,enter結(jié)束操作如果test.txt存在,之前的內(nèi)容會(huì)被覆蓋
adb shell cat >>test.txt
編輯test.txt文件,追加內(nèi)容,ctrl+d退出編輯,enter結(jié)束操作如果test.txt不存在,會(huì)自動(dòng)創(chuàng)建
ctrl+d
退出編輯模式
adb shell cp /sdcard/1.txt /sdcard/Pictures/1.txt
cp 表示復(fù)制
/sdcard/1.txt 表示原路徑位置
/sdcard/Pictures/1.txt 表示移動(dòng)后的路徑位置,支持重命名adb shell mv /sdcard/1.txt /sdcard/Pictures/1.txt
mv 表示移動(dòng)
/sdcard/1.txt 表示原路徑位置
/sdcard/Pictures/1.txt 表示移動(dòng)后的路徑位置,支持重命名adb shell rename oldf.txt newf.txt
rename 表示重命名
oldf.txt 表示原名稱
newf.txt 表示修改后的名稱,重命名adb shell rm test.txt
rm 表示刪除文件
test.txt 表示需要?jiǎng)h除的文件adb shell rmdir test
rmdir 表示刪除文件夾
test 表示需要?jiǎng)h除的文件夾
需要注意的是,當(dāng)文件夾test中包含其他文件或文件夾的時(shí)候,會(huì)刪除失敗adb shell rm -r test
rm 表示刪除
-r 表示遞歸到其子目錄
test 表示需要?jiǎng)h除的文件夾
該方法會(huì)刪除整個(gè)test文件夾,包括其子目錄中,請(qǐng)謹(jǐn)慎操作adb shell find -name uiauto*
find 表示查找匹配
-name 表示名稱區(qū)分大小寫
uiauto* 表示匹配規(guī)則,其中*表示匹配任意數(shù)量的任意字符adb shell find -iname pictures
find 表示查找匹配
-iname 表示不區(qū)分大小寫
pictures 表示需要查找的名稱adb shell find -name [ASP]ictures
find 表示查找匹配
-name 表示名稱區(qū)分大小寫
[ASP]ictures 表示匹配規(guī)則,會(huì)匹配Aictures和Sictures還有Picturesadb shell input keyevent 3
input 表示輸入模式
keyevent 表示模擬按鍵
3 表示三大金剛中的主頁(yè)Home,對(duì)應(yīng)KeyCode碼表adb shell input keyevent 4
input 表示輸入模式
keyevent 表示模擬按鍵
4 表示三大金剛中的返回Back,對(duì)應(yīng)KeyCode碼表adb shell input keyevent 26
input 表示輸入模式
keyevent 表示模擬按鍵
26 表示電源Power,對(duì)應(yīng)KeyCode碼表adb shell input keyevent 82
input 表示輸入模式
keyevent 表示模擬按鍵
82 表示三大金剛中的菜單Menu,對(duì)應(yīng)KeyCode碼表adb shell input text 1234qwer
input 表示輸入模式
text 表示輸入字符/字符串
1234qwer 表示需要輸入的字符/字符串
注意 : 因?yàn)榫幋a的原因,故不支持輸入中文adb shell ime擴(kuò)展一下(對(duì)于編碼和極端輸入有用)
主要關(guān)注list和set,enable和disable僅作了解就行
ADB編碼問(wèn)題解決方案
方案參考:查看ADBKeyboard
adb shell am broadcast -a ADB_INPUT_TEXT --es msg '你好世界'
ADBKeyboard解決輸入中文的編碼問(wèn)題
adb shell am broadcast -a ADB_CLEAR_TEXT
清空輸入的文本
adb shell input tap x y
input 表示輸入模式
tap 表示點(diǎn)擊
x 表示需要點(diǎn)擊的位置橫坐標(biāo)
y 表示需要點(diǎn)擊的位置縱坐標(biāo)adb shell input swipe x1 y1 x2 y2
input 表示輸入模式
swipe 表示滑動(dòng)
x1 表示起始位置的橫坐標(biāo)
y1 表示起始位置的縱坐標(biāo)
x2 表示終點(diǎn)位置的橫坐標(biāo)
y2 表示重點(diǎn)位置的縱坐標(biāo)adb shell screencap /sdcard/Pictures/now.png
screencap 表示截圖
/sdcard/Pictures/now.png 表示截圖保存的位置adb shell ps
ps 表示列出當(dāng)前所有任務(wù)(包括name和pid)
adb shell kill pid
kill 表示殺死進(jìn)程/服務(wù)
pid 表示任務(wù)的pid屬性adb shell /system/bin/uiautomator dump /sdcard/now.xml
/system/bin/uiautomator 表示執(zhí)行uiautomator
dump 表示將當(dāng)前界面轉(zhuǎn)存為靜態(tài)文件
/sdcard/now.xml 轉(zhuǎn)存位置
/system/bin/目錄下為指令集合,亦可簡(jiǎn)寫為adb shell uiautomator dump /sdcard/now.xmladb push .\1.txt /sdcard/1.txt
push 表示將PC端的指定文件推送到移動(dòng)端
.\1.txt 表示文件在PC端的位置,一般都是用相對(duì)路徑
/sdcard/1.txt 表示文件推送到移動(dòng)端保存的位置
多個(gè)設(shè)備同時(shí)在線需要指定設(shè)備名稱,如adb -s emulator-5554 push .\1.txt /sdcard/1.txtadb pull /sdcard/1.txt .\1.txt
pull 表示拉取移動(dòng)端文件到PC端
/sdcard/1.txt 表示文件在移動(dòng)端的位置
.\1.txt 表示拉取到PC端保存的位置
push和pull對(duì)應(yīng),記憶方法:pull中有兩個(gè)L,可以聯(lián)想到漢語(yǔ)中l(wèi)a,相反另外一個(gè)則為推送adb remount
remount 表示掛載為可讀寫
常用于Permission Denied(權(quán)限不足)的問(wèn)題,優(yōu)先考慮掛載,掛載失敗再考慮chmod提權(quán)adb shell chmod -R 777 path
chmod 表示提權(quán)
-R 表示提權(quán)遞歸到該目錄的子目錄,注意大寫
777 表示權(quán)限級(jí)別
path 表示需要提權(quán)的目錄設(shè)備代理模式激活方式1
推送設(shè)備對(duì)應(yīng)的二進(jìn)制文件,adb push newagent-x86 /data/local/tmp/agent
cd到tmp,chmod -R 777 agent//提權(quán)
執(zhí)行adb shell /data/local/tmp/agent -mode=runagent -dport=19901,19902,19903 --password=123設(shè)備代理模式激活方式2
路徑提權(quán),adb remount//remount succeeded
推送二進(jìn)制文件到system目錄?adb push newagent-x86 /system/bin/agent//直接推送會(huì)報(bào)錯(cuò),但并不影響激活效果
執(zhí)行二進(jìn)制文件?adb shell /system/bin/agent -mode=runagent -dport=19901,19902,19903 --password=123設(shè)備代理模式激活方式3
exe程序批量激活
激活結(jié)果驗(yàn)證
查詢指定端口號(hào)19901,19902,19903是否存在通訊?adb shell netstat -ant
Telegram : @Runtime7
QQ : 1446106355