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

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

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

pm命令

pm是什么,pm -> packageManager 翻译过来就是包管理 ,该命令就是提供包的管理功能
包是什么,在安卓系统中 一个包就是一个app,例如三方包,微信 qq等,手机自带的包,时钟、设置 都是一个个独立的包

1. 查看帮助

bash 复制代码
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 都可以加很多参数,不同的参数又不同的含义

bash 复制代码
  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

aapt 是一个安卓提供的一个工具,aapt2下载链接 aapt2简介 可以用来获取apk的详细信息

以linux为例 直接在aapt的目录中运行 ./aapt2 则会看到aapt命令的帮助信息

  • 查看apk信息 ./aapt dump badging ~/app-debug.apk~/app-debug.apk apk的保存路径 可以看到 包名是 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

本期的分享先到这里,每天进步一点点!!!

相关推荐
Libraeking9 小时前
破壁行动:在旧项目中丝滑嵌入 Compose(混合开发实战)
android·经验分享·android jetpack
市场部需要一个软件开发岗位9 小时前
JAVA开发常见安全问题:Cookie 中明文存储用户名、密码
android·java·安全
JMchen12311 小时前
Android后台服务与网络保活:WorkManager的实战应用
android·java·网络·kotlin·php·android-studio
crmscs12 小时前
剪映永久解锁版/电脑版永久会员VIP/安卓SVIP手机永久版下载
android·智能手机·电脑
localbob12 小时前
杀戮尖塔 v6 MOD整合版(Slay the Spire)安卓+PC端免安装中文版分享 卡牌肉鸽神作!杀戮尖塔中文版,电脑和手机都能玩!杀戮尖塔.exe 杀戮尖塔.apk
android·杀戮尖塔apk·杀戮尖塔exe·游戏分享
机建狂魔12 小时前
手机秒变电影机:Blackmagic Camera + LUT滤镜包的专业级视频解决方案
android·拍照·摄影·lut滤镜·拍摄·摄像·录像
hudawei99612 小时前
flutter和Android动画的对比
android·flutter·动画
lxysbly14 小时前
md模拟器安卓版带金手指2026
android
儿歌八万首15 小时前
硬核春节:用 Compose 打造“赛博鞭炮”
android·kotlin·compose·春节
消失的旧时光-194317 小时前
从 Kotlin 到 Dart:为什么 sealed 是处理「多种返回结果」的最佳方式?
android·开发语言·flutter·架构·kotlin·sealed