zoukankan      html  css  js  c++  java
  • Android ADB 实用总结

    一、背景

    从系统架构上来说,Android是基于Linux系统基础上,做了进一步的定制与修改,并融入了自身的特有功能,且向应用层提供应用程序接口,供开发者使用。系统内核层面,主体依然是Linux内核。因此,以往的Linux系统上的开发、使用和经验,在Android系统上很大程度上还是适用的。

    Android应用程序的开发与运行通常处于不同的设备环境,开发端在开发应用程序的过程中,往往涉及到对应用程序运行时的调试。同时,Android设备在运行过程中,需要有类似于Linux系统本身的一套机制,通过直接从另外一端连接后,就能直接在对应的权限范围内,进行系统监测与管理。也就是说,无论是Android应用程序本身的开发调试过程,还是对Android系统的管理,都需要一套机制,以方面外界可以直接触达到Android系统运行时本身。


    二、adb

    adb,全称Android Debug Bridge,看名字就知道,这是一个用来进行Android开发调试的桥接工具。Android一般在PC端进行开发,最终在手机等设备上运行。自然的,此处桥接的两端一端是开发端(或调试端,如PC端),另一端是运行端(如手机端)。两端桥接后,我们可以从开发端直接通过adb命令的方式,将指令传递到运行端,并得以执行。

    实际使用过程中,adb的命令发出,往往通过开发端的命令行工具,如iTerm2等。adb命令都是adb ...形式,系统会自动搜索环境变量下的adb命令实体。一般情况下,开发过程中我们都已经将其路径配置到了环境变量中。

    另一种常见adb启动的方式是,当我们打开Android Studio,AS进程会自动拉起adb进程(如果此时adb进程未被启动),而不需要人为的去执行如adb start-server命令。AS进程会定时去检测adb进程是否被拉起,如果发现adb进程被kill,一定时间后会再次拉起adb进程。也就也是我们在进行Android开发时,往往都不需要手动去启动adb的原因。

    执行adb命令,搜索adb实体位置:

    ~ where adb
    复制代码

    输出结果:

    /Users/corn/Library/Android/sdk/platform-tools/adb
    复制代码

    显然,adb是在Android sdk中的platform-tools目录下。

    此时,我们先搜索下本机adb进程。

    ~ ps -l|head -1;ps -A -l | grep "adb"
      UID   PID  PPID        F CPU PRI NI       SZ    RSS WCHAN     S             ADDR TTY           TIME CMD
      501 61501     1     4044   0  31  0  4301988   2944 -      Ss                  0 ??         0:00.72 adb -L tcp:5037 fork-server server --reply-fd 6
    复制代码

    此进程对应的即为adb server进程,并且,默认情况下监听的是5037端口。 我们可以进一步验证下:

    ~ lsof -i | grep adb
    复制代码

    输出结果:

    adb       63248 corn    6u  IPv4 0xf7434ef5de677a91      0t0  TCP localhost:5037->localhost:56924 (ESTABLISHED)
    adb       63248 corn   10u  IPv4 0xf7434ef5de55ba91      0t0  TCP localhost:5037 (LISTEN)
    复制代码

    查找端口56924对应的进程信息:

    ~ lsof -i :56924
    复制代码

    输出:

    COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
    adb     63248 corn    6u  IPv4 0xf7434ef5de677a91      0t0  TCP localhost:5037->localhost:56924 (ESTABLISHED)
    studio  64007 corn  559u  IPv4 0xf7434ef5b85f2d51      0t0  TCP localhost:56924->localhost:5037 (ESTABLISHED)
    复制代码

    可以看出,adb server与studio通过 TCP的方式建立了连接,当然了,此连接是双向的。

    接下来我们确认下当前正在监听5037端口的进程:

    ~ lsof -i :5037
    复制代码

    输出:

    COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
    adb     63248 corn    6u  IPv4 0xf7434ef5de677a91      0t0  TCP localhost:5037->localhost:56924 (ESTABLISHED)
    adb     63248 corn   10u  IPv4 0xf7434ef5de55ba91      0t0  TCP localhost:5037 (LISTEN)
    studio  64007 corn  559u  IPv4 0xf7434ef5b85f2d51      0t0  TCP localhost:56924->localhost:5037 (ESTABLISHED)
    复制代码

    再一次确认了5037端口已经成功被PID为63248的adb server进程监听。

    接下来打开iTerm2,输入命令:

    ~ adb shell
    复制代码

    此时,再次搜索adb进程:

    ps -l|head -1;ps -A -l | grep "adb"
    复制代码

    输出:

    UID   PID  PPID        F CPU PRI NI       SZ    RSS WCHAN     S             ADDR TTY           TIME CMD
    501 63248     1     4044   0  46  0  4304064   3780 -      Ss                  0 ??         0:03.24 adb -L tcp:5037 fork-server server --reply-fd 6
    501 67543 66192     4006   0  31  0  4293948   1988 -      S+                  0 ttys000    0:00.01 adb shell
    复制代码

    显然,我们发现,此时多了一个adb shell的进程,PID为67543。 执行:

    ~ lsof -i:5037
    复制代码

    输出:

    COMMAND     PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
    adb       63248 corn    6u  IPv4 0xf7434ef5de677a91      0t0  TCP localhost:5037->localhost:56924 (ESTABLISHED)
    adb       63248 corn   10u  IPv4 0xf7434ef5de55ba91      0t0  TCP localhost:5037 (LISTEN)
    adb       63248 corn   11u  IPv4 0xf7434ef5c026e3f1      0t0  TCP localhost:5037->localhost:57780 (ESTABLISHED)
    studio    64007 corn  559u  IPv4 0xf7434ef5b85f2d51      0t0  TCP localhost:56924->localhost:5037 (ESTABLISHED)
    adb       67543 corn    5u  IPv4 0xf7434ef5b3a0ea91      0t0  TCP localhost:57780->localhost:5037 (ESTABLISHED)
    复制代码

    这意味着,针对同一个adb server,我们可以启动对应的多个adb client进程与之对应。adb client与adb server之间,是通过TCP建立的双向连接的。

    通过iTerm2等命令行工具,直接执行adb命令,实际上,启动的只是adb client,adb client内部,会自动判断adb server进程是否存在,存在则建立连接,否则需要先拉起adb server进程。

    至此,开发端adb client及adb server进程启动完毕。

    同样的,在手机等设备上,默认会存在一个demon级别的adb服务进程,我们可以查找下对应名称。

    ~ adb shell ps -l|head -1;adb shell ps -A -l |grep "adb"
    复制代码

    输出结果:

    F S   UID   PID  PPID C PRI  NI BIT    SZ WCHAN  TTY          TIME CMD
    4 R  2000  2636     1 0  19   0   -  6787 0      ?        00:00:00 adbd
    复制代码

    可以看到,手机上adb server的进程名称为:adbd

    开发端的adb server通过TCP/USB,与手机端的adbd进行连接,并相互通信。

    整体上,abd通信流程如下(下图来自网络):

    adb命令为开发端与设备端交互提供了非常方便的操作入口,如最常见的安装apk文件,从手机上获取文件到本机等。更为重要的是,adb为我们提供了直接进入终端shell环境的入口。


    三、adb shell

    adb为开发端与设备端建立了桥接,那么,通过adb链接到设备端后,我们可以类似于本机用命令行工具执行shell命令一样,去直接执行设备的shell命令。对应的操作命令通常adb shell打头。

    当然了,为了使用中方便,我们往往先执行adb shell,直接进入到设备端shell环境,然后操作跟本机一样了。

    ~ adb shell
    OP4A57:/ $
    复制代码

    例如:

    1|OP4A57:/ $ ls  -al
    复制代码

    直接输出对应的根目录的所有文件列表。

    如果需要从设备shell环境中退出,直接执行exit即可。

    adb shell一方面可以执行大量的Linux命令,另一方面,基于Android系统,封装了大量实用的Android相关的命令接口。

    adb shell命令的实体,存储在/system/bin/目录,我们可以先进去看看。

    OP4A57:/ $ cd system/bin
    OP4A57:/system/bin $ ls -al
    复制代码

    显示:

    ls: ./adbd: Permission denied
    ls: ./asserttip: Permission denied
    ls: ./atlasservice: Permission denied
    ls: ./audioserver: Permission denied
    ls: ./autochmod.sh: Permission denied
    ls: ./blank_screen: Permission denied
    ls: ./blkid: Permission denied
    ls: ./bootanimation: Permission denied
    ls: ./bootstat: Permission denied
    ls: ./bpfloader: Permission denied
    ls: ./br_app_data_service: Permission denied
    ls: ./bspCriticalLog: Permission denied
    ls: ./bspFwUpdate: Permission denied
    ls: ./bspatch: Permission denied
    ls: ./bt_logger: Permission denied
    ls: ./cachebench_eng: Permission denied
    ls: ./cameraserver: Permission denied
    ls: ./chcon: Permission denied
    ls: ./clatd: Permission denied
    ls: ./common_dcs: Permission denied
    ls: ./cpustress_eng: Permission denied
    ls: ./criticallog: Permission denied
    ls: ./datafree: Permission denied
    ls: ./dexoptanalyzer: Permission denied
    ls: ./dnsmasq: Permission denied
    ls: ./dpmd: Permission denied
    ls: ./drmserver: Permission denied
    ls: ./dumpstate: Permission denied
    ls: ./dun-server: Permission denied
    ls: ./e2fsck: Permission denied
    ls: ./e2fsdroid: Permission denied
    ls: ./engineer_system_shell.sh: Permission denied
    ls: ./fsck.f2fs: Permission denied
    ls: ./fsck_msdos: Permission denied
    ls: ./gatekeeperd: Permission denied
    ls: ./healthd: Permission denied
    ls: ./hwservicemanager: Permission denied
    ls: ./hypnusd: Permission denied
    ls: ./idmap: Permission denied
    ls: ./incident_helper: Permission denied
    ls: ./incidentd: Permission denied
    ls: ./install-recovery.sh: Permission denied
    ls: ./installd: Permission denied
    ls: ./junklogcollector: Permission denied
    ls: ./keystore: Permission denied
    ls: ./lmkd: Permission denied
    ls: ./logd: Permission denied
    ls: ./make_f2fs: Permission denied
    ls: ./mdnsd: Permission denied
    ls: ./mediadrmserver: Permission denied
    ls: ./mediaextractor: Permission denied
    ls: ./mediametrics: Permission denied
    ls: ./mediaserver: Permission denied
    ls: ./memtester_eng: Permission denied
    ls: ./mke2fs: Permission denied
    ls: ./mmi: Permission denied
    ls: ./mmi_diag: Permission denied
    ls: ./motorcontrol: Permission denied
    ls: ./mtpd: Permission denied
    ls: ./neo: Permission denied
    ls: ./netd: Permission denied
    ls: ./netutils-wrapper-1.0: Permission denied
    ls: ./oae_server: Permission denied
    ls: ./oiface: Permission denied
    ls: ./omedia: Permission denied
    ls: ./oppo_kevent: Permission denied
    ls: ./perfservice: Permission denied
    ls: ./pppd: Permission denied
    ls: ./profman: Permission denied
    ls: ./quickboot: Permission denied
    ls: ./qvrservice: Permission denied
    ls: ./racoon: Permission denied
    ls: ./resize_ext4: Permission denied
    ls: ./rutilsdaemon: Permission denied
    ls: ./screenrecord: Permission denied
    ls: ./sdcard: Permission denied
    ls: ./servicemanager: Permission denied
    ls: ./sgdisk: Permission denied
    ls: ./sload_f2fs: Permission denied
    ls: ./smcinvoked: Permission denied
    ls: ./statsd: Permission denied
    ls: ./storaged: Permission denied
    ls: ./stressapptest_eng: Permission denied
    ls: ./surfaceflinger: Permission denied
    ls: ./thermalserviced: Permission denied
    ls: ./tombstoned: Permission denied
    ls: ./traced: Permission denied
    ls: ./traced_probes: Permission denied
    ls: ./tune2fs: Permission denied
    ls: ./uncrypt: Permission denied
    ls: ./usbd: Permission denied
    ls: ./vdc: Permission denied
    ls: ./vold: Permission denied
    ls: ./vold_prepare_subdirs: Permission denied
    ls: ./wait_for_keymaster: Permission denied
    ls: ./wfdservice: Permission denied
    ls: ./wificond: Permission denied
    ls: ./wififtmtest: Permission denied
    ls: ./wifirftest: Permission denied
    total 19528
    drwxr-xr-x  3 root   shell     8192 2019-10-16 00:52 .
    drwxr-xr-x 19 root   root      4096 2019-10-16 00:52 ..
    -rwxr-xr-x  1 root   shell   965003 2019-10-16 00:52 QMESA_64
    -rwxr-xr-x  1 root   shell  5646592 2019-10-16 00:52 SecrecyAutoUnlocker
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 acpi -> toybox
    -rwxr-xr-x  1 root   shell      207 2019-10-16 00:52 am
    lrwxr-xr-x  1 root   shell       13 2019-10-16 00:52 app_process -> app_process64
    -rwxr-xr-x  1 root   shell    33148 2019-10-16 00:52 app_process32
    -rwxr-xr-x  1 root   shell    68976 2019-10-16 00:52 app_process64
    -rwxr-xr-x  1 root   shell   136184 2019-10-16 00:52 applypatch
    -rwxr-xr-x  1 root   shell       33 2019-10-16 00:52 appops
    -rwxr-xr-x  1 root   shell      232 2019-10-16 00:52 appwidget
    -rwxr-xr-x  1 root   shell    69736 2019-10-16 00:52 atrace
    -rwxr-xr-x  1 root   shell   203576 2019-10-16 00:52 awk
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 base64 -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 basename -> toybox
    -rwxr-xr-x  1 root   shell    69672 2019-10-16 00:52 bcc
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 blockdev -> toybox
    -rwxr-xr-x  1 root   shell      216 2019-10-16 00:52 bmgr
    -rwxr-xr-x  1 root   shell      173 2019-10-16 00:52 bu
    -rwxr-xr-x  1 root   shell    68352 2019-10-16 00:52 bugreport
    -rwxr-xr-x  1 root   shell    68360 2019-10-16 00:52 bugreportz
    lrwxr-xr-x  1 root   shell        5 2019-10-16 00:52 bunzip2 -> bzip2
    lrwxr-xr-x  1 root   shell        5 2019-10-16 00:52 bzcat -> bzip2
    -rwxr-xr-x  1 root   shell    68808 2019-10-16 00:52 bzip2
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 cal -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 cat -> toybox
    -rwxr-xr-x  1 root   shell    68360 2019-10-16 00:52 checkfutexwait
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 chgrp -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 chmod -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 chown -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 chroot -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 chrt -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 cksum -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 clear -> toybox
    -rwxr-xr-x  1 root   shell    69224 2019-10-16 00:52 cmd
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 cmp -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 comm -> toybox
    -rwxr-xr-x  1 root   shell      224 2019-10-16 00:52 content
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 cp -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 cpio -> toybox
    -rwxr-xr-x  1 root   shell   114676 2019-10-16 00:52 crash_dump32
    -rwxr-xr-x  1 root   shell   136544 2019-10-16 00:52 crash_dump64
    -rwxr-xr-x  1 root   shell   534216 2019-10-16 00:52 curl
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 cut -> toybox
    lrwxr-xr-x  1 root   shell       10 2019-10-16 00:52 dalvikvm -> dalvikvm64
    -rwxr-xr-x  1 root   shell    28576 2019-10-16 00:52 dalvikvm32
    -rwxr-xr-x  1 root   shell    68520 2019-10-16 00:52 dalvikvm64
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 date -> toybox
    lrwxr-xr-x  1 root   shell        7 2019-10-16 00:52 dd -> toolbox
    -rwxr-xr-x  1 root   shell    68520 2019-10-16 00:52 debuggerd
    -rwxr-xr-x  1 root   shell   550676 2019-10-16 00:52 dex2oat
    -rwxr-xr-x  1 root   shell    68648 2019-10-16 00:52 dexdiag
    -rwxr-xr-x  1 root   shell   138384 2019-10-16 00:52 dexdump
    -rwxr-xr-x  1 root   shell    68800 2019-10-16 00:52 dexlist
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 df -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 diff -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 dirname -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 dmesg -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 dos2unix -> toybox
    -rwxr-xr-x  1 root   shell      173 2019-10-16 00:52 dpm
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 du -> toybox
    -rwxr-xr-x  1 root   shell    69328 2019-10-16 00:52 dumpsys
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 echo -> toybox
    lrwxr-xr-x  1 root   shell        4 2019-10-16 00:52 egrep -> grep
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 env -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 expand -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 expr -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 fallocate -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 false -> toybox
    lrwxr-xr-x  1 root   shell        4 2019-10-16 00:52 fgrep -> grep
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 file -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 find -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 flock -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 fmt -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 free -> toybox
    -rwxr-xr-x  1 root   shell    69000 2019-10-16 00:52 fsck_exfat
    -rwxr-xr-x  1 root   shell       17 2019-10-16 00:52 get-cplc
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 getenforce -> toybox
    lrwxr-xr-x  1 root   shell        7 2019-10-16 00:52 getevent -> toolbox
    lrwxr-xr-x  1 root   shell        7 2019-10-16 00:52 getprop -> toolbox
    -rwxr-xr-x  1 root   shell    70352 2019-10-16 00:52 grep
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 groups -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 gunzip -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 gzip -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 head -> toybox
    -rwxr-xr-x  1 root   shell      213 2019-10-16 00:52 hid
    -rwxr-xr-x  1 root   shell  1401616 2019-10-16 00:52 host_manager_11ad
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 hostname -> toybox
    drwxr-xr-x  2 root   shell     4096 2019-10-16 00:52 hw
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 hwclock -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 id -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 ifconfig -> toybox
    -rwxr-xr-x  1 root   shell       48 2019-10-16 00:52 ime
    -rwxr-xr-x  1 root   shell    68992 2019-10-16 00:52 incident
    -rwxr-xr-x  1 root   shell     2511 2019-10-16 00:52 init.oppo.bluetooth.sh
    -rwxr-xr-x  1 root   shell     2254 2019-10-16 00:52 init.oppo.wifiAutoRecovery.sh
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 inotifyd -> toybox
    -rwxr-xr-x  1 root   shell      220 2019-10-16 00:52 input
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 insmod -> toybox
    -rwxr-xr-x  1 root   shell    68336 2019-10-16 00:52 invoke_test_client
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 ionice -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 iorenice -> toybox
    -rwxr-xr-x  1 root   shell    69480 2019-10-16 00:52 iotop
    -rwxrwxrwx  1 system system  268152 2019-10-16 00:52 iozone
    -rwxr-xr-x  1 root   shell   399400 2019-10-16 00:52 ip
    lrwxr-xr-x  1 root   shell       20 2019-10-16 00:52 ip-wrapper-1.0 -> netutils-wrapper-1.0
    -rwxr-xr-x  1 root   shell   501240 2019-10-16 00:52 ip6tables
    lrwxr-xr-x  1 root   shell        9 2019-10-16 00:52 ip6tables-restore -> ip6tables
    lrwxr-xr-x  1 root   shell        9 2019-10-16 00:52 ip6tables-save -> ip6tables
    lrwxr-xr-x  1 root   shell       20 2019-10-16 00:52 ip6tables-wrapper-1.0 -> netutils-wrapper-1.0
    -rwxr-xr-x  1 root   shell   500216 2019-10-16 00:52 iptables
    lrwxr-xr-x  1 root   shell        8 2019-10-16 00:52 iptables-restore -> iptables
    lrwxr-xr-x  1 root   shell        8 2019-10-16 00:52 iptables-save -> iptables
    lrwxr-xr-x  1 root   shell       20 2019-10-16 00:52 iptables-wrapper-1.0 -> netutils-wrapper-1.0
    -rwxr-xr-x  1 root   shell   345400 2019-10-16 00:52 iw
    -rwxr-xr-x  1 root   shell    69280 2019-10-16 00:52 iwlist
    -rwxr-xr-x  1 root   shell    68752 2019-10-16 00:52 iwpriv
    -rwxr-xr-x  1 root   shell    83088 2019-10-16 00:52 keystore_cli_v2
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 kill -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 killall -> toybox
    -rwxr-xr-x  1 root   shell   814160 2019-10-16 00:52 ld.mc
    -rwxr-xr-x  1 root   shell  1251892 2019-10-16 00:52 linker
    -rwxr-xr-x  1 root   shell  1812280 2019-10-16 00:52 linker64
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 linker_asan -> linker
    lrwxr-xr-x  1 root   shell        8 2019-10-16 00:52 linker_asan64 -> linker64
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 ln -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 load_policy -> toybox
    -rwxr-xr-x  1 root   shell      211 2019-10-16 00:52 locksettings
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 log -> toybox
    -rwxr-xr-x  1 root   shell    68248 2019-10-16 00:52 logcat
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 logname -> toybox
    -rwxr-xr-x  1 root   shell    68472 2019-10-16 00:52 logwrapper
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 losetup -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 ls -> toybox
    -rwxr-xr-x  1 root   shell    68280 2019-10-16 00:52 lshal
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 lsmod -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 lsof -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 lspci -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 lsusb -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 md5sum -> toybox
    -rwxr-xr-x  1 root   shell      227 2019-10-16 00:52 media
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 microcom -> toybox
    -rwxr-xr-x  1 root   shell    68408 2019-10-16 00:52 minidumpreader
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 mkdir -> toybox
    -rwxr-xr-x  1 root   shell    74912 2019-10-16 00:52 mkexfat
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 mkfifo -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 mkfs.ext2 -> mke2fs
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 mkfs.ext3 -> mke2fs
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 mkfs.ext4 -> mke2fs
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 mknod -> toybox
    -rwxr-xr-x  1 root   shell   135352 2019-10-16 00:52 mkntfs
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 mkswap -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 mktemp -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 modinfo -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 modprobe -> toybox
    -rwxr-xr-x  1 root   shell      268 2019-10-16 00:52 monkey
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 more -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 mount -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 mountpoint -> toybox
    -rwxr-xr-x  1 root   shell      439 2019-10-16 00:52 move_time_data.sh
    -rwxr-xr-x  1 root   shell      506 2019-10-16 00:52 move_widevine_data.sh
    -rwxr-xr-x  1 root   shell     2548 2019-10-16 00:52 move_wifi_data.sh
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 mv -> toybox
    -rwxr-xr-x  1 root   shell    68904 2019-10-16 00:52 ndc
    lrwxr-xr-x  1 root   shell       20 2019-10-16 00:52 ndc-wrapper-1.0 -> netutils-wrapper-1.0
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 netstat -> toybox
    lrwxr-xr-x  1 root   shell        7 2019-10-16 00:52 newfs_msdos -> toolbox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 nice -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 nl -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 nohup -> toybox
    -rwxr-xr-x  1 root   shell   336576 2019-10-16 00:52 ntfs-3g
    -rwxr-xr-x  1 root   shell   201936 2019-10-16 00:52 ntfsfix
    -rwxr-xr-x  1 root   shell   269440 2019-10-16 00:52 oatdump
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 od -> toybox
    -rwxr-xr-x  1 root   shell    68344 2019-10-16 00:52 oppopnscrcmd
    -rwxr-xr-x  1 root   shell    68376 2019-10-16 00:52 opposensortest
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 paste -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 patch -> toybox
    -rwxr-xr-x  1 root   shell    67036 2019-10-16 00:52 patchoat
    -rwxr-xr-x  1 root   shell   284744 2019-10-16 00:52 perfetto
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 pgrep -> toybox
    -rwxr-xr-x  1 root   shell    68696 2019-10-16 00:52 phoenix_log_manager
    -rwxr-xr-x  1 root   shell    15786 2019-10-16 00:52 phoenix_log_native_helper.sh
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 pidof -> toybox
    -rwxr-xr-x  1 root   shell    69208 2019-10-16 00:52 ping
    -rwxr-xr-x  1 root   shell    69776 2019-10-16 00:52 ping6
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 pkill -> toybox
    -rwxr-xr-x  1 root   shell       34 2019-10-16 00:52 pm
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 pmap -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 printenv -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 printf -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 ps -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 pwd -> toybox
    -rwxr-xr-x  1 root   shell   112904 2019-10-16 00:52 qvrservicetest
    -rwxr-xr-x  1 root   shell   135640 2019-10-16 00:52 qvrservicetest64
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 readlink -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 realpath -> toybox
    -rwxr-xr-x  1 root   shell    68392 2019-10-16 00:52 reboot
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 renice -> toybox
    -rwxr-xr-x  1 root   shell      205 2019-10-16 00:52 requestsync
    -rwxr-xr-x  1 root   shell    68896 2019-10-16 00:52 resize2fs
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 restorecon -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 rm -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 rmdir -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 rmmod -> toybox
    -rwxr-xr-x  1 root   shell    33016 2019-10-16 00:52 rtspclient
    -rwxr-xr-x  1 root   shell    32992 2019-10-16 00:52 rtspserver
    -rwxr-x---  1 root   shell    68392 2019-10-16 00:52 run-as
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 runcon -> toybox
    -rwxr-xr-x  1 root   shell    68336 2019-10-16 00:52 schedtest
    -rwxr-xr-x  1 root   shell    68568 2019-10-16 00:52 screencap
    -rwxr-xr-x  1 root   shell    68768 2019-10-16 00:52 secdiscard
    -rwx------  1 root   root    340312 2019-10-16 00:52 secilc
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 sed -> toybox
    -rwxr-xr-x  1 root   shell    68752 2019-10-16 00:52 self-init-system
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 sendevent -> toybox
    -rwxr-xr-x  1 root   shell    68408 2019-10-16 00:52 sensorservice
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 seq -> toybox
    -rwxr-xr-x  1 root   shell    68792 2019-10-16 00:52 service
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 setenforce -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 setprop -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 setsid -> toybox
    -rwxr-xr-x  1 root   shell       35 2019-10-16 00:52 settings
    -rwxr-xr-x  1 root   shell   401184 2019-10-16 00:52 sh
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 sha1sum -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 sha224sum -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 sha256sum -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 sha384sum -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 sha512sum -> toybox
    -rwxr-xr-x  1 root   shell    68736 2019-10-16 00:52 shutdown_log_back
    -rwxr-xr-x  1 root   shell     3591 2019-10-16 00:52 shutdown_log_native_helper.sh
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 sleep -> toybox
    -rwxr-xr-x  1 root   shell      207 2019-10-16 00:52 sm
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 sort -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 split -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 start -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 stat -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 stop -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 strings -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 stty -> toybox
    -rwxr-xr-x  1 root   shell      209 2019-10-16 00:52 svc
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 swapoff -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 swapon -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 sync -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 sysctl -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 tac -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 tail -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 tar -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 taskset -> toybox
    -rwxr-xr-x  1 root   shell   136368 2019-10-16 00:52 tc
    lrwxr-xr-x  1 root   shell       20 2019-10-16 00:52 tc-wrapper-1.0 -> netutils-wrapper-1.0
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 tee -> toybox
    -rwxr-xr-x  1 root   shell      189 2019-10-16 00:52 telecom
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 time -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 timeout -> toybox
    -rwxr-xr-x  1 root   shell    68488 2019-10-16 00:52 tinycap
    -rwxr-xr-x  1 root   shell    68656 2019-10-16 00:52 tinymix
    -rwxr-xr-x  1 root   shell    68448 2019-10-16 00:52 tinypcminfo
    -rwxr-xr-x  1 root   shell    68400 2019-10-16 00:52 tinyplay
    -rwxr-xr-x  1 root   shell    68416 2019-10-16 00:52 tinyplay_ftm
    -rwxr-xr-x  1 root   shell   145960 2019-10-16 00:52 toolbox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 top -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 touch -> toybox
    -rwxr-xr-x  1 root   shell   473824 2019-10-16 00:52 toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 tr -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 true -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 truncate -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 tty -> toybox
    -rwxr-xr-x  1 root   shell    62888 2019-10-16 00:52 tunman
    -rwxr-xr-x  1 root   shell    68648 2019-10-16 00:52 tzdatacheck
    -rwxr-xr-x  1 root   shell     4173 2019-10-16 00:52 uiautomator
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 ulimit -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 umount -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 uname -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 uniq -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 unix2dos -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 uptime -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 usleep -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 uudecode -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 uuencode -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 vmstat -> toybox
    -rwxr-xr-x  1 root   shell      169 2019-10-16 00:52 vr
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 wc -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 which -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 whoami -> toybox
    -rwxr-xr-x  1 root   shell      217 2019-10-16 00:52 wifihwftm
    -rwxr-xr-x  1 root   shell   201320 2019-10-16 00:52 wigig_wiburn
    -rwxr-xr-x  1 root   shell       33 2019-10-16 00:52 wm
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 xargs -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 xxd -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 yes -> toybox
    lrwxr-xr-x  1 root   shell        6 2019-10-16 00:52 zcat -> toybox
    复制代码

    我们发现,除了部分命令实体因为权限问题无法显示外,其中有大量的命令实体列出,并且对一般用户是具有r-x权限的。对于具有x权限的命令实体,我们都是可以按照需要去执行的。每个命令实体都具有特定的用途。

    接下来将总结下比较常用的一些命令实体。

    3.1 dumpsys命令

    adb shell中,这个命令应该是所有Android开发用的最多的一个命令了。
    其中,最常见的又非下面这条莫属。

    1|OP4A57:/ $ dumpsys activity | grep "mResumedActivity"
    复制代码

    这个命令可酷了,一次性将当前手机窗口上的App包名和Activity名称显示出来。

    mResumedActivity: ActivityRecord{47b4848 u0 com.sankuai.meituan/com.meituan.android.pt.homepage.activity.MainActivity t277}
    复制代码

    于是,美团的包名为:
    com.sankuai.meituan
    当前页面的Activity为:
    com.meituan.android.pt.homepage.activity.MainActivity

    这个命令经常用在我们开发调试的时候用到,例如找一个bug,需要定位当前Activity等。更进一步,如果直接执行

    dumpsys activity
    复制代码

    输出的结果中,会有许多非常有用的信息。例如:

    ACTIVITY MANAGER SETTINGS
    ....
    ACTIVITY MANAGER PENDING INTENTS
    ....
    ACTIVITY MANAGER BROADCAST STATE
    ....
    ACTIVITY MANAGER CONTENT PROVIDERS
    ....
    ACTIVITY MANAGER URI PERMISSIONS
    ....
    ACTIVITY MANAGER SERVICES
    ....
    ACTIVITY MANAGER RECENT TASKS
    ....
    ACTIVITY MANAGER LAST ANR
    ....
    ACTIVITY MANAGER STARTER
    ....
    ACTIVITY MANAGER ACTIVITIES
    ....
    ACTIVITY MANAGER RUNNING PROCESSES
    ....
    复制代码

    可以看到,输出的信息中包括了activity、Service、BroadcastReceiver、ContentProvider等四大基本组件外,还包括了如进程、身份权限、ANR、Intent等各大主体信息,且activity中还包括了各运行的app的TaskRecord及对应的ActivityRecord信息,这在处理带有复杂的启动模式或intent flag下的Activity跳转,需要分析Activity栈时非常有用。

    我们发现,命名执行的是dumpsys activity,结果却输出了含有activity之外的许多信息,这已经超出了我们原本对activity的认识。其实,dumpsys activity,此处有点类似ActivityManagerServiceAMS也是包含了对其他组件的处理的。

    更进一步,这个命令中的参数activity,我们是如何知道的呢,除了activity,还有哪些其他参数呢?
    最简单的,就是查看对应命令的帮助说明,执行:

    OP4A57:/ $ dumpsys --help
    复制代码

    输出:

    usage: dumpsys
             To dump all services.
    or:
           dumpsys [-t TIMEOUT] [--priority LEVEL] [--help | -l | --skip SERVICES | SERVICE [ARGS]]
             --help: shows this help
             -l: only list services, do not dump them
             -t TIMEOUT_SEC: TIMEOUT to use in seconds instead of default 10 seconds
             -T TIMEOUT_MS: TIMEOUT to use in milliseconds instead of default 10 seconds
             --proto: filter services that support dumping data in proto format. Dumps               will be in proto format.
             --priority LEVEL: filter services based on specified priority
                   LEVEL must be one of CRITICAL | HIGH | NORMAL
             --skip SERVICES: dumps all services but SERVICES (comma-separated list)
             SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it
    复制代码

    显然,帮助说明中的-l命令,可以列出所有的services名称。并且,dumpsys命令的作用,就是用来输出对应services详细信息的。

    运行:

    OP4A57:/ $ dumpsys -l
    复制代码

    输出:

    Currently running services:
      AtlasService
      DockObserver
      MinkBinderSvc
      OPPO
      OPPOExService
      OppoRoundCornerService
      SurfaceFlinger
      accessibility
      account
      activity
      alarm
      alipay
      android.security.keystore
      appops
      appwidget
      athenaservice
      audio
      backup
      battery
      batteryproperties
      batterystats
      binder_calls_stats
      bluetooth_manager
      carrier_config
      clipboard
      cneservice
      color_screenshot
      com.qualcomm.location.izat.IzatService
      common_dcs
      commontime_management
      companiondevice
      connectivity
      connmetrics
      consumer_ir
      content
      contexthub
      country_detector
      cpuinfo
      critical.log
      crossprofileapps
      dbinfo
      device_identifiers
      device_policy
      deviceidle
      devicestoragemonitor
      diskstats
      display
      dpmservice
      dreams
      drm.drmManager
      dropbox
      engineer
      ethernet
      extphone
      face
      fingerprint
      gfxinfo
      gpu
      graphicsstats
      hardware_properties
      hypnus
      hypnusd
      imms
      input
      input_method
      iphonesubinfo
      ipsec
      isms
      isub
      jobscheduler
      launcherapps
      location
      lock_settings
      luckymoney
      media.audio_flinger
      media.audio_policy
      media.camera
      media.camera.proxy
      media.drm
      media.extractor
      media.metrics
      media.player
      media.resource_manager
      media.sound_trigger_hw
      media_projection
      media_resource_monitor
      media_router
      media_session
      meminfo
      midi
      motor
      motor.control
      mount
      multimediaDaemon
      neoservice
      netd_listener
      netpolicy
      netstats
      network_management
      network_score
      network_time_update_service
      network_watchlist
      neuronsystem
      nfc
      notification
      nwdiagnose
      oae
      oem_lock
      oemlinklatency
      oiface
      omedia
      oppo.com.IRUtils
      oppo.junklog
      oppocustomize
      otadexopt
      overlay
      package
      package_native
      permission
      persistent_data_block
      phone
      pinner
      power
      power_monitor
      print
      processinfo
      procstats
      qti.ims.ext
      recovery
      restrictions
      scheduling_policy
      search
      sec_key_att_app_id_provider
      secrecy
      secure_element
      sensorservice
      serial
      servicediscovery
      settings
      shortcut
      simphonebook
      sip
      slice
      soundtrigger
      stats
      statscompanion
      statusbar
      storaged
      storaged_pri
      storagestats
      system_update
      telecom
      telephony.registry
      textclassification
      textservices
      thermalservice
      trust
      uimode
      updatelock
      usage
      usagestats
      usb
      user
      vendor.perfservice
      vibrator
      voiceinteraction
      wallpaper
      webviewupdate
      wifi
      wificond
      wifip2p
      wifiscanner
      window
    复制代码

    输出的结果中,activity作为了services类型中的一种,赫然在列。但除了activity,还有很多其他的services类型,如display、package、battery、wifi等等。每一种services类型,都具有自身的详细信息,这些信息,在特定需求场景下是非常有用的。

    例如:

    OP4A57:/ $ dumpsys display
    ....
    Display Devices: size=1
      DisplayDeviceInfo{"内置屏幕": uniqueId="local:0", 1080 x 2400, modeId 1, defaultModeId 1, supportedModes [{id=1, width=1080, height=2400, fps=61.000004}], colorMode 0, supportedColorModes [0, 7, 9], HdrCapabilities android.view.Display$HdrCapabilities@7d47da60, density 480, 403.411 x 401.052 dpi, appVsyncOff 1000000, presDeadline 16393442, touch INTERNAL, rotation 0, type BUILT_IN, state OFF, FLAG_DEFAULT_DISPLAY, FLAG_ROTATES_WITH_CONTENT, FLAG_SECURE, FLAG_SUPPORTS_PROTECTED_BUFFERS}
    ....
    复制代码

    dumpsys display可以显示屏幕相关的详细信息。

    dumpsys package可以显示系统中各安装包及对应的安装包信息。

    OP4A57:/ $ dumpsys package | grep meituan
    ....
      [com.sankuai.meituan.ShareFileProvider]:
        Provider{dbcd881 com.sankuai.meituan/com.sankuai.android.share.provider.ShareFileProvider}
          applicationInfo=ApplicationInfo{da22029 com.sankuai.meituan}
      [com.sankuai.meituan]
      Package [com.sankuai.meituan] (f1d4026):
        pkg=Package{320c8ba com.sankuai.meituan}
        codePath=/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ==
        resourcePath=/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ==
        legacyNativeLibraryDir=/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ==/lib
        applicationInfo=ApplicationInfo{da22029 com.sankuai.meituan}
        dataDir=/data/user/0/com.sankuai.meituan
          com.sankuai.meituan.permission.MIPUSH_RECEIVE: prot=signature, INSTALLED
          com.sankuai.meituan.push.permission.MESSAGE: prot=signature, INSTALLED
          com.sankuai.meituan.permission.C2D_MESSAGE: prot=signature, INSTALLED
          com.sankuai.meituan.permission.C2D_MESSAGE: granted=true
          com.sankuai.meituan.push.permission.MESSAGE: granted=true
          com.sankuai.meituan.permission.MIPUSH_RECEIVE: granted=true
        seq=36, package=com.sankuai.meituan
        com.sankuai.meituan/com.meituan.android.quickpass.manage.lib.service.ApduService: android.permission.BIND_NFC_SERVICE
        com.sankuai.meituan/com.dianping.oppopush.OPPOPushService: com.coloros.mcs.permission.SEND_MCS_MESSAGE
      [com.sankuai.meituan]
        path: /data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ==/base.apk
    ....
    复制代码

    再来看一个,比较有意思的。

    dumpsys package
    复制代码

    其中部分信息输出如下:

      Package [com.didi.es.psngr] (ed0be86):
        userId=10195
        pkg=Package{8324cfd com.didi.es.psngr}
        codePath=/data/app/com.didi.es.psngr-TI2cYenR9Sf-xgF8oghSSQ==
        resourcePath=/data/app/com.didi.es.psngr-TI2cYenR9Sf-xgF8oghSSQ==
        legacyNativeLibraryDir=/data/app/com.didi.es.psngr-TI2cYenR9Sf-xgF8oghSSQ==/lib
        primaryCpuAbi=armeabi-v7a
        secondaryCpuAbi=null
        versionCode=119 minSdk=18 targetSdk=26
        versionName=2.6.1
        splits=[base]
        apkSigningVersion=3
        applicationInfo=ApplicationInfo{6069f42 com.didi.es.psngr}
        flags=[ HAS_CODE ALLOW_CLEAR_USER_DATA ]
        privateFlags=[ PRIVATE_FLAG_ACTIVITIES_RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION ]
        dataDir=/data/user/0/com.didi.es.psngr
        supportsScreens=[small, medium, large, xlarge, resizeable, anyDensity]
        usesLibraries:
          org.apache.http.legacy
        usesLibraryFiles:
          /system/framework/org.apache.http.legacy.boot.jar
        timeStamp=2019-11-07 21:04:40
        firstInstallTime=2019-11-07 21:04:41
        lastUpdateTime=2019-11-07 21:04:41
        installerPackageName=com.oppo.market
        signatures=PackageSignatures{98e0df2 version:3, signatures:[16b402b6], past signatures:[177dbb98 flags: 17, 16b402b6 flags: 17]}
        installPermissionsFixed=true
        pkgFlags=[ HAS_CODE ALLOW_CLEAR_USER_DATA ]
        declared permissions:
          android.permission.GET_TASKS: prot=normal
          com.didi.es.login.permission.broadcast: prot=normal, INSTALLED
          com.didi.es.psngr.permission.MIPUSH_RECEIVE: prot=signature, INSTALLED
          getui.permission.GetuiService.com.didi.es.psngr: prot=normal, INSTALLED
        install permissions:
          getui.permission.GetuiService.com.didi.es.psngr: granted=true
          android.permission.MODIFY_AUDIO_SETTINGS: granted=true
          android.permission.MANAGE_ACCOUNTS: granted=true
          com.didi.es.login.permission.broadcast: granted=true
          android.permission.CHANGE_NETWORK_STATE: granted=true
          android.permission.RECEIVE_BOOT_COMPLETED: granted=true
          android.permission.BLUETOOTH: granted=true
          android.permission.GET_TASKS: granted=true
          android.permission.INTERNET: granted=true
          android.permission.BLUETOOTH_ADMIN: granted=true
          android.permission.ACCESS_LOCATION_EXTRA_COMMANDS: granted=true
          android.permission.BROADCAST_STICKY: granted=true
          android.permission.CHANGE_WIFI_STATE: granted=true
          android.permission.FLASHLIGHT: granted=true
          android.permission.ACCESS_NETWORK_STATE: granted=true
          android.permission.DISABLE_KEYGUARD: granted=true
          com.didi.es.psngr.permission.MIPUSH_RECEIVE: granted=true
          android.permission.VIBRATE: granted=true
          android.permission.ACCESS_WIFI_STATE: granted=true
          com.android.launcher.permission.INSTALL_SHORTCUT: granted=true
          android.permission.WAKE_LOCK: granted=true
        User 0: ceDataInode=2883956 installed=true hidden=false suspended=false stopped=true notLaunched=false enabled=2 ofs=2 instant=false virtual=false
          lastDisabledCaller: com.coloros.safecenter
          gids=[3002, 3003, 3001]
          runtime permissions:
            android.permission.ACCESS_FINE_LOCATION: granted=true
            android.permission.READ_EXTERNAL_STORAGE: granted=true
            android.permission.READ_PHONE_STATE: granted=true
            android.permission.WRITE_EXTERNAL_STORAGE: granted=true
          enabledComponents:
            com.xiaomi.push.service.XMPushService
    复制代码

    这里面,这些信息就比较有意思了。

    firstInstallTime - app首次安装时间
    lastUpdateTime - app最近更新时间
    installerPackageName - app从哪个应用市场下载的 以及,其后面的runtime permissions运行时权限授予情况等。

    dumpsys battery显示电量信息。

    OP4A57:/ $ dumpsys battery
    Current OPPO Battery Service state:
      Charger voltage : 4930
      Battery current : -338
      ChargerTechnology: 1
      ChargeFastCharger: false
      PlugType: 2
      UpdatesStopped: false
      UsbHwStatus: 0
    
    Current Battery Service state:
      AC powered: false
      USB powered: true
      Wireless powered: false
      Max charging current: 500000
      Max charging voltage: 5000000
      Charge counter: 50
      status: 2
      health: 2
      present: true
      level: 96
      scale: 100
      voltage: 4385
      temperature: 305
      technology: Li-ion
      mUsbStatus: 0
      PhoneTemp: 329
    复制代码

    dumpsys命令是在太强大了,只能用霸气来形容。


    3.2 am命令

    同样的,am命令可厉害了。如果说dumpsys命令侧重在于显示信息,am命令则侧重于与App的直接交互。例如,开发调试时,发一个广播,启动一个服务,启动activity,强行停止与包关联的所有进程,设置设备属性等等。

    不举例子了,具体直接看帮助说明。

    255|OP4A57:/ $ am  help
    Activity manager (activity) commands:
      help
          Print this help text.
      start-activity [-D] [-N] [-W] [-P <FILE>] [--start-profiler <FILE>]
              [--sampling INTERVAL] [--streaming] [-R COUNT] [-S]
              [--track-allocation] [--user <USER_ID> | current] <INTENT>
          Start an Activity.  Options are:
          -D: enable debugging
          -N: enable native debugging
          -W: wait for launch to complete
          --start-profiler <FILE>: start profiler and send results to <FILE>
          --sampling INTERVAL: use sample profiling with INTERVAL microseconds
              between samples (use with --start-profiler)
          --streaming: stream the profiling output to the specified file
              (use with --start-profiler)
          -P <FILE>: like above, but profiling stops when app goes idle
          --attach-agent <agent>: attach the given agent before binding
          --attach-agent-bind <agent>: attach the given agent during binding
          -R: repeat the activity launch <COUNT> times.  Prior to each repeat,
              the top activity will be finished.
          -S: force stop the target app before starting the activity
          --track-allocation: enable tracking of object allocations
          --user <USER_ID> | current: Specify which user to run as; if not
              specified then run as the current user.
          --windowingMode <WINDOWING_MODE>: The windowing mode to launch the activity into.
          --activityType <ACTIVITY_TYPE>: The activity type to launch the activity as.
      start-service [--user <USER_ID> | current] <INTENT>
          Start a Service.  Options are:
          --user <USER_ID> | current: Specify which user to run as; if not
              specified then run as the current user.
      start-foreground-service [--user <USER_ID> | current] <INTENT>
          Start a foreground Service.  Options are:
          --user <USER_ID> | current: Specify which user to run as; if not
              specified then run as the current user.
      stop-service [--user <USER_ID> | current] <INTENT>
          Stop a Service.  Options are:
          --user <USER_ID> | current: Specify which user to run as; if not
              specified then run as the current user.
      broadcast [--user <USER_ID> | all | current] <INTENT>
          Send a broadcast Intent.  Options are:
          --user <USER_ID> | all | current: Specify which user to send to; if not
              specified then send to all users.
          --receiver-permission <PERMISSION>: Require receiver to hold permission.
      instrument [-r] [-e <NAME> <VALUE>] [-p <FILE>] [-w]
              [--user <USER_ID> | current] [--no-hidden-api-checks]
              [--no-window-animation] [--abi <ABI>] <COMPONENT>
          Start an Instrumentation.  Typically this target <COMPONENT> is in the
          form <TEST_PACKAGE>/<RUNNER_CLASS> or only <TEST_PACKAGE> if there
          is only one instrumentation.  Options are:
          -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT).  Use with
              [-e perf true] to generate raw output for performance measurements.
          -e <NAME> <VALUE>: set argument <NAME> to <VALUE>.  For test runners a
              common form is [-e <testrunner_flag> <value>[,<value>...]].
          -p <FILE>: write profiling data to <FILE>
          -m: Write output as protobuf to stdout (machine readable)
          -f <Optional PATH/TO/FILE>: Write output as protobuf to a file (machine
              readable). If path is not specified, default directory and file name will
              be used: /sdcard/instrument-logs/log-yyyyMMdd-hhmmss-SSS.instrumentation_data_proto
          -w: wait for instrumentation to finish before returning.  Required for
              test runners.
          --user <USER_ID> | current: Specify user instrumentation runs in;
              current user if not specified.
          --no-hidden-api-checks: disable restrictions on use of hidden API.
          --no-window-animation: turn off window animations while running.
          --abi <ABI>: Launch the instrumented process with the selected ABI.
              This assumes that the process supports the selected ABI.
      trace-ipc [start|stop] [--dump-file <FILE>]
          Trace IPC transactions.
          start: start tracing IPC transactions.
          stop: stop tracing IPC transactions and dump the results to file.
          --dump-file <FILE>: Specify the file the trace should be dumped to.
      profile [start|stop] [--user <USER_ID> current] [--sampling INTERVAL]
              [--streaming] <PROCESS> <FILE>
          Start and stop profiler on a process.  The given <PROCESS> argument
            may be either a process name or pid.  Options are:
          --user <USER_ID> | current: When supplying a process name,
              specify user of process to profile; uses current user if not specified.
          --sampling INTERVAL: use sample profiling with INTERVAL microseconds
              between samples
          --streaming: stream the profiling output to the specified file
      dumpheap [--user <USER_ID> current] [-n] [-g] <PROCESS> <FILE>
          Dump the heap of a process.  The given <PROCESS> argument may
            be either a process name or pid.  Options are:
          -n: dump native heap instead of managed heap
          -g: force GC before dumping the heap
          --user <USER_ID> | current: When supplying a process name,
              specify user of process to dump; uses current user if not specified.
      set-debug-app [-w] [--persistent] <PACKAGE>
          Set application <PACKAGE> to debug.  Options are:
          -w: wait for debugger when application starts
          --persistent: retain this value
      clear-debug-app
          Clear the previously set-debug-app.
      set-watch-heap <PROCESS> <MEM-LIMIT>
          Start monitoring pss size of <PROCESS>, if it is at or
          above <HEAP-LIMIT> then a heap dump is collected for the user to report.
      clear-watch-heap
          Clear the previously set-watch-heap.
      bug-report [--progress | --telephony]
          Request bug report generation; will launch a notification
            when done to select where it should be delivered. Options are:
         --progress: will launch a notification right away to show its progress.
         --telephony: will dump only telephony sections.
      force-stop [--user <USER_ID> | all | current] <PACKAGE>
          Completely stop the given application package.
      crash [--user <USER_ID>] <PACKAGE|PID>
          Induce a VM crash in the specified package or process
      kill [--user <USER_ID> | all | current] <PACKAGE>
          Kill all background processes associated with the given application.
      kill-all
          Kill all processes that are safe to kill (cached, etc).
      make-uid-idle [--user <USER_ID> | all | current] <PACKAGE>
          If the given application's uid is in the background and waiting to
          become idle (not allowing background services), do that now.
      monitor [--gdb <port>]
          Start monitoring for crashes or ANRs.
          --gdb: start gdbserv on the given port at crash/ANR
      watch-uids [--oom <uid>]
          Start watching for and reporting uid state changes.
          --oom: specify a uid for which to report detailed change messages.
      hang [--allow-restart]
          Hang the system.
          --allow-restart: allow watchdog to perform normal system restart
      restart
          Restart the user-space system.
      idle-maintenance
          Perform idle maintenance now.
      screen-compat [on|off] <PACKAGE>
          Control screen compatibility mode of <PACKAGE>.
      package-importance <PACKAGE>
          Print current importance of <PACKAGE>.
      to-uri [INTENT]
          Print the given Intent specification as a URI.
      to-intent-uri [INTENT]
          Print the given Intent specification as an intent: URI.
      to-app-uri [INTENT]
          Print the given Intent specification as an android-app: URI.
      switch-user <USER_ID>
          Switch to put USER_ID in the foreground, starting
          execution of that user if it is currently stopped.
      get-current-user
          Returns id of the current foreground user.
      start-user <USER_ID>
          Start USER_ID in background if it is currently stopped;
          use switch-user if you want to start the user in foreground
      unlock-user <USER_ID> [TOKEN_HEX]
          Attempt to unlock the given user using the given authorization token.
      stop-user [-w] [-f] <USER_ID>
          Stop execution of USER_ID, not allowing it to run any
          code until a later explicit start or switch to it.
          -w: wait for stop-user to complete.
          -f: force stop even if there are related users that cannot be stopped.
      is-user-stopped <USER_ID>
          Returns whether <USER_ID> has been stopped or not.
      get-started-user-state <USER_ID>
          Gets the current state of the given started user.
      track-associations
          Enable association tracking.
      untrack-associations
          Disable and clear association tracking.
      get-uid-state <UID>
          Gets the process state of an app given its <UID>.
      attach-agent <PROCESS> <FILE>
        Attach an agent to the specified <PROCESS>, which may be either a process name or a PID.
      get-config [--days N] [--device] [--proto]
          Retrieve the configuration and any recent configurations of the device.
          --days: also return last N days of configurations that have been seen.
          --device: also output global device configuration info.
          --proto: return result as a proto; does not include --days info.
      supports-multiwindow
          Returns true if the device supports multiwindow.
      supports-split-screen-multi-window
          Returns true if the device supports split screen multiwindow.
      suppress-resize-config-changes <true|false>
          Suppresses configuration changes due to user resizing an activity/task.
      set-inactive [--user <USER_ID>] <PACKAGE> true|false
          Sets the inactive state of an app.
      get-inactive [--user <USER_ID>] <PACKAGE>
          Returns the inactive state of an app.
      set-standby-bucket [--user <USER_ID>] <PACKAGE> active|working_set|frequent|rare
          Puts an app in the standby bucket.
      get-standby-bucket [--user <USER_ID>] <PACKAGE>
          Returns the standby bucket of an app.
      send-trim-memory [--user <USER_ID>] <PROCESS>
              [HIDDEN|RUNNING_MODERATE|BACKGROUND|RUNNING_LOW|MODERATE|RUNNING_CRITICAL|COMPLETE]
          Send a memory trim event to a <PROCESS>.  May also supply a raw trim int level.
      display [COMMAND] [...]: sub-commands for operating on displays.
           move-stack <STACK_ID> <DISPLAY_ID>
               Move <STACK_ID> from its current display to <DISPLAY_ID>.
      stack [COMMAND] [...]: sub-commands for operating on activity stacks.
           start <DISPLAY_ID> <INTENT>
               Start a new activity on <DISPLAY_ID> using <INTENT>
           move-task <TASK_ID> <STACK_ID> [true|false]
               Move <TASK_ID> from its current stack to the top (true) or
               bottom (false) of <STACK_ID>.
           resize <STACK_ID> <LEFT,TOP,RIGHT,BOTTOM>
               Change <STACK_ID> size and position to <LEFT,TOP,RIGHT,BOTTOM>.
           resize-animated <STACK_ID> <LEFT,TOP,RIGHT,BOTTOM>
               Same as resize, but allow animation.
           resize-docked-stack <LEFT,TOP,RIGHT,BOTTOM> [<TASK_LEFT,TASK_TOP,TASK_RIGHT,TASK_BOTTOM>]
               Change docked stack to <LEFT,TOP,RIGHT,BOTTOM>
               and supplying temporary different task bounds indicated by
               <TASK_LEFT,TOP,RIGHT,BOTTOM>
           move-top-activity-to-pinned-stack: <STACK_ID> <LEFT,TOP,RIGHT,BOTTOM>
               Moves the top activity from
               <STACK_ID> to the pinned stack using <LEFT,TOP,RIGHT,BOTTOM> for the
               bounds of the pinned stack.
           positiontask <TASK_ID> <STACK_ID> <POSITION>
               Place <TASK_ID> in <STACK_ID> at <POSITION>
           list
               List all of the activity stacks and their sizes.
           info <WINDOWING_MODE> <ACTIVITY_TYPE>
               Display the information about activity stack in <WINDOWING_MODE> and <ACTIVITY_TYPE>.
           remove <STACK_ID>
               Remove stack <STACK_ID>.
      task [COMMAND] [...]: sub-commands for operating on activity tasks.
           lock <TASK_ID>
               Bring <TASK_ID> to the front and don't allow other tasks to run.
           lock stop
               End the current task lock.
           resizeable <TASK_ID> [0|1|2|3]
               Change resizeable mode of <TASK_ID> to one of the following:
               0: unresizeable
               1: crop_windows
               2: resizeable
               3: resizeable_and_pipable
           resize <TASK_ID> <LEFT,TOP,RIGHT,BOTTOM>
               Makes sure <TASK_ID> is in a stack with the specified bounds.
               Forces the task to be resizeable and creates a stack if no existing stack
               has the specified bounds.
      update-appinfo <USER_ID> <PACKAGE_NAME> [<PACKAGE_NAME>...]
          Update the ApplicationInfo objects of the listed packages for <USER_ID>
          without restarting any processes.
      write
          Write all pending state to storage.
    
    <INTENT> specifications include these flags and arguments:
        [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
        [-c <CATEGORY> [-c <CATEGORY>] ...]
        [-n <COMPONENT_NAME>]
        [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
        [--esn <EXTRA_KEY> ...]
        [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
        [--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
        [--el <EXTRA_KEY> <EXTRA_LONG_VALUE> ...]
        [--ef <EXTRA_KEY> <EXTRA_FLOAT_VALUE> ...]
        [--eu <EXTRA_KEY> <EXTRA_URI_VALUE> ...]
        [--ecn <EXTRA_KEY> <EXTRA_COMPONENT_NAME_VALUE>]
        [--eia <EXTRA_KEY> <EXTRA_INT_VALUE>[,<EXTRA_INT_VALUE...]]
            (mutiple extras passed as Integer[])
        [--eial <EXTRA_KEY> <EXTRA_INT_VALUE>[,<EXTRA_INT_VALUE...]]
            (mutiple extras passed as List<Integer>)
        [--ela <EXTRA_KEY> <EXTRA_LONG_VALUE>[,<EXTRA_LONG_VALUE...]]
            (mutiple extras passed as Long[])
        [--elal <EXTRA_KEY> <EXTRA_LONG_VALUE>[,<EXTRA_LONG_VALUE...]]
            (mutiple extras passed as List<Long>)
        [--efa <EXTRA_KEY> <EXTRA_FLOAT_VALUE>[,<EXTRA_FLOAT_VALUE...]]
            (mutiple extras passed as Float[])
        [--efal <EXTRA_KEY> <EXTRA_FLOAT_VALUE>[,<EXTRA_FLOAT_VALUE...]]
            (mutiple extras passed as List<Float>)
        [--esa <EXTRA_KEY> <EXTRA_STRING_VALUE>[,<EXTRA_STRING_VALUE...]]
            (mutiple extras passed as String[]; to embed a comma into a string,
             escape it using "\,")
        [--esal <EXTRA_KEY> <EXTRA_STRING_VALUE>[,<EXTRA_STRING_VALUE...]]
            (mutiple extras passed as List<String>; to embed a comma into a string,
             escape it using "\,")
        [-f <FLAG>]
        [--grant-read-uri-permission] [--grant-write-uri-permission]
        [--grant-persistable-uri-permission] [--grant-prefix-uri-permission]
        [--debug-log-resolution] [--exclude-stopped-packages]
        [--include-stopped-packages]
        [--activity-brought-to-front] [--activity-clear-top]
        [--activity-clear-when-task-reset] [--activity-exclude-from-recents]
        [--activity-launched-from-history] [--activity-multiple-task]
        [--activity-no-animation] [--activity-no-history]
        [--activity-no-user-action] [--activity-previous-is-top]
        [--activity-reorder-to-front] [--activity-reset-task-if-needed]
        [--activity-single-top] [--activity-clear-task]
        [--activity-task-on-home] [--activity-match-external]
        [--receiver-registered-only] [--receiver-replace-pending]
        [--receiver-foreground] [--receiver-no-abort]
        [--receiver-include-background]
        [--selector]
        [<URI> | <PACKAGE> | <COMPONENT>]
    复制代码

    强大如斯。


    3.3 pm命令

    pm命令侧重于安装包管理,包括安装包的信息查看与处理。如输出安装包路径,清除app数据,查看权限详情等信息。

    先来看看帮助文档说明:

    OP4A57:/ $ pm help
    Package manager (package) commands:
      help
        Print this help text.
    
      path [--user USER_ID] PACKAGE
        Print the path to the .apk of the given PACKAGE.
    
      dump PACKAGE
        Print various system state associated with the given PACKAGE.
    
      list features
        Prints all features of the system.
    
      has-feature FEATURE_NAME [version]
        Prints true and returns exit status 0 when system has a FEATURE_NAME,
        otherwise prints false and returns exit status 1
    
      list instrumentation [-f] [TARGET-PACKAGE]
        Prints all test packages; optionally only those targeting TARGET-PACKAGE
        Options:
          -f: dump the name of the .apk file containing the test package
    
      list libraries
        Prints all system libraries.
    
      list packages [-f] [-d] [-e] [-s] [-3] [-i] [-l] [-u] [-U]
          [--uid UID] [--user USER_ID] [FILTER]
        Prints all packages; optionally only those whose name contains
        the text in FILTER.  Options are:
          -f: see their associated file
          -d: filter to only show disabled packages
          -e: filter to only show enabled packages
          -s: filter to only show system packages
          -3: filter to only show third party packages
          -i: see the installer for the packages
          -l: ignored (used for compatibility with older releases)
          -U: also show the package UID
          -u: also include uninstalled packages
          --uid UID: filter to only show packages with the given UID
          --user USER_ID: only list packages belonging to the given user
    
      list permission-groups
        Prints all known permission groups.
    
      list permissions [-g] [-f] [-d] [-u] [GROUP]
        Prints all known permissions; optionally only those in GROUP.  Options are:
          -g: organize by group
          -f: print all information
          -s: short summary
          -d: only list dangerous permissions
          -u: list only the permissions users will see
    
      resolve-activity [--brief] [--components] [--user USER_ID] INTENT
        Prints the activity that resolves to the given INTENT.
    
      query-activities [--brief] [--components] [--user USER_ID] INTENT
        Prints all activities that can handle the given INTENT.
    
      query-services [--brief] [--components] [--user USER_ID] INTENT
        Prints all services that can handle the given INTENT.
    
      query-receivers [--brief] [--components] [--user USER_ID] INTENT
        Prints all broadcast receivers that can handle the given INTENT.
    
      install [-lrtsfdg] [-i PACKAGE] [--user USER_ID|all|current]
           [-p INHERIT_PACKAGE] [--install-location 0/1/2]
           [--originating-uri URI] [---referrer URI]
           [--abi ABI_NAME] [--force-sdk]
           [--preload] [--instantapp] [--full] [--dont-kill]
           [--force-uuid internal|UUID] [--pkg PACKAGE] [-S BYTES] [PATH|-]
        Install an application.  Must provide the apk data to install, either as a
        file path or '-' to read from stdin.  Options are:
          -l: forward lock application
          -R: disallow replacement of existing application
          -t: allow test packages
          -i: specify package name of installer owning the app
          -s: install application on sdcard
          -f: install application on internal flash
          -d: allow version code downgrade (debuggable packages only)
          -p: partial application install (new split on top of existing pkg)
          -g: grant all runtime permissions
          -S: size in bytes of package, required for stdin
          --user: install under the given user.
          --dont-kill: installing a new feature split, don't kill running app
          --originating-uri: set URI where app was downloaded from
          --referrer: set URI that instigated the install of the app
          --pkg: specify expected package name of app being installed
          --abi: override the default ABI of the platform
          --instantapp: cause the app to be installed as an ephemeral install app
          --full: cause the app to be installed as a non-ephemeral full app
          --install-location: force the install location:
              0=auto, 1=internal only, 2=prefer external
          --force-uuid: force install on to disk volume with given UUID
          --force-sdk: allow install even when existing app targets platform
              codename but new one targets a final API level
    
      install-create [-lrtsfdg] [-i PACKAGE] [--user USER_ID|all|current]
           [-p INHERIT_PACKAGE] [--install-location 0/1/2]
           [--originating-uri URI] [---referrer URI]
           [--abi ABI_NAME] [--force-sdk]
           [--preload] [--instantapp] [--full] [--dont-kill]
           [--force-uuid internal|UUID] [--pkg PACKAGE] [-S BYTES]
        Like "install", but starts an install session.  Use "install-write"
        to push data into the session, and "install-commit" to finish.
    
      install-write [-S BYTES] SESSION_ID SPLIT_NAME [PATH|-]
        Write an apk into the given install session.  If the path is '-', data
        will be read from stdin.  Options are:
          -S: size in bytes of package, required for stdin
    
      install-commit SESSION_ID
        Commit the given active install session, installing the app.
    
      install-abandon SESSION_ID
        Delete the given active install session.
    
      set-install-location LOCATION
        Changes the default install location.  NOTE this is only intended for debugging;
        using this can cause applications to break and other undersireable behavior.
        LOCATION is one of:
        0 [auto]: Let system decide the best location
        1 [internal]: Install on internal device storage
        2 [external]: Install on external media
    
      get-install-location
        Returns the current install location: 0, 1 or 2 as per set-install-location.
    
      move-package PACKAGE [internal|UUID]
    
      move-primary-storage [internal|UUID]
    
      pm uninstall [-k] [--user USER_ID] [--versionCode VERSION_CODE] PACKAGE [SPLIT]
        Remove the given package name from the system.  May remove an entire app
        if no SPLIT name is specified, otherwise will remove only the split of the
        given app.  Options are:
          -k: keep the data and cache directories around after package removal.
          --user: remove the app from the given user.
          --versionCode: only uninstall if the app has the given version code.
    
      clear [--user USER_ID] PACKAGE
        Deletes all data associated with a package.
    
      enable [--user USER_ID] PACKAGE_OR_COMPONENT
      disable [--user USER_ID] PACKAGE_OR_COMPONENT
      disable-user [--user USER_ID] PACKAGE_OR_COMPONENT
      disable-until-used [--user USER_ID] PACKAGE_OR_COMPONENT
      default-state [--user USER_ID] PACKAGE_OR_COMPONENT
        These commands change the enabled state of a given package or
        component (written as "package/class").
    
      hide [--user USER_ID] PACKAGE_OR_COMPONENT
      unhide [--user USER_ID] PACKAGE_OR_COMPONENT
    
      suspend [--user USER_ID] TARGET-PACKAGE
        Suspends the specified package (as user).
    
      unsuspend [--user USER_ID] TARGET-PACKAGE
        Unsuspends the specified package (as user).
    
      grant [--user USER_ID] PACKAGE PERMISSION
      revoke [--user USER_ID] PACKAGE PERMISSION
        These commands either grant or revoke permissions to apps.  The permissions
        must be declared as used in the app's manifest, be runtime permissions
        (protection level dangerous), and the app targeting SDK greater than Lollipop MR1.
    
      reset-permissions
        Revert all runtime permissions to their default state.
    
      set-permission-enforced PERMISSION [true|false]
    
      get-privapp-permissions TARGET-PACKAGE
        Prints all privileged permissions for a package.
    
      get-privapp-deny-permissions TARGET-PACKAGE
        Prints all privileged permissions that are denied for a package.
    
      get-oem-permissions TARGET-PACKAGE
        Prints all OEM permissions for a package.
    
      set-app-link [--user USER_ID] PACKAGE {always|ask|never|undefined}
      get-app-link [--user USER_ID] PACKAGE
    
      trim-caches DESIRED_FREE_SPACE [internal|UUID]
        Trim cache files to reach the given free space.
    
      create-user [--profileOf USER_ID] [--managed] [--restricted] [--ephemeral]
          [--guest] USER_NAME
        Create a new user with the given USER_NAME, printing the new user identifier
        of the user.
    
      remove-user USER_ID
        Remove the user with the given USER_IDENTIFIER, deleting all data
        associated with that user
    
      set-user-restriction [--user USER_ID] RESTRICTION VALUE
    
      get-max-users
    
      get-max-running-users
    
      compile [-m MODE | -r REASON] [-f] [-c] [--split SPLIT_NAME]
              [--reset] [--check-prof (true | false)] (-a | TARGET-PACKAGE)
        Trigger compilation of TARGET-PACKAGE or all packages if "-a".  Options are:
          -a: compile all packages
          -c: clear profile data before compiling
          -f: force compilation even if not needed
          -m: select compilation mode
              MODE is one of the dex2oat compiler filters:
                assume-verified
                extract
                verify
                quicken
                space-profile
                space
                speed-profile
                speed
                everything
          -r: select compilation reason
              REASON is one of:
                first-boot
                boot
                install
                bg-dexopt
                ab-ota
                inactive
                shared
                core-app
          --reset: restore package to its post-install state
          --check-prof (true | false): look at profiles when doing dexopt?
          --secondary-dex: compile app secondary dex files
          --split SPLIT: compile only the given split name
    
      force-dex-opt PACKAGE
        Force immediate execution of dex opt for the given PACKAGE.
    
      bg-dexopt-job
        Execute the background optimizations immediately.
        Note that the command only runs the background optimizer logic. It may
        overlap with the actual job but the job scheduler will not be able to
        cancel it. It will also run even if the device is not in the idle
        maintenance mode.
    
      reconcile-secondary-dex-files TARGET-PACKAGE
        Reconciles the package secondary dex files with the generated oat files.
    
      dump-profiles TARGET-PACKAGE
        Dumps method/class profile files to
        /data/misc/profman/TARGET-PACKAGE.txt
    
      snapshot-profile TARGET-PACKAGE [--code-path path]
        Take a snapshot of the package profiles to
        /data/misc/profman/TARGET-PACKAGE[-code-path].prof
        If TARGET-PACKAGE=android it will take a snapshot of the boot image
    
      set-home-activity [--user USER_ID] TARGET-COMPONENT
        Set the default home activity (aka launcher).
    
      set-installer PACKAGE INSTALLER
        Set installer package name
    
      get-instantapp-resolver
        Return the name of the component that is the current instant app installer.
    
      set-harmful-app-warning [--user <USER_ID>] <PACKAGE> [<WARNING>]
        Mark the app as harmful with the given warning message.
    
      get-harmful-app-warning [--user <USER_ID>] <PACKAGE>
        Return the harmful app warning message for the given app, if present
    
      uninstall-system-updates
        Remove updates to all system applications and fall back to their /system version.
    
    <INTENT> specifications include these flags and arguments:
        [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
        [-c <CATEGORY> [-c <CATEGORY>] ...]
        [-n <COMPONENT_NAME>]
        [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
        [--esn <EXTRA_KEY> ...]
        [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
        [--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
        [--el <EXTRA_KEY> <EXTRA_LONG_VALUE> ...]
        [--ef <EXTRA_KEY> <EXTRA_FLOAT_VALUE> ...]
        [--eu <EXTRA_KEY> <EXTRA_URI_VALUE> ...]
        [--ecn <EXTRA_KEY> <EXTRA_COMPONENT_NAME_VALUE>]
        [--eia <EXTRA_KEY> <EXTRA_INT_VALUE>[,<EXTRA_INT_VALUE...]]
            (mutiple extras passed as Integer[])
        [--eial <EXTRA_KEY> <EXTRA_INT_VALUE>[,<EXTRA_INT_VALUE...]]
            (mutiple extras passed as List<Integer>)
        [--ela <EXTRA_KEY> <EXTRA_LONG_VALUE>[,<EXTRA_LONG_VALUE...]]
            (mutiple extras passed as Long[])
        [--elal <EXTRA_KEY> <EXTRA_LONG_VALUE>[,<EXTRA_LONG_VALUE...]]
            (mutiple extras passed as List<Long>)
        [--efa <EXTRA_KEY> <EXTRA_FLOAT_VALUE>[,<EXTRA_FLOAT_VALUE...]]
            (mutiple extras passed as Float[])
        [--efal <EXTRA_KEY> <EXTRA_FLOAT_VALUE>[,<EXTRA_FLOAT_VALUE...]]
            (mutiple extras passed as List<Float>)
        [--esa <EXTRA_KEY> <EXTRA_STRING_VALUE>[,<EXTRA_STRING_VALUE...]]
            (mutiple extras passed as String[]; to embed a comma into a string,
             escape it using "\,")
        [--esal <EXTRA_KEY> <EXTRA_STRING_VALUE>[,<EXTRA_STRING_VALUE...]]
            (mutiple extras passed as List<String>; to embed a comma into a string,
             escape it using "\,")
        [-f <FLAG>]
        [--grant-read-uri-permission] [--grant-write-uri-permission]
        [--grant-persistable-uri-permission] [--grant-prefix-uri-permission]
        [--debug-log-resolution] [--exclude-stopped-packages]
        [--include-stopped-packages]
        [--activity-brought-to-front] [--activity-clear-top]
        [--activity-clear-when-task-reset] [--activity-exclude-from-recents]
        [--activity-launched-from-history] [--activity-multiple-task]
        [--activity-no-animation] [--activity-no-history]
        [--activity-no-user-action] [--activity-previous-is-top]
        [--activity-reorder-to-front] [--activity-reset-task-if-needed]
        [--activity-single-top] [--activity-clear-task]
        [--activity-task-on-home] [--activity-match-external]
        [--receiver-registered-only] [--receiver-replace-pending]
        [--receiver-foreground] [--receiver-no-abort]
        [--receiver-include-background]
        [--selector]
        [<URI> | <PACKAGE> | <COMPONENT>]
    复制代码

    这里面有几个常用的。

    1,直接看安装的app的位置信息,例如查看对应的so架构类型及so文件。

    OP4A57:/ $ pm path com.sankuai.meituan
    package:/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ==/base.apk
    OP4A57:/ $
    OP4A57:/ $
    OP4A57:/ $
    OP4A57:/ $ cd  /data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ==
    OP4A57:/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ== $
    OP4A57:/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ== $
    OP4A57:/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ== $ ls  -al
    total 89488
    drwxr-xr-x  4 system system      4096 2019-11-09 16:59 .
    drwxrwx--x 64 system system      4096 2019-11-09 16:59 ..
    -rw-r--r--  1 system system  91619230 2019-11-09 16:59 base.apk
    drwxr-xr-x  3 system system      4096 2019-11-09 16:59 lib
    drwxrwx--x  3 system install     4096 2019-11-09 16:59 oat
    OP4A57:/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ== $
    OP4A57:/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ== $
    OP4A57:/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ== $ cd lib
    OP4A57:/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ==/lib $
    OP4A57:/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ==/lib $ ls -al
    total 12
    drwxr-xr-x 3 system system 4096 2019-11-09 16:59 .
    drwxr-xr-x 4 system system 4096 2019-11-09 16:59 ..
    drwxr-xr-x 2 system system 4096 2019-11-09 16:59 arm
    OP4A57:/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ==/lib $
    OP4A57:/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ==/lib $
    OP4A57:/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ==/lib $
    OP4A57:/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ==/lib $ cd  arm
    OP4A57:/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ==/lib/arm $
    OP4A57:/data/app/com.sankuai.meituan-DJ5zz15kfZBSI030X71JkQ==/lib/arm $ ls -al
    total 31720
    drwxr-xr-x 2 system system    4096 2019-11-09 16:59 .
    drwxr-xr-x 3 system system    4096 2019-11-09 16:59 ..
    -rwxr-xr-x 1 system system 3056817 1979-11-30 00:00 libAMapSDK_MAP_v6_9_4.so
    -rwxr-xr-x 1 system system  390628 1979-11-30 00:00 libBaiduMapSDK_base_v5_4_5.so
    -rwxr-xr-x 1 system system 2491780 1979-11-30 00:00 libBaiduMapSDK_map_v5_4_5.so
    -rwxr-xr-x 1 system system  628096 1979-11-30 00:00 libCardOcr.so
    -rwxr-xr-x 1 system system  296828 1979-11-30 00:00 libCtaApiLib.so
    -rwxr-xr-x 1 system system  124724 1979-11-30 00:00 libHprofUtils.so
    -rwxr-xr-x 1 system system   62868 1979-11-30 00:00 libPayRequestCrypt.so
    -rwxr-xr-x 1 system system 5363100 1979-11-30 00:00 libagora-rtc-sdk-jni.so
    -rwxr-xr-x 1 system system  255932 1979-11-30 00:00 libanimated-webp.so
    -rwxr-xr-x 1 system system   17820 1979-11-30 00:00 libbsdiff-gzip.so
    -rwxr-xr-x 1 system system   53448 1979-11-30 00:00 libbspatch.so
    -rwxr-xr-x 1 system system  108256 1979-11-30 00:00 libcrash_extra_tools.so
    -rwxr-xr-x 1 system system   13404 1979-11-30 00:00 libdpobj.so
    -rwxr-xr-x 1 system system  115928 1979-11-30 00:00 libentryexpro.so
    -rwxr-xr-x 1 system system   99600 1979-11-30 00:00 libextractCard.so
    -rwxr-xr-x 1 system system  456248 1979-11-30 00:00 libfaceliveness.so
    -rwxr-xr-x 1 system system   79124 1979-11-30 00:00 libfb.so
    -rwxr-xr-x 1 system system  111940 1979-11-30 00:00 libfolly_json.so
    -rwxr-xr-x 1 system system   75108 1979-11-30 00:00 libglog.so
    -rwxr-xr-x 1 system system   17672 1979-11-30 00:00 libglog_init.so
    -rwxr-xr-x 1 system system  661132 1979-11-30 00:00 libgnustl_shared.so
    -rwxr-xr-x 1 system system   13524 1979-11-30 00:00 libgpuimage-library.so
    -rwxr-xr-x 1 system system 4029548 1979-11-30 00:00 libhce-engine.so
    -rwxr-xr-x 1 system system  943444 1979-11-30 00:00 libicu_common.so
    -rwxr-xr-x 1 system system 1910900 1979-11-30 00:00 libjsc.so
    -rwxr-xr-x 1 system system 5987248 1979-11-30 00:00 libjse.so
    -rwxr-xr-x 1 system system  132620 1979-11-30 00:00 liblogan.so
    -rwxr-xr-x 1 system system  165576 1979-11-30 00:00 libmmkv.so
    -rwxr-xr-x 1 system system  136692 1979-11-30 00:00 libmoon.so
    -rwxr-xr-x 1 system system  522656 1979-11-30 00:00 libmtcodeReaderjni.so
    -rwxr-xr-x 1 system system  262892 1979-11-30 00:00 libmtguard.so
    -rwxr-xr-x 1 system system   25740 1979-11-30 00:00 libnh.so
    -rwxr-xr-x 1 system system   38432 1979-11-30 00:00 libpl_droidsonroids_gif.so
    -rwxr-xr-x 1 system system    9480 1979-11-30 00:00 libprivatedata.so
    -rwxr-xr-x 1 system system   17892 1979-11-30 00:00 libqrcode-engine.so
    -rwxr-xr-x 1 system system  308560 1979-11-30 00:00 libreactnativejni.so
    -rwxr-xr-x 1 system system  120400 1979-11-30 00:00 libsnare_1.0.2.so
    -rwxr-xr-x 1 system system 2104456 1979-11-30 00:00 libtxmapengine.so
    -rwxr-xr-x 1 system system  472964 1979-11-30 00:00 libuptsmaddon.so
    -rwxr-xr-x 1 system system  472964 1979-11-30 00:00 libuptsmaddonmi.so
    -rwxr-xr-x 1 system system   30232 1979-11-30 00:00 libutility.so
    -rwxr-xr-x 1 system system   21752 1979-11-30 00:00 libweibosdkcore.so
    -rwxr-xr-x 1 system system   34416 1979-11-30 00:00 libwind.so
    -rwxr-xr-x 1 system system  120120 1979-11-30 00:00 libyoga.so
    复制代码

    2,pm clear packageName,这个命令太实用了。
    清除app数据,这在不卸载,不去设置中找清除数据的地方,就可以直接清楚数据,模拟首次安装场景。

    3,grant package_name permission/revoke package_name permission
    授予权限/清除权限,配合前面的dumpsys package可以调试特殊机型上权限这块的问题。


    四、结语

    通过adb,可以从开发环境桥接到设备环境,甚至在目标设备内部,通过程序的方式直接执行adb shell环境下的对应命令,以获取应用层程序原本不能直接获取到的有用信息,这在某些特定的需求场景下非常有用。

    adb shell命令,在原本Linux命令基础上,进一步丰富了Android系统相关的命令集。尤其以dumpsys、am、pm等命令为代表的命令实体,内含有丰富的命令参数。大概了解这些命令及对应的作用,可以在一定程序上大大方便我们的开发调试过程。




    五、环境

    文中实践及对应结果,可能与本机环境及设备有关,特备注如下:

    MacOS:10.13.4
    Android:Oppo Reno2
    AS:3.5.1
    AS:3.5.1
    Gralde: 4.6
    AGP: 3.2.1
    复制代码



    六、参考文档

    developer.android.com/studio/comm…
    github.com/mzlogin/awe…


    作者:HappyCorn
    链接:https://juejin.im/post/5dc622b06fb9a04a856f7a9c
    来源:掘金
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    《3D Math Primer for Graphics and Game Development》读书笔记1
    OpenGL学习资料汇总
    [Unity3D]做个小Demo学习Input.touches
    [Unity3D]再次点击以退出程序
    图文详解Unity3D中Material的Tiling和Offset是怎么回事
    自制Unity小游戏TankHero-2D(5)声音+爆炸+场景切换+武器弹药
    自制Unity小游戏TankHero-2D(4)关卡+小地图图标+碰撞条件分析
    Unity3D核心类型一览
    自制Unity小游戏TankHero-2D(3)开始玩起来
    自制Unity小游戏TankHero-2D(2)制作敌方坦克
  • 原文地址:https://www.cnblogs.com/lwbqqyumidi/p/11991828.html
Copyright © 2011-2022 走看看