什麼是 adb 指令?
adb 工具即 Android Debug Bridge(安卓調試橋) tools。它就是一個指令行視窗,用于通過電腦端與模拟器或者真實裝置互動。在某些特殊的情況下進入不了系統,adb 就派上用場啦!
源碼傳送位址分類指令
ADB Debugging
-
adb devices
主要是用于列印目前連接配接的所有模拟器或者裝置。
-
adb forward
端口映射,将 PC 端的某端口資料重定向到手機端的一個端口。
adb forward <local> <remote>
-
adb kill-server
終止 adb 程序。
Wireless
-
adb connect
無限調試必備指令,需要保證裝置和 PC 在同一區域網路内,是以可通過遠端桌面達到遠端調試的結果。
adb connect <host>[:<port>]
需要保證裝置的 /system/build.prop 檔案中有指令
service.adb.tcp.port=5555
,否則會遭到拒絕。
此處安利一下無限調試設定方法:
- 打開裝置的調試模式,允許 USB 以 MTP 模式傳輸,確定裝置和 PC 能正常連接配接,可以通過
或者
adb shell
等進行驗證。
adb devices
- 確定已連接配接後,依次執行以下指令:
adb root
adb remount
adb pull /system/build.prop ./
- 在 adb 指令執行的檔案夾下的 build.prop 中加入指令
service.adb.tcp.port=5555
- 執行
adb push ./build.prop /system/
後重新開機裝置
結束後斷開 USB 連接配接線,輸入 adb connect 裝置IP:5555 确認可以正常連接配接。
-
adb usb
設定裝置以 USB 形式連接配接 PC。
Package Manager
-
adb install
主要用于往 Android 裝置 push 應用。
adb install [option] <path>
其中的 option 也是比較有講究的,下面就隻說最常用的。
直接安裝應用
adb install test.apk
替代存在的應用,不會删除應用資料,用于更新應用特别友善。
adb install -r test.apk
其餘的不是很常用的就不多提了,感興趣的可以自行了解。
-
adb uninstall
從裝置或者模拟器解除安裝應用。
adb uninstall [options] <package>
兩種情況,假設我們的應用 APP 包名是 com.example.application
直接删除應用和所有資料
adb uninstall com.example.application
删除應用,但會保留應用資料和緩存資料。
adb uninstall -k com.example.application
-
adb shell pm list packages
列印裝置下面的所有應用包名。
adb shell pm list packages [options] <FiLTER>
其他的過濾方式和限定條件這裡也不舉例了。
-
adb shell pm path
列印 apk 的路徑。
adb shell pm path <package>
-
adb shell pm clear
清除應用緩存。
adb shell pm clear <package>
File Manager
-
adb pull
從 Android 裝置下載下傳檔案到 PC。
adb pull <remote> [local]
其中 <remote> 代表檔案在裝置中的位址,[local] 代表存放目錄。
-
adb push
把 PC 的檔案存放到 Android 裝置。
adb push <local> <remote>
-
adb shell ls
列出目錄内容。
adb shell ls [options] <directory>
-
adb shell cd
和一般的 PC 的
差不多,主要用于切換目錄。cd
adb shell cd <directory>
-
adb shell rm
删除檔案或者目錄
adb shell rm [options] <file or directory>
-
adb shell mkdir
建立一個檔案夾
adb shell mkdir [options] <directory name>
-
adb shell touch
建立一個新檔案或者改變檔案修改時間
adb shell touch [options] <file>
-
adb shell pwd
定位目前的操作位置
-
adb shell cp
字面意思,很好了解,複制。
adb shell cp [options] <source> <dest>
-
adb shell mv
移動或者更名檔案
adb shell mv [options] <source> <dest>
Network
-
adb shell netstat
主要用于網絡統計。
-
adb shell ping
沒啥好說的,和 PC 的 ping 指令一樣的。
-
adb shell netcfg
通過配置檔案配置和管理網絡連接配接。
adb shell netcfg [<interface> {dhcp|up|down}]
-
adb shell ip
主要用于顯示一些資料
adb shell ip [OPTIONS] OBJECT
Logcat
-
adb logcat
列印日志檔案。
adb logcat [options] [filter-specs]
當然可以像 Android Studio 一樣隻列印固定的日志
lowest priority, filter to only show Verbose level
adb logcat *:V
filter to only show Debug level
adb logcat *:D
filter to only show Info level
adb logcat *:I
filter to only show Warning level
adb logcat *:W
filter to only show Error level
adb logcat *:E
filter to only show Fatal level
adb logcat *:F
Silent, highest priority, on which nothing is ever printed
adb logcat *:S
adb logcat -b <Buffer>
View the buffer that contains radio/telephony related messages.
adb logcat -b radio
View the buffer containing events-related messages.
adb logcat -b event
default
adb logcat -b main
Clears the entire log and exits.
adb logcat -c
Dumps the log to the screen and exits.
adb logcat -d
Writes log message output to test.logs .
adb logcat -f test.logs
Prints the size of the specified log buffer and exits.
adb logcat -g
*Sets the maximum number of rotated logs to <count>. *
adb logcat -n <count>
-
adb shell dumpsys
擷取系統資料。
adb shell dumpsys [options]
其中有個非常好用的是,當你在新接觸一個項目的時候,不熟悉項目流程,此時正好可以用這個直接定位到你的 Activity 位置。
adb shell dumpsys activity activities
如圖,直接在列印出來内容的後半段找到了目前 Activity 的定位,是
NewLoginActivity
。
-
adb shell dumpstate
和指令直譯差不多,dumps state。
Screenshot
-
adb shell screencap
一般的手機都有截圖功能(一般下拉菜單中有),但不代表所有 Android 裝置都在可視化中開啟了這個功能,是以這時候這個 adb 指令就顯得特别重要。
adb shell screencap <filename>
結合前面的 pull 指令,就可以讓我們輕松拿到螢幕截圖。
截圖存放
adb shell screencap /sdcard/test.png
取到 PC 目前檔案夾
adb pull /sdcard/test.png
-
adb shell screencord
有了螢幕截圖,自然也得有螢幕錄制,可惜這個必須在 Android 4.4 (API level 19) 以上才可使用。
adb shell screencord /sdcard/test.mp4
這個還可以對大小 size 和 時間做限制,感興趣的可以自行了解。
System
-
adb root
擷取 root 權限。
- adb sideload
-
adb shell ps
列印程序狀态。
-
adb shell top
展現上層 CPU 程序資訊。
-
adb shell getprop
擷取 Android 系統服務屬性
-
adb shell setprop
設定服務屬性。