[譯]一些常用的Android ADB命令
在網上看到的一篇介紹常用ADB命令的文章,翻譯過來和大家分享一下。原文地址:Handy adb commands for Android
下面是一些我找到Android的ADB有用的命令,可以手動在命令行使用,也可以在自動化打包和測試中使用。
一、查看已連接的設備
使用下面的命令查看所有連接的設備,同時列出它們的IDs:
adb devices
如果連接了多個設備,可以使用 adb -s DEVICE_ID 單獨查看某個設備的信息。
二、安裝APP
使用 install 命令來安裝一個APP,使用命令行參數 -r 來重新安裝APP,同時會保留這個APP之前在手機上的數據。例子如下:
adb install -r APK_FILE # example adb install -r ~/application.apk
三、卸載APP
比較簡單,不過做介紹,命令如下:
adb uninstall PACKAGE_NAME # example adb uninstall com.growingwiththeweb.example
四、打開應用界面(Activity)
打開應用中的某個Activity:
adb shell am start PACKAGE_NAME/ACTIVITY_IN_PACKAGE adb shell am start PACKAGE_NAME/FULLY_QUALIFIED_ACTIVITY # example adb shell am start -n com.growingwiththeweb.example/.MainActivity adb shell am start -n com.growingwiththeweb.example/com.growingwiththeweb.example.MainActivity
五、進入設備命令行
adb shell
六、截屏
這個截屏的方法是 Sergei Shvetsov 想出來的:使用 shell screencap 截圖,然后使用Perl腳本保存到本地。詳細介紹可以查看他的 博客
adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen.png
七、電源開關
這個命令會發送電源按鈕事件,打開或關閉某個設備:
adb shell input keyevent 26
八、解鎖屏幕
這個命令可以向設備發送 解鎖或鎖屏 的事件,它可以和上面的電源命令一起使用:打開并解鎖設備:
adb shell input keyevent 82
九、打印所有安裝的APP
adb shell pm list packages -f
十、清除APP數據
adb shell pm clear PACKAGE_NAME # example adb shell pm clear com.growingwiththeweb.example
十一、Logging
1. 在命令行展示Log信息:
adb logcat
2. 使用Tag Name過濾Log信息:
adb logcat -s TAG_NAME adb logcat -s TAG_NAME_1 TAG_NAME_2 #example adb logcat -s TEST adb logcat -s TEST MYAPP
3. 使用Log級別過濾Log信息:
adb logcat "*:PRIORITY" # example adb logcat "*:W"
以下是常用的Log級別:
V - Verbose (最低)
D - Debug
I - Info
W - Warning
E - Error
F - Fatal
S - Silent (最高, 使用這個級別將什么也不會打印)
4. 使用Tag和級別同時過濾Log:
adb logcat -s TAG_NAME:PRIORITY adb logcat -s TAG_NAME_1:PRIORITY TAG_NAME_2:PRIORITY #example adb logcat -s TEST: W
5. 使用 grep 命令過濾Log:
adb logcat | grep "SEARCH_TERM" adb logcat | grep "SEARCH_TERM_1\|SEARCH_TERM_2" #example adb logcat | grep "Exception" adb logcat | grep "Exception\|Error"
6. 清除 logcat 信息:
adb logcat -c
十二、更多介紹
更多的ADB命令介紹請看Android官方文檔:official adb reference site
Over!
來自:http://xianglong.me/article/handy-adb-commands-for-Android/