找回密码
 立即注册
首页 业界区 业界 每天一个安卓测试开发小知识之 (四) ---常用的adb she ...

每天一个安卓测试开发小知识之 (四) ---常用的adb shell命令第二期 pm命令

杆树 昨天 20:29
每天一个安卓测试开发小知识之 (四) ---常用的adb shell命令第二期 pm命令

上一期我们简单介绍了如何进入\退出 adb shell以及 adb shell 的常用命令,本期继续介绍
pm命令

pm是什么,pm -> packageManager 翻译过来就是包管理 ,该命令就是提供包的管理功能
包是什么,在安卓系统中 一个包就是一个app,例如三方包,微信 qq等,手机自带的包,时钟、设置 都是一个个独立的包
1. 查看帮助
  1. adb shell pm -h
复制代码
1.png

2. 查看已安装的全部包

adb shell pm  list packages 会列出包名。
包名就是包的唯一id,例如微信的包名就是 com.tencent.mm ,一个手机中包名不能重复,即不能安装包名相同的app
2.png

3. 包的安装

adb shell pm install *** 安装apk,** 是手机中的apk路径
例如安装 app-debug.apk (安卓系统中app的后缀名是 .apk)
1. push 主机上的apk到手机中
2. 通过 pm install安装
3.png

还有一种方式是 直接 adb install app-debug.apk,也能实现相同的功能
4.png

无论是 adb install 还是 adb shell pm install 都可以加很多参数,不同的参数又不同的含义
  1.   install [-rtfdg] [-i PACKAGE] [--user USER_ID|all|current]
  2.        [-p INHERIT_PACKAGE] [--install-location 0/1/2]
  3.        [--install-reason 0/1/2/3/4] [--originating-uri URI]
  4.        [--referrer URI] [--abi ABI_NAME] [--force-sdk]
  5.        [--preload] [--instant] [--full] [--dont-kill]
  6.        [--enable-rollback [0/1/2]]
  7.        [--force-uuid internal|UUID] [--pkg PACKAGE] [-S BYTES]
  8.        [--apex] [--non-staged] [--force-non-staged]
  9.        [--staged-ready-timeout TIMEOUT] [--ignore-dexopt-profile]
  10.        [--dexopt-compiler-filter FILTER]
  11.        [PATH [SPLIT...]|-]
  12.     Install an application.  Must provide the apk data to install, either as
  13.     file path(s) or '-' to read from stdin.  Options are:
  14.       -R: disallow replacement of existing application
  15.       -t: allow test packages
  16.       -i: specify package name of installer owning the app
  17.       -f: install application on internal flash
  18.       -d: allow version code downgrade (debuggable packages only)
  19.       -p: partial application install (new split on top of existing pkg)
  20.       -g: grant all runtime permissions
  21.       -S: size in bytes of package, required for stdin
  22.       --user: install under the given user.
  23.       --dont-kill: installing a new feature split, don't kill running app
  24.       --restrict-permissions: don't whitelist restricted permissions at install
  25.       --originating-uri: set URI where app was downloaded from
  26.       --referrer: set URI that instigated the install of the app
  27.       --pkg: specify expected package name of app being installed
  28.       --abi: override the default ABI of the platform
  29.       --instant: cause the app to be installed as an ephemeral install app
  30.       --full: cause the app to be installed as a non-ephemeral full app
  31.       --enable-rollback: enable rollbacks for the upgrade.
  32.           0=restore (default), 1=wipe, 2=retain
  33.       --rollback-impact-level: set device impact required for rollback.
  34.           0=low (default), 1=high, 2=manual only
  35.       --install-location: force the install location:
  36.           0=auto, 1=internal only, 2=prefer external
  37.       --install-reason: indicates why the app is being installed:
  38.           0=unknown, 1=admin policy, 2=device restore,
  39.           3=device setup, 4=user request
  40.       --update-ownership: request the update ownership enforcement
  41.       --force-uuid: force install on to disk volume with given UUID
  42.       --apex: install an .apex file, not an .apk
  43.       --non-staged: explicitly set this installation to be non-staged.
  44.           This flag is only useful for APEX installs that are implicitly
  45.           assumed to be staged.
  46.       --force-non-staged: force the installation to run under a non-staged
  47.           session, which may complete without requiring a reboot. This will
  48.           force a rebootless update even for APEXes that don't support it
  49.       --staged-ready-timeout: By default, staged sessions wait 60000
  50.           milliseconds for pre-reboot verification to complete when
  51.           performing staged install. This flag is used to alter the waiting
  52.           time. You can skip the waiting time by specifying a TIMEOUT of '0'
  53.       --ignore-dexopt-profile: if set, all profiles are ignored by dexopt
  54.           during the installation, including the profile in the DM file and
  55.           the profile embedded in the APK file. If an invalid profile is
  56.           provided during installation, no warning will be reported by `adb
  57.           install`.
  58.           This option does not affect later dexopt operations (e.g.,
  59.           background dexopt and manual `pm compile` invocations).
  60.       --dexopt-compiler-filter: the target compiler filter for dexopt during
  61.           the installation. The filter actually used may be different.
  62.           Valid values: one of the values documented in
  63.           https://source.android.com/docs/core/runtime/configure#compiler_filters
  64.           or 'skip'
  65.       --disable-auto-install-dependencies: if set, any missing shared
  66.           library dependencies will not be auto-installed
复制代码
-R 允许重复安装
-t 允许安装测试包
-d 允许降级安装
-g 安装时直接授予app全部运行时权限
等等参数,这里只介绍一些常用的参数
例如 :
5.png

4. 包的卸载

adb shell pm uninstall packageName 或者 adb uninstall packageName packageName 是包名
6.png

4.1 包名如何获取

如果是自己开发的app,包名肯定已知,如果是其他的app呢
方式一:通过手机上的界面去查看包名
方式二:通过aapt命令查看 适用于在主机上查看apk的信息
方式三: 通过adb命令查看 适用于手机上查看apk信息


  • 方式一,以小米手机为例,长按app图表 - > 点击感叹号 -> 点击右上角三个点->点击应用详情
    7.png

  • 方式二: 通过aapt
aapt 是一个安卓提供的一个工具,aapt2下载链接 aapt2简介 可以用来获取apk的详细信息
以linux为例 直接在aapt的目录中运行 ./aapt2 则会看到aapt命令的帮助信息
8.png


  • 查看apk信息 ./aapt dump badging ~/app-debug.apk,   ~/app-debug.apk apk的保存路径
    9.png

    可以看到 包名是 com.miui.sysopt 当前的版本号 569 以及 sdk版本 、声明的权限等等
  • 方式三 通过adb命令查看包名
当我们打开某个app时,以设置为例,我们可以通过adb命令获取当前界面上是哪个app,从而得到app的包名
对应的adb命令是   adb  shell dumpsys activity activities | grep mFocusedApp=ActivityRecord
10.png

通过以上命令可以得到当前的app是 com.android.settings 并且可以看到当前的 activity是  com.android.settings/.MainSettings
本期的分享先到这里,每天进步一点点!!!

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册