出发点一:
Android 引入了「任务栈」的概念,这个概念对于 Android 设备上的返回按键有极其重要的联系。有时候按下返回按钮或者点击关闭当前 Activity 的操作,都不知道 Android 系统会把程序带到哪个 Activity ,不确定这是否是最后一个 Activity 以致退出了整个程序。亦或者一些按钮和操作循环产生 Activity 而造成内存膨胀。对于这些问题,如果能够在调试期间知道当前任务栈的情况,就能很方便的观察和发现问题存在的原因,进而选择正确的 launchMode ,设置且当 Intent 的 Flag 来使程序达到预期的效果。
出发点二:
手机里装着一个应用,在不反编译也不做其他处理的情况下,你可以对这个应用了解多少。
出发点三:「出自官方文档」
dumpsys is a tool that runs on Android devices and provides information about system services. You can call dumpsys from the command line using the Android Debug Bridge (ADB) to get diagnostic output for all system services running on a connected device. This output is typically more verbose than you may want, so use the command line options described below to get output for only the system services you’re interested in. This page also describes how to use dumpsys to accomplish common tasks, such as inspecting input, RAM, battery, or network diagnostics.
命令格式:
adb shell dumpsys activity [options] [WHAT]
option 参数
options | 含义 |
---|---|
-a | 包括所有可用 Servier 状态 |
-c | 包括 Client 状态,即 App 端情况 |
-p PACKAGE | 限定输出指定包名 |
WHAT 参数
WHAT | 含义 |
---|---|
a[ctivities] | activity 状态 |
b[roadcasts] [PACKAGE_NAME] | broadcast 状态 |
s[ervices] [COMP_SPEC …] | service 状态 |
prov[iders] [COMP_SPEC …] | content provider 状态 |
p[rocesses][PACKAGE_NAME] | 进程状态 |
o[om] | 内存管理 |
i[ntents] [PACKAGE_NAME] | pending intent 状态 |
r[ecents] | 最近 activity |
perm[issions] | URI 授权情况 |
all | 所有 activities 信息 |
top | 顶部 activity 信息 |
package | package 相关信息 |
开始练习
练习一:
输入:
adb shell dumpsys activity
输出:
ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents) //注意一下
* PendingIntentRecord{1004eb1 com.tencent.mobileqq broadcastIntent}
* PendingIntentRecord{a9858c7 com.google.android.gms broadcastIntent}
* PendingIntentRecord{dc3ae3d com.tencent.mm startService}
// 省略 N 行 ...
ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts) //注意一下
Historical broadcasts [foreground]:
#0: BroadcastRecord{63c6dd9 u-1 android.hardware.usb.action.USB_STATE}
// 省略 N 行 ...
ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers) //注意一下
Published single-user content providers (by class):
* ContentProviderRecord{ef6fc1f u0 com.android.providers.telephony/.TelephonyProvider}
proc=ProcessRecord{efab720 3684:com.android.phone/1001}
singleton=true
authority=telephony
// 省略 N 行 ...
ACTIVITY MANAGER URI PERMISSIONS (dumpsys activity permissions) //注意一下
Granted Uri Permissions:
* UID 10129 holds:
UriPermission{b827fb4 0 @ content://downloads/all_downloads/125}
UriPermission{2b167dd 0 @ content://downloads/all_downloads/134}
UriPermission{51cbf52 0 @ content://downloads/all_downloads/148}
UriPermission{af4a223 0 @ content://downloads/all_downloads/169}
ACTIVITY MANAGER SERVICES (dumpsys activity services) //注意一下
User 0 active services:
* ServiceRecord{da1e320 u0 com.android.bluetooth/.hid.HidService}
app=null
created=-12d3h35m44s197ms started=false connections=1
// 省略 N 行 ...
ACTIVITY MANAGER RECENT TASKS (dumpsys activity recents) //注意一下
Recent tasks:
* Recent #0: TaskRecord{162657c #917 A=android.task.mms U=0 sz=1}
// 省略 N 行 ...
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities) //注意一下
Display #0 (activities from top to bottom):
// 省略 N 行 ...
ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes) //注意一下
Isolated process list (sorted by uid):
Isolated # 0: ProcessRecord{ffa12e5 3187:WebViewLoader-arm64-v8a/1037}
UID states:
UID 1000: UidRecord{15a01ba 1000 P / 19 procs}
// 省略 N 行 ...
上面标注「注意一下」的位置,其实是提示可以使用括号中的命令来获取此部分对应的内容:
所以「adb shell dumpsys activity」命令等价于依次输出下面 8 条命令:
adb shell dumpsys activity intents //主要输出 PendingIntentRecord
adb shell dumpsys activity broadcasts
adb shell dumpsys activity providers// Published single-user content providers (by class) / Published user 0 content providers (by class) /
adb shell dumpsys activity permissions
adb shell dumpsys activity services
adb shell dumpsys activity recents
adb shell dumpsys activity activities
adb shell dumpsys activity processes
练习二:
输入:
adb shell dumpsys activity top
输出:
TASK com.jianshu.haruki id=101
ACTIVITY com.jianshu.haruki/com.baiji.jianshu.MainActivity fb571c1 pid=21291
// ... 只举出了前两行,省略了 N 行代码
获取当前 Android 系统中与用户交互的 Activity 的详细信息,根据输出可得到简书的应用包名是:「com.jianshu .haruki」
练习三:
输入:
adb shell dumpsys activity activities
意义:
显示当前所有在运行的任务栈,它们的 id 分别是什么。对于每个 Task 也有 Activity 数量等信息,同时也列出了其中的 Activity 列表,并且对于每个 Activity 也有比较详细的描述,比如启动它的 Intent 的内容。如果觉得内容过多,只想看看栈的内容,也可以直接跳到「Running activities (most recent first) 那部分,比较简洁而又明了的列出了栈中的 Activity 列表,就能知道当按下返回键的时候会应该回到哪个 Activity 」
练习四:
输入:
adb shell dumpsys package com.jianshu.haruki[此处写的是『简书』的应用包名]
输出:
Activity Resolver Table: // 标注:这里开始解析「activity」标签
Full MIME Types:
text/plain:
28edf2a com.jianshu.haruki/com.baiji.jianshu.ui.editor.EditorActivityV19
Base MIME Types:
text:
28edf2a com.jianshu.haruki/com.baiji.jianshu.ui.editor.EditorActivityV19
Schemes:
jianshu: // 标注:列出了所有以 jianshu 为 scheme 的 Activity
3057264 com.jianshu.haruki/com.baiji.jianshu.ui.messages.submission.SubmissionDetailActivity
350011b com.jianshu.haruki/com.baiji.jianshu.MainActivity
96cc891 com.jianshu.haruki/com.baiji.jianshu.ui.user.collection.CollectionActivity (2 filters)
993c3f6 com.jianshu.haruki/com.baiji.jianshu.ui.user.userinfo.UserCenterActivity (2 filters)
f48e2b8 com.jianshu.haruki/com.baiji.jianshu.activity.NotifyDetailActivity
f845ccd com.jianshu.haruki/com.baiji.jianshu.ui.articledetail.ArticleDetailActivity
fe393f7 com.jianshu.haruki/com.baiji.jianshu.ui.user.notebook.normal.NotebookActivity (2 filters)
tencent100410602:
3302982 com.jianshu.haruki/com.tencent.tauth.AuthActivity
Non-Data Actions: // 标注:列出了所有有「intent-filter」标签但是无「data」标签的 Activity
android.intent.action.MAIN:
4b37893 com.jianshu.haruki/com.baiji.jianshu.ui.splash.SplashScreenActivity
com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY:
3d950d0 com.jianshu.haruki/haruki.jianshu.com.lib_share.weibo.WBShareActivity
android.intent.action.CREATE_SHORTCUT:
28edf2a com.jianshu.haruki/com.baiji.jianshu.ui.editor.EditorActivityV19
// 省略多行
MIME Typed Actions:
android.intent.action.SEND:
28edf2a com.jianshu.haruki/com.baiji.jianshu.ui.editor.EditorActivityV19
Receiver Resolver Table: // 标注:这里开始解析「receiver」标签
Schemes:
package: // 标注:列出了所有以 jianshu 为 scheme 的 receiver
c930aef com.jianshu.haruki/com.huawei.android.pushagent.PushEventReceiver
Non-Data Actions:// 标注:列出了所有含「intent-filter」标签但是无「action」标签的 receiver
com.xiaomi.mipush.MESSAGE_ARRIVED:
bcaa9fc com.jianshu.haruki/com.baiji.jianshu.ui.receiver.xiaomi.XiaoMiPushReceiver
android.intent.action.ACTION_POWER_DISCONNECTED:
9fd5885 com.jianshu.haruki/com.igexin.sdk.PushReceiver
// 省略多行
Service Resolver Table:// 标注:这里开始解析「service」标签
Non-Data Actions:// 标注:列出了所有含「intent-filter」标签但是无「action」标签的 service
com.google.firebase.INSTANCE_ID_EVENT:
649f732 com.jianshu.haruki/com.google.firebase.iid.FirebaseInstanceIdService
com.igexin.sdk.action.service.message:
97bac83 com.jianshu.haruki/com.igexin.sdk.PushService
Permissions:
Permission [com.jianshu.haruki.permission.C2D_MESSAGE] (88a8e00):
sourcePackage=com.jianshu.haruki
uid=10141 gids=null type=0 prot=signature
perm=Permission{e42a639 com.jianshu.haruki.permission.C2D_MESSAGE}
packageSetting=PackageSetting{a1c547e com.jianshu.haruki/10141}
Permission [getui.permission.GetuiService.com.jianshu.haruki] (7428ddf):
sourcePackage=com.jianshu.haruki
uid=10141 gids=null type=0 prot=normal
perm=Permission{80a8a2c getui.permission.GetuiService.com.jianshu.haruki}
packageSetting=PackageSetting{a1c547e com.jianshu.haruki/10141}
Permission [com.jianshu.haruki.permission.MIPUSH_RECEIVE] (c9e6cf5):
sourcePackage=com.jianshu.haruki
uid=10141 gids=null type=0 prot=signature
perm=Permission{f253a8a com.jianshu.haruki.permission.MIPUSH_RECEIVE}
packageSetting=PackageSetting{a1c547e com.jianshu.haruki/10141}
Registered ContentProviders:
com.jianshu.haruki/com.google.firebase.provider.FirebaseInitProvider:
Provider{b6c28fb com.jianshu.haruki/com.google.firebase.provider.FirebaseInitProvider}
com.jianshu.haruki/com.baiji.jianshu.db.core.JianshuProvider:
Provider{eac1d18 com.jianshu.haruki/com.baiji.jianshu.db.core.JianshuProvider}
com.jianshu.haruki/com.sensorsdata.analytics.android.sdk.SensorsDataContentProvider:
Provider{8b5e371 com.jianshu.haruki/com.sensorsdata.analytics.android.sdk.SensorsDataContentProvider}
ContentProvider Authorities:
[com.jianshu.haruki.SensorsDataContentProvider]:
Provider{8b5e371 com.jianshu.haruki/com.sensorsdata.analytics.android.sdk.SensorsDataContentProvider}
applicationInfo=ApplicationInfo{b247556 com.jianshu.haruki}
[com.jianshu.haruki.firebaseinitprovider]:
Provider{b6c28fb com.jianshu.haruki/com.google.firebase.provider.FirebaseInitProvider}
applicationInfo=ApplicationInfo{b247556 com.jianshu.haruki}
[com.jianshu.haruki.provider]:
Provider{eac1d18 com.jianshu.haruki/com.baiji.jianshu.db.core.JianshuProvider}
applicationInfo=ApplicationInfo{b247556 com.jianshu.haruki}
Key Set Manager:
[com.jianshu.haruki]
Signing KeySets: 67
Packages:
Package [com.jianshu.haruki] (a1c547e):
userId=10141
pkg=Package{ca59d7 com.jianshu.haruki}
codePath=/data/app/com.jianshu.haruki-2
resourcePath=/data/app/com.jianshu.haruki-2
legacyNativeLibraryDir=/data/app/com.jianshu.haruki-2/lib
primaryCpuAbi=arm64-v8a
secondaryCpuAbi=null
versionCode=2017082915 targetSdk=23
versionName=2.6.0
splits=[base]
applicationInfo=ApplicationInfo{b247556 com.jianshu.haruki}
flags=[ HAS_CODE ALLOW_CLEAR_USER_DATA LARGE_HEAP ]
dataDir=/data/user/0/com.jianshu.haruki
supportsScreens=[small, medium, large, xlarge, resizeable, anyDensity]
timeStamp=2017-09-22 15:39:43
firstInstallTime=2017-09-22 15:38:20
lastUpdateTime=2017-09-22 15:40:02
signatures=PackageSignatures{d1f2c4 [8f445ad]}
installPermissionsFixed=true installStatus=1
pkgFlags=[ HAS_CODE ALLOW_CLEAR_USER_DATA LARGE_HEAP ]
declared permissions:
getui.permission.GetuiService.com.jianshu.haruki: prot=normal, INSTALLED
com.jianshu.haruki.permission.MIPUSH_RECEIVE: prot=signature, INSTALLED
com.jianshu.haruki.permission.C2D_MESSAGE: prot=signature, INSTALLED
requested permissions:
android.permission.INTERNET
android.permission.WRITE_EXTERNAL_STORAGE
// 省略多行
install permissions:
com.jianshu.haruki.permission.C2D_MESSAGE: granted=true
com.google.android.c2dm.permission.RECEIVE: granted=true
android.permission.MANAGE_ACCOUNTS: granted=true
getui.permission.GetuiService.com.jianshu.haruki: granted=true
// 省略多行
User 0: installed=true hidden=false stopped=false notLaunched=false enabled=0
gids=[3002, 3003, 3001]
runtime permissions:
android.permission.READ_EXTERNAL_STORAGE: granted=true
android.permission.READ_PHONE_STATE: granted=true
android.permission.WRITE_EXTERNAL_STORAGE: granted=true
disabledComponents:
com.huawei.android.pushagent.PushBootReceiver
练习五:
输入:
adb shell dumpsys meminfo com.jianshu.haruki[此处写的是『简书』的应用包名]
输出:
Applications Memory Usage (kB):
Uptime: 94384953 Realtime: 367426401
** MEMINFO in pid 21291 [com.jianshu.haruki] **
Pss Private Private Swapped Heap Heap Heap
Total Dirty Clean Dirty Size Alloc Free
------ ------ ------ ------ ------ ------ ------
Native Heap 20236 19432 0 1180 50580 48838 1741
Dalvik Heap 12299 12264 0 56 52530 45197 7333
Dalvik Other 23104 23064 0 9680
Stack 1484 1484 0 0
Ashmem 134 128 0 0
Gfx dev 14698 14596 0 0
Other dev 10 0 8 0
.so mmap 5924 252 4312 2908
.apk mmap 27189 144 26692 0
.ttf mmap 130 0 104 0
.dex mmap 12242 12 11060 0
.oat mmap 3661 0 1256 4
.art mmap 2450 1940 92 144
Other mmap 1300 12 1280 0
EGL mtrack 41280 41280 0 0
Unknown 6052 6044 0 84
TOTAL 172193 120652 44804 14056 103110 94035 9074
App Summary
Pss(KB)
------
Java Heap: 14296
Native Heap: 19432
Code: 43832
Stack: 1484
Graphics: 55876
Private Other: 30536
System: 6737
TOTAL: 172193 TOTAL SWAP (KB): 14056
Objects
Views: 837 ViewRootImpl: 1
AppContexts: 2 Activities: 1
Assets: 4 AssetManagers: 2
Local Binders: 31 Proxy Binders: 31
Parcel memory: 23 Parcel count: 92
Death Recipients: 1 OpenSSL Sockets: 2
SQL
MEMORY_USED: 1054
PAGECACHE_OVERFLOW: 297 MALLOC_SIZE: 62
DATABASES
pgsz dbsz Lookaside(b) cache Dbname
4 24 36 112/19/5 /data/user/0/com.jianshu.haruki/databases/com.jianshu.haruki
4 76 176 116/64/25 /data/user/0/com.jianshu.haruki/databases/google_app_measurement.db
4 40 33 6/21/5 /data/user/0/com.jianshu.haruki/databases/tencent_analysis.db
4 32 98 75/26/12 /data/user/0/com.jianshu.haruki/databases/bugly_db_
4 40 19 4/19/3 /data/user/0/com.jianshu.haruki/databases/pri_tencent_analysis.db
4 20 34 3/18/4 /data/user/0/com.jianshu.haruki/databases/ThrowalbeLog.db
4 20 32 786/19/5 /data/user/0/com.jianshu.haruki/databases/sharesdk.db
4 124 57 45/24/10 /data/user/0/com.jianshu.haruki/databases/jian_shu.db
command | description |
---|---|
adb shell dumpsys activity | 获取当前 Android 系统 Activity 栈中 Activity 信息 |
adb shell dumpsys activity top | 获取当前 Android 系统 中与用户交互的 Activity 的详细信息 |
adb shell dumpsys meminfo [应用包名] | 查看应用的内存使用情况 |
adb shell dumpsys package [应用报名] | 获取手机里面某个 apk 的应用信息、版本信息 |
adb shell dumpsys activity activities | 显示当前所有在运行的任务栈,并可查看栈中所有的 Activity 的列表 |
参考链接:
官方文档,这里有比较详细的文档介绍: https://developer.android.com/studio/command-line/dumpsys.html
开发必备—你应该知道的一些 ADB 命令 http://www.jianshu.com/p/0693b841c83b
dumpsys 命令用法 http://gityuan.com/2017/07/04/ams_dumpsys/