每天一個安卓測試開發小知識之 (四) ---常用的adb shell命令第二期 pm命令
每天一個安卓測試開發小知識之 (四) ---常用的adb shell命令第二期 pm命令
上一期我們簡單介紹了如何進入\退出 adb shell以及 adb shell 的常用命令,本期繼續介紹
pm命令
pm是什么,pm -> packageManager 翻譯過來就是包管理 ,該命令就是提供包的管理功能
包是什么,在安卓系統中 一個包就是一個app,例如三方包,微信 qq等,手機自帶的包,時鐘、設置 都是一個個獨立的包
1. 查看幫助
adb shell pm -h

2. 查看已安裝的全部包
adb shell pm list packages 會列出包名。
包名就是包的唯一id,例如微信的包名就是 com.tencent.mm ,一個手機中包名不能重復,即不能安裝包名相同的app

3. 包的安裝
adb shell pm install *** 安裝apk,** 是手機中的apk路徑
例如安裝 app-debug.apk (安卓系統中app的后綴名是 .apk)
1. push 主機上的apk到手機中
2. 通過 pm install安裝

還有一種方式是 直接 adb install app-debug.apk,也能實現相同的功能

無論是 adb install 還是 adb shell pm install 都可以加很多參數,不同的參數又不同的含義
install [-rtfdg] [-i PACKAGE] [--user USER_ID|all|current]
[-p INHERIT_PACKAGE] [--install-location 0/1/2]
[--install-reason 0/1/2/3/4] [--originating-uri URI]
[--referrer URI] [--abi ABI_NAME] [--force-sdk]
[--preload] [--instant] [--full] [--dont-kill]
[--enable-rollback [0/1/2]]
[--force-uuid internal|UUID] [--pkg PACKAGE] [-S BYTES]
[--apex] [--non-staged] [--force-non-staged]
[--staged-ready-timeout TIMEOUT] [--ignore-dexopt-profile]
[--dexopt-compiler-filter FILTER]
[PATH [SPLIT...]|-]
Install an application. Must provide the apk data to install, either as
file path(s) or '-' to read from stdin. Options are:
-R: disallow replacement of existing application
-t: allow test packages
-i: specify package name of installer owning the app
-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
--restrict-permissions: don't whitelist restricted permissions at install
--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
--instant: cause the app to be installed as an ephemeral install app
--full: cause the app to be installed as a non-ephemeral full app
--enable-rollback: enable rollbacks for the upgrade.
0=restore (default), 1=wipe, 2=retain
--rollback-impact-level: set device impact required for rollback.
0=low (default), 1=high, 2=manual only
--install-location: force the install location:
0=auto, 1=internal only, 2=prefer external
--install-reason: indicates why the app is being installed:
0=unknown, 1=admin policy, 2=device restore,
3=device setup, 4=user request
--update-ownership: request the update ownership enforcement
--force-uuid: force install on to disk volume with given UUID
--apex: install an .apex file, not an .apk
--non-staged: explicitly set this installation to be non-staged.
This flag is only useful for APEX installs that are implicitly
assumed to be staged.
--force-non-staged: force the installation to run under a non-staged
session, which may complete without requiring a reboot. This will
force a rebootless update even for APEXes that don't support it
--staged-ready-timeout: By default, staged sessions wait 60000
milliseconds for pre-reboot verification to complete when
performing staged install. This flag is used to alter the waiting
time. You can skip the waiting time by specifying a TIMEOUT of '0'
--ignore-dexopt-profile: if set, all profiles are ignored by dexopt
during the installation, including the profile in the DM file and
the profile embedded in the APK file. If an invalid profile is
provided during installation, no warning will be reported by `adb
install`.
This option does not affect later dexopt operations (e.g.,
background dexopt and manual `pm compile` invocations).
--dexopt-compiler-filter: the target compiler filter for dexopt during
the installation. The filter actually used may be different.
Valid values: one of the values documented in
https://source.android.com/docs/core/runtime/configure#compiler_filters
or 'skip'
--disable-auto-install-dependencies: if set, any missing shared
library dependencies will not be auto-installed
-R 允許重復安裝
-t 允許安裝測試包
-d 允許降級安裝
-g 安裝時直接授予app全部運行時權限
等等參數,這里只介紹一些常用的參數
例如 :

4. 包的卸載
adb shell pm uninstall packageName 或者 adb uninstall packageName packageName 是包名

4.1 包名如何獲取
如果是自己開發的app,包名肯定已知,如果是其他的app呢
方式一:通過手機上的界面去查看包名
方式二:通過aapt命令查看 適用于在主機上查看apk的信息
方式三: 通過adb命令查看 適用于手機上查看apk信息
- 方式一,以小米手機為例,長按app圖表 - > 點擊感嘆號 -> 點擊右上角三個點->點擊應用詳情
![在這里插入圖片描述]()
- 方式二: 通過aapt
以linux為例 直接在aapt的目錄中運行 ./aapt2 則會看到aapt命令的幫助信息

- 查看apk信息
./aapt dump badging ~/app-debug.apk,~/app-debug.apkapk的保存路徑
![在這里插入圖片描述]()
可以看到 包名是com.miui.sysopt當前的版本號569以及sdk版本、聲明的權限等等 - 方式三 通過adb命令查看包名
當我們打開某個app時,以設置為例,我們可以通過adb命令獲取當前界面上是哪個app,從而得到app的包名
對應的adb命令是 adb shell dumpsys activity activities | grep mFocusedApp=ActivityRecord

通過以上命令可以得到當前的app是 com.android.settings 并且可以看到當前的 activity是 com.android.settings/.MainSettings
本期的分享先到這里,每天進步一點點!!!


浙公網安備 33010602011771號