歡迎關注公眾號[喵先生課堂]
adb是什么?
adb的全稱為Android Debug Bridge。通過adb我們可以在Eclipse中方面通過DDMS來調試Android程序,說白了就是debug工具,默認端口為5037。
adb有什么用?
借助adb工具,我們可以管理設備或手機模擬器的狀態。還可以進行很多手機操作,如安裝軟件、系統升級、運行shell命令等等。其實簡而言說,adb就是連接Android手機與PC端的橋梁,可以讓用戶在電腦上對手機進行全面的操作!
環境搭建
本文以windows為例:
adb 的安裝可以有多種方式:
-
直接下載adb相關文件到本地。
-
通SDK 平台工具(https://developer.android.com/studio/releases/platform-tools?hl=zh_cn)下載 SDK Platform Tools。
-
下載Android 的SDk(文件較大,包含上述兩個文件)。
這里以下載 SDK Platform Tools 為例:
-
訪問SDK平台工具(https://developer.android.com/studio/releases/platform-tools?hl=zh_cn), 在"下載"一欄中選擇 "下載適用於Windows的SDK Platform-Tools",點擊直接下載即可。
-
解壓已下載的SDK Platform-Tools文件夾,路徑最好不要存在中文,以免出現不必要的麻煩。
-
添加環境變量:找到 “此電腦”,右鍵選擇屬性-->高級系統設置-->環境變量,在系統變量中找到 “Path” 項,選中並點擊“編輯”,將“SDK Platform-Tools”文件夾的絕對路徑添加到Path中,依次點擊確定即可。
-
按住 win+R 鍵打開cmd窗口,輸入"adb version",展示adb版本信息即表示安裝成功。
———————————————————————————————
基本用法
啟動/停止:
啟動 adb server 命令:
adb start-server
停止 adb server 命令:
adb kill-server
查看 adb 版本****命令:
adb version
示例輸出:
Android Debug Bridge version 1.0.41
指定 adb server 的網絡端口:
adb -P <port> start-server
默認端口為 5037。
設備連接管理
查詢已連接設備/模擬器:
adb devices
輸出示例:
List of devices attached
APH7N19529019277 device
-
輸出格式為 [serialNumber] [state],serialNumber 即SN,state 有如下幾種:
-
`offline` —— 表示設備未連接成功或無響應。
-
`device` —— 設備已連接。注意這個狀態並不能標識 Android 系統已經完全啟動和可操作,在設備啟動過程中設備實例就可連接到 adb,但啟動完畢后系統才處於可操作狀態。
-
`no device` —— 沒有設備/模擬器連接。
以上輸出顯示當前已經連接了三台設備/模擬器
常見異常輸出:
- 沒有設備/模擬器連接成功。
List of devices attached
- 設備/模擬器未連接到 adb 或無響應。
List of devices attached
cf264b8f offline
USB 連接
通過 USB 連接來正常使用 adb 需要保證幾點:
-
硬件狀態正常。
-
包括 Android 設備處於正常開機狀態,USB 連接線和各種接口完好。
-
Android 設備的開發者選項和 USB 調試模式已開啟。
-
可以到「設置」-「開發者選項」-「Android 調試」查看。
-
如果在設置里找不到開發者選項,那需要通過一個彩蛋來讓它顯示出來:在「設置」-「關於手機」連續點擊「版本號」7 次。
-
設備驅動狀態正常。
-
這一點貌似在 Linux 和 Mac OS X 下不用操心,在 Windows 下有可能遇到需要安裝驅動的情況,確認這一點可以右鍵「計算機」-「屬性」,
-
到「設備管理器」里查看相關設備上是否有黃色感嘆號或問號,如果沒有就說明驅動狀態已經好了。否則可以下載一個手機助手類程序來安裝驅動先。
-
通過 USB 線連接好電腦和設備后確認狀態。
adb devices
如果能看到
xxxxxx device
說明連接成功。
應用管理
查看應用列表
adb shell pm list packages [-f] [-d] [-e] [-s] [-3] [-i] [-u] [--user USER_ID] [FILTER]
即在 `adb shell pm list packages` 的基礎上可以加一些參數進行過濾查看不同的列表,支持的過濾參數如下:
參數 |
顯示列表 |
無 |
所有應用 |
-f |
顯示應用關聯的 apk 文件參數 |
-d | 只顯示 disabled 的應用 |
-e | 只顯示 enabled 的應用 |
-s | 只顯示系統應用 |
-3 | 只顯示第三方應用 |
-i | 顯示應用的 installer |
-u | 包含已卸載應用 |
`<FILTER>` | 包名包含 `<FILTER>` 字符串 |
所有應用命令:
adb shell pm list packages
輸出示例:
package:com.taobao.taobao
package:com.huawei.ca
package:com.huawei.skytone
package:com.huawei.assetsyncservice
package:jnhyjt.tjgamsfwpt.tjga
package:com.qq.qcloud
package:com.huawei.aod
package:com.huawei.def
package:com.huawei.hbm
package:com.huawei.hff
package:com.huawei.ims
package:com.huawei.lbs
package:com.android.bluetooth
package:com.xiaomi.hm.health
package:com.ggyd.EarPro
package:com.android.providers.contacts
package:com.netease.cloudmusic
package:tv.danmaku.bili
package:com.android.captiveportallogin
package:com.huawei.audioaccessorymanager
package:com.huawei.hiaction
package:com.huawei.trustedthingsauth
package:com.eg.android.AlipayGphone
package:com.huawei.android.airsharing
package:cn.wps.moffice_eng
package:com.huawei.rcsserviceapplication
package:com.lucky.luckyclient
package:com.huawei.ohos.famanager
...
查看系統應用:
adb shell pm list packages -s
查看第三方應用:
adb shell pm list packages -3
包名包含某字符串的應用:
adb shell pm list packages jingdong
使用 grep 過濾(windows 改用 findstr):
adb shell pm list packages | grep jingdong
安裝 APK命令格式:
adb install [-lrtsdg] <path_to_apk>
參數:
`adb install` 后面可以跟一些可選參數來控制安裝 APK 的行為,可用參數及含義如下:
參數 | 含義 |
-l | 將應用安裝到保護目錄 /mnt/asec |
-r |
允許覆蓋安裝(常用) |
-t |
允許安裝 AndroidManifest.xml 里 application 指定 `android:testOnly="true"` 的應用 |
-s |
將應用安裝到 sdcard |
-d |
允許降級覆蓋安裝(常用) |
-g | 授予所有運行時權限(常用) |
運行后如果見到類似如下輸出(狀態為 `Success`)代表安裝成功:
[100%] /data/local/tmp/1.apk
pkg: /data/local/tmp/1.apk
Success
而如果狀態為 `Failure` 則表示安裝失敗,比如:
[100%] /data/local/tmp/map-20160831.apk
pkg: /data/local/tmp/map-20160831.apk
Failure [INSTALL_FAILED_ALREADY_EXISTS]
卸載應用命令:
adb uninstall [-k] <packagename>
`
命令示例:
adb uninstall -k com.jingdong.app.mall
表示卸載 京東App。
清除應用數據與緩存命令:
adb shell pm clear <packagename>
命令示例:
adb shell pm clear com.jingdong.app.mall
清除 京東App的數據和緩存。
查看前台 Activity命令:
adb shell dumpsys activity activities | grep mResumedActivity
輸出示例:
mResumedActivity: ActivityRecord{c6b5f44 u0 com.tencent.mm/.ui.LauncherUI t36978}
其中的 `com.tencent.mm/.ui.LauncherUI` 就是當前處於前台的 Activity。
*在 Windows 可以嘗試 `adb shell dumpsys activity activities | findstr mResumedActivity`
查看正在運行的 Services命令:
adb shell dumpsys activity services [<packagename>]
查看應用詳細信息命令:
adb shell dumpsys package <packagename>
輸出中包含很多信息,包括 Activity Resolver Table、Registered ContentProviders、包名、userId、安裝后的文件資源代碼等路徑、版本信息、權限信息和授予狀態、簽名版本信息等。
`
輸出示例:
Activity Resolver Table:
Full MIME Types:
vnd.android.cursor.item/vnd.com.tencent.mm.chatting.profile:
be9b872 com.tencent.mm/.plugin.account.ui.ContactsSyncUI filter be5cecc
Action: "android.intent.action.VIEW"
Action: "com.tencent.mm.login.ACTION_LOGIN"
Category: "android.intent.category.DEFAULT"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.login"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.profile"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.voip"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.voip.video"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.plugin.sns.timeline"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.phonenum"
vnd.android.cursor.item/vnd.com.tencent.mm.plugin.sns.timeline:
be9b872 com.tencent.mm/.plugin.account.ui.ContactsSyncUI filter be5cecc
Action: "android.intent.action.VIEW"
Action: "com.tencent.mm.login.ACTION_LOGIN"
Category: "android.intent.category.DEFAULT"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.login"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.profile"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.voip"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.voip.video"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.plugin.sns.timeline"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.phonenum"
application/*:
be5c38a com.tencent.mm/.ui.tools.ShareImgUI filter be2b1c3
Action: "android.intent.action.SEND"
Category: "android.intent.category.DEFAULT"
Type: "image"
Type: "video"
Type: "text"
Type: "application"
mPriority=0, mOrder=0, mHasPartialTypes=true
be5c398 com.tencent.mm/.ui.tools.AddFavoriteUI filter be2b1ea
Action: "android.intent.action.SEND"
Category: "android.intent.category.DEFAULT"
Type: "image"
Type: "video"
Type: "text"
Type: "application"
Type: "audio"
mPriority=0, mOrder=0, mHasPartialTypes=true
vnd.android.cursor.item/vnd.com.tencent.mm.chatting.voip:
be9b872 com.tencent.mm/.plugin.account.ui.ContactsSyncUI filter be5cecc
Action: "android.intent.action.VIEW"
Action: "com.tencent.mm.login.ACTION_LOGIN"
Category: "android.intent.category.DEFAULT"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.login"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.profile"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.voip"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.voip.video"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.plugin.sns.timeline"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.phonenum"
text/*:
be5c38a com.tencent.mm/.ui.tools.ShareImgUI filter be2b1c3
Action: "android.intent.action.SEND"
Category: "android.intent.category.DEFAULT"
Type: "image"
Type: "video"
Type: "text"
Type: "application"
mPriority=0, mOrder=0, mHasPartialTypes=true
be5c398 com.tencent.mm/.ui.tools.AddFavoriteUI filter be2b1ea
Action: "android.intent.action.SEND"
Category: "android.intent.category.DEFAULT"
Type: "image"
Type: "video"
Type: "text"
Type: "application"
Type: "audio"
mPriority=0, mOrder=0, mHasPartialTypes=true
audio/*:
be5c398 com.tencent.mm/.ui.tools.AddFavoriteUI filter be2b1ea
Action: "android.intent.action.SEND"
Category: "android.intent.category.DEFAULT"
Type: "image"
Type: "video"
Type: "text"
Type: "application"
Type: "audio"
mPriority=0, mOrder=0, mHasPartialTypes=true
vnd.android.cursor.item/vnd.com.tencent.mm.chatting.phonenum:
be9b872 com.tencent.mm/.plugin.account.ui.ContactsSyncUI filter be5cecc
Action: "android.intent.action.VIEW"
Action: "com.tencent.mm.login.ACTION_LOGIN"
Category: "android.intent.category.DEFAULT"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.login"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.profile"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.voip"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.voip.video"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.plugin.sns.timeline"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.phonenum"
vnd.android.cursor.item/vnd.com.tencent.mm.chatting.voip.video:
be9b872 com.tencent.mm/.plugin.account.ui.ContactsSyncUI filter be5cecc
Action: "android.intent.action.VIEW"
Action: "com.tencent.mm.login.ACTION_LOGIN"
Category: "android.intent.category.DEFAULT"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.login"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.profile"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.voip"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.voip.video"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.plugin.sns.timeline"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.phonenum"
vnd.android.cursor.item/vnd.com.tencent.mm.login:
be9b872 com.tencent.mm/.plugin.account.ui.ContactsSyncUI filter be5cecc
Action: "android.intent.action.VIEW"
Action: "com.tencent.mm.login.ACTION_LOGIN"
Category: "android.intent.category.DEFAULT"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.login"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.profile"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.voip"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.voip.video"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.plugin.sns.timeline"
Type: "vnd.android.cursor.item/vnd.com.tencent.mm.chatting.phonenum"
video/*:
be5c38a com.tencent.mm/.ui.tools.ShareImgUI filter be2b1c3
Action: "android.intent.action.SEND"
Category: "android.intent.category.DEFAULT"
Type: "image"
Type: "video"
Type: "text"
Type: "application"
mPriority=0, mOrder=0, mHasPartialTypes=true
be5c391 com.tencent.mm/.ui.tools.ShareToStatusUI filter be2b1dd
Action: "android.intent.action.SEND"
Category: "android.intent.category.DEFAULT"
Type: "image"
Type: "video"
mPriority=0, mOrder=0, mHasPartialTypes=true
be5c398 com.tencent.mm/.ui.tools.AddFavoriteUI filter be2b1ea
Action: "android.intent.action.SEND"
Category: "android.intent.category.DEFAULT"
Type: "image"
Type: "video"
Type: "text"
Type: "application"
Type: "audio"
mPriority=0, mOrder=0, mHasPartialTypes=true
*/heic:
be5c3bb com.tencent.mm/.ui.tools.ShareScreenImgUI filter be5ce23
Action: "android.intent.action.VIEW"
Category: "android.intent.category.DEFAULT"
Type: "image"
Type: "*/heic"
mPriority=0, mOrder=0, mHasPartialTypes=true
be5c3c9 com.tencent.mm/.ui.tools.ShareScreenToTimeLineUI filter be5ce3d
Action: "android.intent.action.VIEW"
Category: "android.intent.category.DEFAULT"
Type: "image"
Type: "*/heic"
mPriority=0, mOrder=0, mHasPartialTypes=true
image/*:
be5c38a com.tencent.mm/.ui.tools.ShareImgUI filter be2b1c3
Action: "android.intent.action.SEND"
Category: "android.intent.category.DEFAULT"
Type: "image"
Type: "video"
Type: "text"
Type: "application"
mPriority=0, mOrder=0, mHasPartialTypes=true
be5c38a com.tencent.mm/.ui.tools.ShareImgUI filter be2b1d0
Action: "android.intent.action.SEND_MULTIPLE"
Category: "android.intent.category.DEFAULT"
Type: "image"
mPriority=0, mOrder=0, mHasPartialTypes=true
be5c391 com.tencent.mm/.ui.tools.ShareToStatusUI filter be2b1dd
Action: "android.intent.action.SEND"
Category: "android.intent.category.DEFAULT"
Type: "image"
Type: "video"
mPriority=0, mOrder=0, mHasPartialTypes=true
be5c398 com.tencent.mm/.ui.tools.AddFavoriteUI filter be2b1ea
Action: "android.intent.action.SEND"
Category: "android.intent.category.DEFAULT"
Type: "image"
Type: "video"
Type: "text"
Type: "application"
Type: "audio"
mPriority=0, mOrder=0, mHasPartialTypes=true
be5c398 com.tencent.mm/.ui.tools.AddFavoriteUI filter be5ce09
Action: "android.intent.action.SEND_MULTIPLE"
Category: "android.intent.category.DEFAULT"
Type: "image"
mPriority=0, mOrder=0, mHasPartialTypes=true
be5c39f com.tencent.mm/.ui.tools.ShareToTimeLineUI filter be5ce16
Action: "android.intent.action.SEND"
Category: "android.intent.category.DEFAULT"
Type: "image"
mPriority=0, mOrder=0, mHasPartialTypes=true
be5c3bb com.tencent.mm/.ui.tools.ShareScreenImgUI filter be5ce23
Action: "android.intent.action.VIEW"
Category: "android.intent.category.DEFAULT"
Type: "image"
Type: "*/heic"
mPriority=0, mOrder=0, mHasPartialTypes=true
be5c3c9 com.tencent.mm/.ui.tools.ShareScreenToTimeLineUI filter be5ce3d
Action: "android.intent.action.VIEW"
Category: "android.intent.category.DEFAULT"
Type: "image"
Type: "*/heic"
mPriority=0, mOrder=0, mHasPartialTypes=true
查看應用安裝路徑命令:
adb shell pm path <PACKAGE>
輸出應用安裝路徑
輸出示例:
adb shell pm path com.tencent.mm
package:/data/app/com.tencent.mm-Khc5y5damU6A9MMQerHRgA==/base.apk
啟動應用/ 調起 Activity
- 指定Activity名稱啟動命令格式:
adb shell am start [options] <INTENT>
例如:
adb shell am start -n com.tencent.mm/.ui.LauncherUI
表示調起微信主界面。
強制停止應用命令:
adb shell am force-stop <packagename>
命令示例:
adb shell am force-stop com.tencent.mm
表示停止 微信的一切進程與服務。
文件管理
復制設備里的文件到電腦命令:
adb pull <手機里的文件路徑> [電腦上的存放目錄]
其中 `電腦上的目錄` 參數可以省略,默認復制到當前目錄。
例:
adb pull /sdcard/adn.mp4 ~/tmp/
復制電腦里的文件到設備命令:
adb push <電腦上的文件路徑> <設備里的目錄>
例:
adb push ~/adb.mp4 /sdcard/
模擬按鍵/輸入
`adb shell input` ,通過它可以做很多事情。
比如使用 `adb shell input keyevent
keycode | 含義 |
3 | HOME 鍵 |
4 |
返回鍵 |
5 | 打開撥號應用 |
6 | 掛斷電話 |
24 | 增加音量 |
25 | 降低音量 |
26 | 電源鍵 |
27 | 拍照(需要在相機應用里) |
64 | 打開瀏覽器 |
82 | 菜單鍵 |
85 | 播放/暫停 |
86 | 停止播放 |
87 | 播放下一首 |
88 |
播放上一首 |
122 | 移動光標到行首或列表頂部 |
123 | 移動光標到行末或列表底部 |
126 | 恢復播放 |
127 | 暫停播放 |
164 | 靜音 |
176 | 打開系統設置 |
187 | 切換應用 |
207 | 打開聯系人 |
208 | 打開日歷 |
209 | 打開音樂 |
210 | 打開計算器 |
220 | 降低屏幕亮度 |
221 | 提高屏幕亮度 |
223 | 系統休眠 |
224 | 點亮屏幕 |
231 | 打開語音助手 |
276 | 如果沒有 wakelock 則讓系統休眠 |
下面是 `input` 命令的一些用法舉例。
電源鍵命令:
adb shell input keyevent 26
執行效果相當於按電源鍵。
菜單鍵命令:
adb shell input keyevent 82
其它命令使用方式如上,這里不再做過多演示
滑動解鎖
鎖屏通過滑動手勢解鎖,可以通過 `input swipe` 來實現。:
adb shell input swipe 300 1000 300 500
參數 `300 1000 300 500` 分別表示`起始點x坐標 起始點y坐標 結束點x坐標 結束點y坐標`。
輸入文本
點擊某文本框后,可以通過 `input` 命令來輸入文本:
adb shell input text hello
現在 `hello` 出現在文本框了。
查看日志
Android 日志命令:
[adb] logcat [<option>] ... [<filter-spec>] ...
常用用法列舉如下:
#### 按級別過濾日志
Android 的日志優先級(priority):
-
V —— Verbose(最低,輸出得最多)
-
D —— Debug
-
I —— Info
-
W —— Warning
-
E —— Error
-
F —— Fatal
-
S —— Silent(最高,啥也不輸出)
按某級別過濾日志則會將該級別及以上的日志輸出。
比如,命令:
adb logcat *:W
會將 Warning、Error、Fatal 和 Silent 日志輸出。
按 tag 和級別過濾日志
`
比如,命令:
adb logcat ActivityManager:I MyApp:D *:S
表示輸出 tag `ActivityManager` 的 Info 以上級別日志,輸出 tag `MyApp` 的 Debug 以上級別日志,及其它 tag 的 Silent 級別日志(即屏蔽其它 tag 日志)。
日志格式
可以用 `adb logcat -v
日志支持按以下幾種 `
* brief
默認格式。格式為:
<priority>/<tag>(<pid>): <message>
示例:
D/HeadsetStateMachine( 1785): Disconnected process message: 10, size: 0
* process
格式為:
<priority>(<pid>) <message>
示例:
D( 1785) Disconnected process message: 10, size: 0 (HeadsetStateMachine)
* tag
格式為<priority>/<tag>: <message>
示例:
D/HeadsetStateMachine: Disconnected process message: 10, size: 0
* raw
格式為:
<message>
示例:
Disconnected process message: 10, size: 0
* time
格式為:
<datetime> <priority>/<tag>(<pid>): <message>
示例:
08-28 22:39:39.974 D/HeadsetStateMachine( 1785): Disconnected process message: 10, size: 0
* threadtime
格式為:
<datetime> <pid> <tid> <priority> <tag>: <message>
示例:
08-28 22:39:39.974 1785 1832 D HeadsetStateMachine: Disconnected process message: 10, size: 0
* long
格式為:
[ <datetime> <pid>:<tid> <priority>/<tag> ]
<message>
示例:
[ 08-28 22:39:39.974 1785: 1832 D/HeadsetStateMachine ]
Disconnected process message: 10, size: 0
指定格式可與上面的過濾同時使用。比如:
adb logcat -v long ActivityManager:I *:S
清空日志
adb logcat -c
內核日志
adb shell dmesg
輸出示例:
<6>[14201.684016] PM: noirq resume of devices complete after 0.982 msecs
<6>[14201.685525] PM: early resume of devices complete after 0.838 msecs
<6>[14201.753642] PM: resume of devices complete after 68.106 msecs
<4>[14201.755954] Restarting tasks ... done.
<6>[14201.771229] PM: suspend exit 2016-08-28 13:31:32.679217193 UTC
<6>[14201.872373] PM: suspend entry 2016-08-28 13:31:32.780363596 UTC
<6>[14201.872498] PM: Syncing filesystems ... done.
中括號里的 `[14201.684016]` 代表內核開始啟動后的時間,單位為秒。
通過內核日志我們可以做一些事情,比如衡量內核啟動時間,在系統啟動完畢后的內核日志里找到 `Freeing init memory` 那一行前面的時間就是。
查看設備信息
型號命令:
adb shell getprop ro.product.model
輸出示例:
VOG-AL10
電池狀況
adb shell dumpsys battery
輸入示例:
Current Battery Service state:
AC powered: false
USB powered: true
Wireless powered: false
Max charging current: 500000
Max charging voltage: 4718000
Charge counter: 450000
status: 2
health: 2
present: true
level: 85
scale: 100
voltage: 4203
temperature: 240
technology: Li-poly
其中 `scale` 代表最大電量,`level` 代表當前電量。上面的輸出表示還剩下 44% 的電量。
屏幕分辨率
adb shell wm size
輸出示例:
Physical size: 1080x2340
該設備屏幕分辨率為 1080px * 2340px。
屏幕密度
adb shell wm density
輸出示例:
Physical density: 480
顯示屏參數
adb shell dumpsys window displays
輸出示例:
WINDOW MANAGER DISPLAY CONTENTS (dumpsys window displays)
Display: mDisplayId=0
init=1080x2340 480dpi cur=1080x2340 app=1080x2265 rng=1080x1005-2265x2265
deferred=false mLayoutNeeded=false mTouchExcludeRegion=SkRegion((0,0,1080,2458))
mLayoutSeq=355690
mDeferredRotationPauseCount=0
mCurrentFocus=Window{c5dd371 u0 com.tencent.mm/com.tencent.mm.ui.LauncherUI}
mFocusedApp=AppWindowToken{c67d8b1 token=Token{c617f21 ActivityRecord{c04d409 u0 com.tencent.mm/.ui.LauncherUI t37102}}}
mLastStatusBarVisibility=0xa518
displayId=0
mWallpaperTarget=null
mLastWallpaperX=0.5 mLastWallpaperY=0.5
mDeferUpdateImeTargetCount=0
...
android\_id
adb shell settings get secure android_id
輸出示例:
aadf190ed474a1ae
Android 系統版本
adb shell getprop ro.build.version.release
輸出示例:
10
CPU 信息
adb shell cat /proc/cpuinfo
輸出示例:
Processor : AArch64 Processor rev 0 (aarch64)
processor : 0
BogoMIPS : 3.84
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x1
CPU part : 0xd05
CPU revision : 0
processor : 1
BogoMIPS : 3.84
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x1
CPU part : 0xd05
CPU revision : 0
processor : 2
BogoMIPS : 3.84
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x1
CPU part : 0xd05
CPU revision : 0
processor : 3
BogoMIPS : 3.84
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x1
CPU part : 0xd05
CPU revision : 0
processor : 4
BogoMIPS : 3.84
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp
CPU implementer : 0x48
CPU architecture: 8
CPU variant : 0x1
CPU part : 0xd40
CPU revision : 0
processor : 5
BogoMIPS : 3.84
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp
CPU implementer : 0x48
CPU architecture: 8
CPU variant : 0x1
CPU part : 0xd40
CPU revision : 0
processor : 6
BogoMIPS : 3.84
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp
CPU implementer : 0x48
CPU architecture: 8
CPU variant : 0x1
CPU part : 0xd40
CPU revision : 0
processor : 7
BogoMIPS : 3.84
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp
CPU implementer : 0x48
CPU architecture: 8
CPU variant : 0x1
CPU part : 0xd40
CPU revision : 0
Hardware : vendor Kirin980
這是華為P30pro 的 CPU 信息
內存信息
adb shell cat /proc/meminfo
輸出示例:
MemTotal: 7789200 kB
MemFree: 552980 kB
MemAvailable: 3076176 kB
Buffers: 8228 kB
Cached: 2675048 kB
SwapCached: 35740 kB
Active: 1518432 kB
Inactive: 2521564 kB
Active(anon): 313244 kB
Inactive(anon): 1172720 kB
Active(file): 1205188 kB
Inactive(file): 1348844 kB
Unevictable: 154676 kB
Mlocked: 154676 kB
SwapTotal: 4194300 kB
SwapFree: 1386624 kB
Dirty: 76 kB
Writeback: 0 kB
AnonPages: 1505672 kB
Mapped: 1369448 kB
Shmem: 42384 kB
Slab: 558428 kB
SReclaimable: 215332 kB
SUnreclaim: 343096 kB
SlabLarge: 16224 kB
KernelStack: 113808 kB
PageTables: 141496 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 8088900 kB
Committed_AS: 118311256 kB
VmallocTotal: 262143936 kB
VmallocUsed: 0 kB
VmallocChunk: 0 kB
CmaTotal: 1187840 kB
CmaFree: 0 kB
IonTotalCache: 0 kB
IonTotalUsed: 282752 kB
Protected: 0 kB
RsvTotalUsed: 361988 kB
其中,`MemTotal` 就是設備的總內存,`MemFree` 是當前空閑內存。
修改設置
**注:** 修改設置之后,運行恢復命令有可能顯示仍然不太正常,可以運行 `adb reboot` 重啟設備,或手動重啟。
修改設置的原理主要是通過 settings 命令修改 /data/data/com.android.providers.settings/databases/settings.db 里存放的設置值。
分辨率
adb shell wm size 480x1024
表示將分辨率修改為 480px * 1024px。
恢復原分辨率命令:
adb shell wm size reset
屏幕密度
adb shell wm density 160
表示將屏幕密度修改為 160dpi。
恢復原屏幕密度命令:
adb shell wm density reset
顯示區域
adb shell wm overscan 0,0,0,200
四個數字分別表示距離左、上、右、下邊緣的留白像素,以上命令表示將屏幕底部 200px 留白。
恢復原顯示區域命令:
adb shell wm overscan reset
關閉 USB 調試模式
恢復:
用命令恢復不了了,畢竟關閉了 USB 調試 adb 就連接不上 Android 設備了。
去設備上手動恢復吧:「設置」-「開發者選項」-「Android 調試」。
允許/禁止訪問非 SDK API
允許訪問非 SDK API:
adb shell settings put global hidden_api_policy_pre_p_apps 1
adb shell settings put global hidden_api_policy_p_apps 1
禁止訪問非 SDK API:
adb shell settings delete global hidden_api_policy_pre_p_apps
adb shell settings delete global hidden_api_policy_p_apps
不需要設備獲得 Root 權限。
命令最后的數字的含義:
值 | 含義 |
0 | 禁止檢測非 SDK 接口的調用。該情況下,日志記錄功能被禁用,並且令 strict mode API,即 detectNonSdkApiUsage() 無效。不推薦 |
1 | 僅警告——允許訪問所有非 SDK 接口,但保留日志中的警告信息,可繼續使用 strick mode API |
2 | 禁止調用深灰名單和黑名單中的接口 |
3 | 禁止調用黑名單中的接口,但允許調用深灰名單中的接口 |
狀態欄和導航欄的顯示隱藏
本節所說的相關設置對應 Cyanogenmod 里的「擴展桌面」。
adb shell settings put global policy_control <key-values>
`
key | 含義 |
immersive.full | 同時隱藏 |
immersive.status | 隱藏狀態欄 |
immersive.navigation | 隱藏導航欄 |
mmersive.preconfirms | ? |
這些鍵對應的值可則如下值用逗號組合:
value | 含義 |
apps | 所有應用 |
* | 所有界面 |
packagename | 指定應用 |
-packagename | 排除指定應用 |
例如:
adb shell settings put global policy_control immersive.full=*
表示設置在所有界面下都同時隱藏狀態欄和導航欄。
adb shell settings put global policy_control immersive.status=com.package1,com.package2:immersive.navigation=apps,-com.package3
表示設置在包名為 `com.package1` 和 `com.package2` 的應用里隱藏狀態欄,在除了包名為 `com.package3` 的所有應用里隱藏導航欄。
實用功能
屏幕截圖
截圖保存到電腦:
adb exec-out screencap -p > sc.png
如果 adb 版本較老,無法使用 `exec-out` 命令,這時候建議更新 adb 版本。無法更新的話可以使用以下麻煩點的辦法:
先截圖保存到設備里:
adb shell screencap -p /sdcard/sc.png
然后將 png 文件導出到電腦:
adb pull /sdcard/sc.png
錄制屏幕
錄制屏幕以 mp4 格式保存到 /sdcard:
adb shell screenrecord /sdcard/filename.mp4
需要停止時按 Ctrl-C,默認錄制時間和最長錄制時間都是 180 秒。
如果需要導出到電腦:
adb pull /sdcard/filename.mp4
可以使用 `adb shell screenrecord --help` 查看 `screenrecord` 命令的幫助信息,下面是常見參數及含義:
參數 | 含義 |
--size WIDTHxHEIGHT | 視頻的尺寸,比如 `1280x720`,默認是屏幕分辨率。 |
--bit-rate RATE | 視頻的比特率,默認是 4Mbps。 |
--time-limit TIME | 錄制時長,單位秒 |
--verbose | 輸出更多信息。 |
設置系統日期和時間
**注:需要 root 權限。**
adb shell
su
date -s 20160823.131500
表示將系統日期和時間更改為 2016 年 08 月 23 日 13 點 15 分 00 秒。
重啟手機
adb reboot
檢測設備是否已 root
adb shell
此時命令行提示符是 `$` 則表示沒有 root 權限,是 `#` 則表示已 root。
使用 Monkey 進行壓力測試
Monkey 可以生成偽隨機用戶事件來模擬單擊、觸摸、手勢等操作,可以對正在開發中的程序進行隨機壓力測試。
簡單用法:
adb shell monkey -p <packagename> -v 500
表示向 `
Monkey 的詳細用法參考 官方文檔(https://developer.android.com/studio/test/monkey.html)。
Android 系統是基於 Linux 內核的,所以 Linux 里的很多命令在 Android 里也有相同或類似的實現,在 `adb shell` 里可以調用。本文檔前面的部分內容已經用到了 `adb shell` 命令。
查看進程
adb shell ps
輸出示例:
USER PID PPID VSZ RSS WCHAN ADDR S NAME
root 1 0 78156 3180 0 0 S init
root 2 0 0 0 0 0 S [kthreadd]
root 3 2 0 0 0 0 I [rcu_gp]
root 7 2 0 0 0 0 I [mm_percpu_wq]
root 8 2 0 0 0 0 S [ksoftirqd/0]
root 9 2 0 0 0 0 I [rcu_preempt]
root 10 2 0 0 0 0 I [rcu_sched]
root 11 2 0 0 0 0 I [rcu_bh]
root 12 2 0 0 0 0 S [migration/0]
root 13 2 0 0 0 0 S [watchdog/0]
root 14 2 0 0 0 0 S [cpuhp/0]
root 15 2 0 0 0 0 S [cpuhp/1]
root 16 2 0 0 0 0 S [watchdog/1]
root 17 2 0 0 0 0 S [migration/1]
root 18 2 0 0 0 0 S [ksoftirqd/1]
root 20 2 0 0 0 0 I [kworker/1:0H]
root 21 2 0 0 0 0 S [cpuhp/2]
root 22 2 0 0 0 0 S [watchdog/2]
root 23 2 0 0 0 0 S [migration/2]
root 24 2 0 0 0 0 S [ksoftirqd/2]
root 27 2 0 0 0 0 S [cpuhp/3]
root 28 2 0 0 0 0 S [watchdog/3]
root 29 2 0 0 0 0 S [migration/3]
root 30 2 0 0 0 0 S [ksoftirqd/3]
root 32 2 0 0 0 0 I [kworker/3:0H]
root 33 2 0 0 0 0 S [cpuhp/4]
root 34 2 0 0 0 0 S [watchdog/4]
root 35 2 0 0 0 0 S [migration/4]
root 36 2 0 0 0 0 S [ksoftirqd/4]
root 38 2 0 0 0 0 I [kworker/4:0H]
root 39 2 0 0 0 0 S [cpuhp/5]
root 40 2 0 0 0 0 S [watchdog/5]
root 41 2 0 0 0 0 S [migration/5]
root 42 2 0 0 0 0 S [ksoftirqd/5]
root 44 2 0 0 0 0 I [kworker/5:0H]
root 45 2 0 0 0 0 S [cpuhp/6]
root 46 2 0 0 0 0 S [watchdog/6]
root 47 2 0 0 0 0 S [migration/6]
root 48 2 0 0 0 0 S [ksoftirqd/6]
root 50 2 0 0 0 0 I [kworker/6:0H]
root 51 2 0 0 0 0 S [cpuhp/7]
root 52 2 0 0 0 0 S [watchdog/7]
root 53 2 0 0 0 0 S [migration/7]
root 54 2 0 0 0 0 S [ksoftirqd/7]
root 56 2 0 0 0 0 I [kworker/7:0H]
root 58 2 0 0 0 0 D [bbox_main]
root 60 2 0 0 0 0 D [bbox_cleartext]
root 62 2 0 0 0 0 S [mailbox-10]
root 63 2 0 0 0 0 S [mailbox-11]
root 64 2 0 0 0 0 S [mailbox-12]
root 65 2 0 0 0 0 S [mailbox-13]
root 66 2 0 0 0 0 S [mailbox-14]
root 67 2 0 0 0 0 S [mailbox-15]
root 68 2 0 0 0 0 S [mailbox-16]
root 69 2 0 0 0 0 S [mailbox-17]
root 70 2 0 0 0 0 S [mailbox-18]
root 71 2 0 0 0 0 S [mailbox-23]
root 72 2 0 0 0 0 S [mailbox-25]
root 73 2 0 0 0 0 S [mailbox-26]
root 74 2 0 0 0 0 S [mailbox-27]
root 75 2 0 0 0 0 S [mailbox-28]
root 76 2 0 0 0 0 S [mailbox-29]
root 77 2 0 0 0 0 S [ao-mailbox-0]
root 79 2 0 0 0 0 S [khungtaskd]
root 80 2 0 0 0 0 I [init_wdt_wkq]
root 81 2 0 0 0 0 S [oom_reaper]
root 82 2 0 0 0 0 I [writeback]
root 83 2 0 0 0 0 S [kcompactd0]
root 84 2 0 0 0 0 I [crypto]
root 85 2 0 0 0 0 I [kblockd]
root 86 2 0 0 0 0 I [io_latency_work]
root 87 2 0 0 0 0 I [dsm_wq]
root 88 2 0 0 0 0 S [spi3]
root 90 2 0 0 0 0 S [irq/148-hisi_pm]
root 91 2 0 0 0 0 S [sys_pool]
root 92 2 0 0 0 0 S [sys_heap]
root 93 2 0 0 0 0 S [camera_daemonhe]
root 94 2 0 0 0 0 S [camera_heap]
root 95 2 0 0 0 0 I [devfreq_wq]
root 96 2 0 0 0 0 S [rdr_lpm3_thread]
root 97 2 0 0 0 0 D [hwxcollie]
root 98 2 0 0 0 0 I [cfg80211]
root 99 2 0 0 0 0 I [watchdogd]
root 100 2 0 0 0 0 I [usb_analog_hs_p]
root 101 2 0 0 0 0 I [usb_analog_hs_p]
root 103 2 0 0 0 0 I [slimbus_lost_sy]
root 105 2 0 0 0 0 I [mptcp_wq]
root 106 2 0 0 0 0 S [dspdumplog]
root 107 2 0 0 0 0 I [hifi_om_work_vo]
root 108 2 0 0 0 0 I [hifi_om_work_au]
root 109 2 0 0 0 0 I [hifi_om_work_vo]
root 110 2 0 0 0 0 I [hifi_om_work_vo]
root 111 2 0 0 0 0 I [hifi_om_work_sm]
root 112 2 0 0 0 0 I [hifi_om_work_au]
root 113 2 0 0 0 0 I [hifi_om_work_au]
root 114 2 0 0 0 0 D [mailboxNormal]
root 115 2 0 0 0 0 D [mailboxHigh]
root 117 2 0 0 0 0 S [siqthread/0]
root 118 2 0 0 0 0 S [ipihelper]
root 120 2 0 0 0 0 S [agent_rpmb]
root 121 2 0 0 0 0 S [tuid]
root 122 2 0 0 0 0 S [smc_svc_thread]
root 123 2 0 0 0 0 S [interface_msg_p]
root 162 2 0 0 0 0 S [kauditd]
root 163 2 0 0 0 0 S [kswapd0]
root 164 2 0 0 0 0 S [ecryptfs-kthrea]
root 165 2 0 0 0 0 I [cifsiod]
root 166 2 0 0 0 0 I [cifsoplockd]
root 167 2 0 0 0 0 S [fid_async_t]
root 168 2 0 0 0 0 I [EIMA]
root 173 2 0 0 0 0 I [HW_KERNEL_STP_M]
root 174 2 0 0 0 0 I [HW_KERNEL_STP_S]
root 218 2 0 0 0 0 I [kthrotld]
root 219 2 0 0 0 0 I [fb0_dss_debug]
root 220 2 0 0 0 0 I [fb0_ldi_underfl]
root 221 2 0 0 0 0 I [pipe_clk_updt_w]
root 222 2 0 0 0 0 I [fb0_hiace_end]
root 223 2 0 0 0 0 I [fb0_gmp_lut]
各列含義:
列名 | 含義 |
USER | 所屬用戶 |
PID | 進程 ID |
PPID | 父進程 ID |
NAME | 進程名 |
查看實時資源占用情況
adb shell top
輸出示例:
Tasks: 938 total, 1 running, 937 sleeping, 0 stopped, 0 zombie
Mem: 7.4G total, 7.0G used, 380M free, 8.1M buffers
Swap: 4.0G total, 2.8G used, 1.1G free, 2.6G cached
800%cpu 13%user 0%nice 16%sys 763%idle 0%iow 6%irq 2%sirq 0%host
PID USER PR NI VIRT RES SHR S[%CPU] %MEM TIME+ ARGS
1612 system 18 -2 15G 451M 257M S 7.0 5.9 1682:20.6 system_server
787 system -2 0 3.2G 22M 12M S 5.0 0.2 2966:24.2 surfaceflinger
514 u0_a139 20 0 3.2G 196M 192M S 4.6 2.5 7:17.01 com.UCMobile
16810 shell 20 0 4.3G 29M 29M S 4.3 0.3 492:30.40 apkagent.cli
1098 system 20 0 87M 1.7M 1.3M S 3.6 0.0 237:31.60 vendor.huawei.h+
7514 shell 20 0 30M 4.7M 3.3M R 2.6 0.0 0:00.14 top
7356 u0_a157 20 0 7.3G 164M 151M S 2.6 2.1 48:54.08 com.sohu.inputm+
3243 system 20 0 6.9G 130M 94M S 1.6 1.7 105:44.14 com.huawei.HwOP+
2844 u0_a139 20 0 2.0G 99M 86M S 1.0 1.2 0:04.83 com.UCMobile:Do+
2486 system 20 0 6.5G 129M 101M S 1.0 1.6 58:59.61 com.huawei.syst+
5023 root 20 0 0 0 0 I 0.6 0.0 0:03.81 [kworker/u16:4]
1059 root 20 0 0 0 0 I 0.6 0.0 0:06.91 [kworker/u16:1]
3547 u0_a139 20 0 2.0G 114M 96M S 0.6 1.4 0:11.31 com.UCMobile:ch+
2917 u0_a139 20 0 2.1G 113M 93M S 0.6 1.4 4:00.37 com.UCMobile:Me+
2771 system 20 0 6.3G 110M 87M S 0.6 1.4 118:19.66 com.huawei.iawa+
1567 root -2 0 0 0 0 S 0.6 0.0 227:06.46 [hisi_hcc]
692 root 20 0 2.6G 4.7M 2.9M S 0.6 0.0 28:35.16 netd
6558 root 20 0 0 0 0 I 0.3 0.0 0:00.06 [kworker/5:3]
25705 u0_a365 20 0 2.4G 205M 133M S 0.3 2.6 1:51.42 com.lite.lanxin
24441 u0_a152 30 10 8.2G 245M 183M S 0.3 3.2 1:31.13 com.tencent.mob+
...
各列含義:
列名 | 含義 |
PID | 進程 ID |
PR | 優先級 |
CPU% | 當前瞬間占用 CPU 百分比 |
S | 進程狀態(R=運行,S=睡眠,T=跟蹤/停止,Z=僵屍進程) |
#THR | 線程數 |
VSS | Virtual Set Size 虛擬耗用內存(包含共享庫占用的內存) |
RSS | Resident Set Size 實際使用物理內存(包含共享庫占用的內存) |
PCY | 調度策略優先級,SP_BACKGROUND/SPFOREGROUND |
UID | 進程所有者的用戶 ID |
NAME | 進程名 |
|
`top` 命令還支持一些命令行參數,詳細用法如下:
Usage: top [ -m max_procs ] [ -n iterations ] [ -d delay ] [ -s sort_column ] [ -t ] [ -h ]
-m num 最多顯示多少個進程
-n num 刷新多少次后退出
-d num 刷新時間間隔(單位秒,默認值 5)
-s col 按某列排序(可用 col 值:cpu, vss, rss, thr)
-t 顯示線程信息
-h 顯示幫助文檔
查看進程 UID
有兩種方案:
1. `adb shell dumpsys package
如:
$ adb shell dumpsys package org.mazhuang.guanggoo | grep userId=
userId=10394
2. 通過 ps 命令找到對應進程的 pid 之后 `adb shell cat /proc/
如:
$ adb shell
gemini:/ $ ps | grep org.mazhuang.guanggoo
u0_a394 28635 770 1795812 78736 SyS_epoll_ 0000000000 S org.mazhuang.guanggoo
gemini:/ $ cat /proc/28635/status | grep Uid
Uid: 10394 10394 10394 10394
gemini:/ $
其它
如下是其它常用命令的簡單描述,前文已經專門講過的命令不再額外說明:
命令 | 功能 |
cat | 顯示文件內容 |
cd | 切換目錄 |
chmod | 改變文件的存取模式/訪問權 |
df | 查看磁盤空間使用情況 |
grep | 過濾輸出 |
kill | 殺死指定 PID 的進程 |
ls | 列舉目錄內容 |
mount | 掛載目錄的查看和管理 |
mv | 查看正在運行的進程 |
ps | 查看正在運行的進程 |
rm | 刪除文件 |
top | 查看進程的資源占用情況 |
常見問題
啟動 adb server 失敗
**出錯提示**
error: protocol fault (couldn't read status): No error
**可能原因**
adb server 進程想使用的 5037 端口被占用。
**解決方案**
找到占用 5037 端口的進程,然后終止它。以 Windows 下為例:
netstat -ano | findstr LISTENING
TCP 0.0.0.0:5037 0.0.0.0:0 LISTENING 1548
這里 1548 即為進程 ID,用命令結束該進程:
taskkill /PID 1548
然后再啟動 adb 就沒問題了。
com.android.ddmlib.AdbCommandRejectedException
在 Android Studio 里新建一個模擬器,但是用 adb 一直連接不上,提示:
com.android.ddmlib.AdbCommandRejectedException: device unauthorized.
This adb server's $ADB_VENDOR_KEYS is not set
Try 'adb kill-server' if that seems wrong.
Otherwise check for a confirmation dialog on your device.
在手機上安裝一個終端然后執行 su 提示沒有該命令,這不正常。
於是刪除該模擬器后重新下載安裝一次,這次就正常了。