adb 到底可以帶給我們多少便利?
閱讀文本大概需要 10 分鐘。
什麼是 adb 命令?
adb 工具即 Android Debug Bridge(安卓調試橋) tools。它就是一個命令行窗口,用於通過電腦端與模擬器或者真實設備交互。在某些特殊的情況下進入不了系統,adb 就派上用場啦!
源碼傳送地址:http://androidxref.com/8.0.0_r4/xref/system/core/adb/
分類命令
ADB Debugging
adb devices
主要是用於列印當前連接的所有模擬器或者設備。
adb forward
埠映射,將 PC 端的某埠數據重定向到手機端的一個埠。
adb forward
adb kill-server
終止 adb 進程。
adb kill-server
Wireless
adb connect
無限調試必備命令,需要保證設備和 PC 在同一區域網內,所以可通過遠程桌面達到遠程調試的結果。
adb connect[:
]
需要保證設備的 /system/build.prop 文件中有命令,否則會遭到拒絕。
此處安利一下無限調試設置方法:
打開設備的調試模式,允許 USB 以 MTP 模式傳輸,確保設備和 PC 能正常連接,可以通過或者等進行驗證。
確保已連接後,依次執行以下命令:
在 adb 命令執行的文件夾下的 build.prop 中加入命令
執行後重啟設備
結束後斷開 USB 連接線,輸入adb connect 設備IP:5555確認可以正常連接。
adb usb
設置設備以 USB 形式連接 PC。
Package Manager
adb install
主要用於往 Android 設備 push 應用。
adb install [option]
其中的 option 也是比較有講究的,下面就只說最常用的。
直接安裝應用
替代存在的應用,不會刪除應用數據,用於更新應用特別方便。
其餘的不是很常用的就不多提了,感興趣的可以自行了解。
adb uninstall
從設備或者模擬器卸載應用。
adb uninstall [options]
兩種情況,假設我們的應用 APP 包名是 com.example.application
直接刪除應用和所有數據
刪除應用,但會保留應用數據和緩存數據。
adb shell pm list packages
列印設備下面的所有應用包名。
adb shell pm list packages [options]
其他的過濾方式和限定條件這裡也不舉例了。
adb shell pm path
列印 apk 的路徑。
adb shell pm path
adb shell pm clear
清除應用緩存。
adb shell pm clear
File Manager
adb pull
從 Android 設備下載文件到 PC。
adb pull[local]
其中代表文件在設備中的地址,[local] 代表存放目錄。
adb push
把 PC 的文件存放到 Android 設備。
adb push
adb shell ls
列出目錄內容。
adb shell ls [options]
adb shell cd
和一般的 PC 的差不多,主要用於切換目錄。
adb shell cd
adb shell rm
刪除文件或者目錄
adb shell rm [options]
adb shell mkdir
創建一個文件夾
adb shell mkdir [options]
adb shell touch
創建一個新文件或者改變文件修改時間
adb shell touch [options]
adb shell pwd
定位當前的操作位置
adb shell pwd
adb shell cp
字面意思,很好理解,複製。
adb shell cp [options]
adb shell mv
移動或者更名文件
adb shell mv [options]
Network
adb shell netstat
主要用於網路統計。
adb shell ping
沒啥好說的,和 PC 的 ping 命令一樣的。
adb shell netcfg
通過配置文件配置和管理網路連接。
adb shell netcfg []
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
filter to only show Debug level
filter to only show Info level
filter to only show Warning level
filter to only show Error level
filter to only show Fatal level
Silent, highest priority, on which nothing is ever printed
adb logcat -b
View the buffer that contains radio/telephony related messages.
View the buffer containing events-related messages.
default
Clears the entire log and exits.
Dumps the log to the screen and exits.
Writes log message output to test.logs .
Prints the size of the specified log buffer and exits.
*Sets the maximum number of rotated logs to. *
adb shell dumpsys
獲取系統數據。
adb shell dumpsys [options]
其中有個非常好用的是,當你在新接觸一個項目的時候,不熟悉項目流程,此時正好可以用這個直接定位到你的 Activity 位置。
如圖,直接在列印出來內容的後半段找到了當前 Activity 的定位,是。
adb shell dumpstate
和命令直譯差不多,dumps state。
Screenshot
adb shell screencap
一般的手機都有截圖功能(一般下拉菜單中有),但不代表所有 Android 設備都在可視化中開啟了這個功能,所以這時候這個 adb 命令就顯得特別重要。
adb shell screencap
結合前面的 pull 命令,就可以讓我們輕鬆拿到屏幕截圖。
截圖存放
取到 PC 當前文件夾
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
設置服務屬性。
—————END—————
我是南塵,只做比心的公眾號,歡迎關注我。
※當 RecyclerView讓Google 工程師背了鍋
※我為什麼突然想轉管理?
TAG:nanchen |