什么是 adb 命令?
adb 工具即 Android Debug Bridge(安卓調(diào)試橋) tools。它就是一個(gè)命令行窗口,用于通過電腦端與模擬器或者真實(shí)設(shè)備交互。在某些特殊的情況下進(jìn)入不了系統(tǒng),adb 就派上用場(chǎng)啦!
分類命令
ADB Debugging
-
adb devices
主要是用于打印當(dāng)前連接的所有模擬器或者設(shè)備。
adb forward
端口映射,將 PC 端的某端口數(shù)據(jù)重定向到手機(jī)端的一個(gè)端口。
adb forward <local> <remote>
- adb kill-server
終止 adb 進(jìn)程。
adb kill-server
Wireless
- adb connect
無限調(diào)試必備命令,需要保證設(shè)備和 PC 在同一局域網(wǎng)內(nèi),所以可通過遠(yuǎn)程桌面達(dá)到遠(yuǎn)程調(diào)試的結(jié)果。
adb connect <host>[:<port>]
需要保證設(shè)備的 /system/build.prop 文件中有命令 service.adb.tcp.port=5555,否則會(huì)遭到拒絕。

此處安利一下無限調(diào)試設(shè)置方法:
- 打開設(shè)備的調(diào)試模式,允許 USB 以 MTP 模式傳輸,確保設(shè)備和 PC 能正常連接,可以通過
adb shell或者adb devices等進(jìn)行驗(yàn)證。- 確保已連接后,依次執(zhí)行以下命令:
adb root
adb remount
adb pull /system/build.prop ./- 在 adb 命令執(zhí)行的文件夾下的 build.prop 中加入命令
service.adb.tcp.port=5555- 執(zhí)行
adb push ./build.prop /system/后重啟設(shè)備
結(jié)束后斷開 USB 連接線,輸入 adb connect 設(shè)備IP:5555 確認(rèn)可以正常連接。
- adb usb
設(shè)置設(shè)備以 USB 形式連接 PC。
Package Manager
- adb install
主要用于往 Android 設(shè)備 push 應(yīng)用。
adb install [option] <path>
其中的 option 也是比較有講究的,下面就只說最常用的。
adb install test.apk直接安裝應(yīng)用adb install -r test.apk替代存在的應(yīng)用,不會(huì)刪除應(yīng)用數(shù)據(jù),用于更新應(yīng)用特別方便。
其余的不是很常用的就不多提了,感興趣的可以自行了解。
- adb uninstall
從設(shè)備或者模擬器卸載應(yīng)用。
adb uninstall [options] <package>
兩種情況,假設(shè)我們的應(yīng)用 APP 包名是 com.example.application
adb uninstall com.example.application直接刪除應(yīng)用和所有數(shù)據(jù)
adb uninstall -k com.example.application刪除應(yīng)用,但會(huì)保留應(yīng)用數(shù)據(jù)和緩存數(shù)據(jù)。
- adb shell pm list packages
打印設(shè)備下面的所有應(yīng)用包名。
adb shell pm list packages [options] <FiLTER>

其他的過濾方式和限定條件這里也不舉例了。
- adb shell pm path
打印 apk 的路徑。
adb shell pm path <package>
- adb shell pm clear
清除應(yīng)用緩存。
adb shell pm clear <package>
File Manager
- adb pull
從 Android 設(shè)備下載文件到 PC。
adb pull <remote> [local]
其中 <remote> 代表文件在設(shè)備中的地址,[local] 代表存放目錄。
- adb push
把 PC 的文件存放到 Android 設(shè)備。
adb push <local> <remote>
- adb shell ls
列出目錄內(nèi)容。
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
創(chuàng)建一個(gè)文件夾
adb shell mkdir [options] <directory name>
- adb shell touch
創(chuàng)建一個(gè)新文件或者改變文件修改時(shí)間
adb shell touch [options] <file>
- adb shell pwd
定位當(dāng)前的操作位置
adb shell pwd
- adb shell cp
字面意思,很好理解,復(fù)制。
adb shell cp [options] <source> <dest>
- adb shell mv
移動(dòng)或者更名文件
adb shell mv [options] <source> <dest>
Network
adb shell netstat
主要用于網(wǎng)絡(luò)統(tǒng)計(jì)。adb shell ping
沒啥好說的,和 PC 的 ping 命令一樣的。adb shell netcfg
通過配置文件配置和管理網(wǎng)絡(luò)連接。
adb shell netcfg [<interface> {dhcp|up|down}]
- adb shell ip
主要用于顯示一些數(shù)據(jù)
adb shell ip [OPTIONS] OBJECT

Logcat
- adb logcat
打印日志文件。
adb logcat [options] [filter-specs]
當(dāng)然可以像 Android Studio 一樣只打印固定的日志
adb logcat *:Vlowest priority, filter to only show Verbose level
adb logcat *:Dfilter to only show Debug level
adb logcat *:Ifilter to only show Info level
adb logcat *:Wfilter to only show Warning level
adb logcat *:Efilter to only show Error level
adb logcat *:Ffilter to only show Fatal level
adb logcat *:SSilent, highest priority, on which nothing is ever printed
adb logcat -b <Buffer>
adb logcat -b radioView the buffer that contains radio/telephony related messages.
adb logcat -b eventView the buffer containing events-related messages.
adb logcat -b maindefault
adb logcat -cClears the entire log and exits.
adb logcat -dDumps the log to the screen and exits.
adb logcat -f test.logsWrites log message output to test.logs .
adb logcat -gPrints the size of the specified log buffer and exits.
adb logcat -n <count>*Sets the maximum number of rotated logs to <count>. *
- adb shell dumpsys
獲取系統(tǒng)數(shù)據(jù)。
adb shell dumpsys [options]
其中有個(gè)非常好用的是,當(dāng)你在新接觸一個(gè)項(xiàng)目的時(shí)候,不熟悉項(xiàng)目流程,此時(shí)正好可以用這個(gè)直接定位到你的 Activity 位置。
adb shell dumpsys activity activities

如圖,直接在打印出來內(nèi)容的后半段找到了當(dāng)前 Activity 的定位,是 NewLoginActivity。
- adb shell dumpstate
和命令直譯差不多,dumps state。
Screenshot
- adb shell screencap
一般的手機(jī)都有截圖功能(一般下拉菜單中有),但不代表所有 Android 設(shè)備都在可視化中開啟了這個(gè)功能,所以這時(shí)候這個(gè) adb 命令就顯得特別重要。
adb shell screencap <filename>
結(jié)合前面的 pull 命令,就可以讓我們輕松拿到屏幕截圖。
adb shell screencap /sdcard/test.png截圖存放
adb pull /sdcard/test.png取到 PC 當(dāng)前文件夾
- adb shell screencord
有了屏幕截圖,自然也得有屏幕錄制,可惜這個(gè)必須在 Android 4.4 (API level 19) 以上才可使用。
adb shell screencord /sdcard/test.mp4
這個(gè)還可以對(duì)大小 size 和 時(shí)間做限制,感興趣的可以自行了解。
System
adb root
獲取 root 權(quán)限。adb sideload
adb shell ps
打印進(jìn)程狀態(tài)。adb shell top
展現(xiàn)上層 CPU 進(jìn)程信息。adb shell getprop
獲取 Android 系統(tǒng)服務(wù)屬性adb shell setprop
設(shè)置服務(wù)屬性。
