adb 操作命令集合

什么是 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è)置方法:

  1. 打開設(shè)備的調(diào)試模式,允許 USB 以 MTP 模式傳輸,確保設(shè)備和 PC 能正常連接,可以通過 adb shell 或者 adb devices 等進(jìn)行驗(yàn)證。
  2. 確保已連接后,依次執(zhí)行以下命令:
    adb root
    adb remount
    adb pull /system/build.prop ./
  3. 在 adb 命令執(zhí)行的文件夾下的 build.prop 中加入命令 service.adb.tcp.port=5555
  4. 執(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 *:V lowest priority, filter to only show Verbose level
adb logcat *:D filter to only show Debug level
adb logcat *:I filter to only show Info level
adb logcat *:W filter to only show Warning level
adb logcat *:E filter to only show Error level
adb logcat *:F filter to only show Fatal level
adb logcat *:S Silent, highest priority, on which nothing is ever printed

adb logcat -b <Buffer>

adb logcat -b radio View the buffer that contains radio/telephony related messages.
adb logcat -b event View the buffer containing events-related messages.
adb logcat -b main default
adb logcat -c Clears the entire log and exits.
adb logcat -d Dumps the log to the screen and exits.
adb logcat -f test.logs Writes log message output to test.logs .
adb logcat -g Prints 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ù)屬性。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容