一、背景
從系統架構上來說,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
,此處有點類似ActivityManagerService
,AMS
也是包含了對其他組件的處理的。
更進一步,這個命令中的參數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
復制代碼
六、參考文檔
作者:HappyCorn
鏈接:https://juejin.im/post/5dc622b06fb9a04a856f7a9c
來源:掘金
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。