ADB使PC可以通過USB或網路與android設備通訊。以下為Debug&測試時常用的ADB指令。 ### 使ADB有Root權限 `adb root` 進入裝置之shell有以下方式,適用於連接多台裝置 1. 若只有連接一台裝置 `adb shell` 2. 連接多台裝置,先確認目標裝置名稱/IP `adb devices` `adb -s "device name" shell` e.g. ``` adb devices List of devices attached 172.18.66.167:5555 device 172.18.66.187:5555 device ``` 進入第一台裝置之shell ``` adb -s 172.18.66.167:5555 shell ``` 4. 裝置transpot_id `adb device -l` `adb -t transpot_id shell` 5. 關閉分區檢測功能 `adb disable-verity` 6. 使system分區為可讀可寫模式 `adb remount` 7. 清空log `adb logcat -c` 開始錄製日誌,並輸入到 A.log下,位置是在adb目錄下的data文件夾內 `adb logcat -v time > /data/A.log` 8. 將檔案複製到裝置 `adb push 電腦的檔案路徑 手機的檔案路徑` 9. 將檔案複製到電腦 `adb pull 手機的檔案路徑 電腦的檔案路徑` e.g. `adb push logcat.log /data/logcat.log` 下完cmd後開始進行實驗 Ctrl+C =>停止錄製,此時查看 A.log文件,有相關log輸出 10. 截圖並儲存,以下3種指令皆適用 `adb shell rm /sdcard/Pictures/1.jpg` `adb shell screencap -p /sdcard/Pictures/1.png` `adb -s emulator-5554 shell screencap -p /sdcard/Pictures/1.png` 11. 模擬按鍵 方法一:`adb shell input keyevent KEYCODE_BACK` 方法二:`adb shell input keyevent 4` 備註: 1. logcat是針對Device端的,所以和在哪一個PC端(win/mac)錄製log無關。 2. Device端連接PC端時,一定要開啟開發者模式下的USB調試模式 ※Device端為Android Phone or Android TV 重啟adb server `adb kill-server` `adb start-server` 使用ADB安裝apk file `adb install path_to_apk` 如需要關閉SELinux可以下(reboot後須重下一次) `adb shell "setenforce 0"` Reference: https://developer.android.com/studio/command-line/adb http://www.temblast.com/ref/akeyscode.htm https://wiki.mediatek.inc/pages/viewpage.action?pageId=777749719 https://www.cnblogs.com/guo2733/p/10584891.html