installing-and-managing-apps

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Installing and Managing Apps —
adb install
and
pm

安装和管理应用 —
adb install
与
pm

App lifecycle from the host.
adb install
ships an APK;
pm
is the on-device PackageManager front-end that lists, paths, clears, enables/disables, and grants permissions. The most common subtle bug in test scaffolding is using
am force-stop
to "reset" an app between tests — that command kills the process but leaves SharedPreferences, the Room DB, and files on disk. The hermetic equivalent is
pm clear
.
从主机端管理应用生命周期。
adb install
用于传送 APK;
pm
是设备端的 PackageManager 前端,用于列出包、查找路径、清除数据、启用或禁用应用以及授予权限。测试脚手架中最常见的细微错误,是使用
am force-stop
在测试之间“重置”应用——该命令只会终止进程,但会保留 SharedPreferences、Room DB 以及磁盘上的文件。实现隔离的等价方式是
pm clear
。

When to use this skill

何时使用此 skill

  • The user wants to install an APK from the host or push a debug build between every test run.
  • The user asks "how do I reset the app's state between tests?" or sees stale data carry over from one test to the next.
  • The user hits
    INSTALL_FAILED_VERSION_DOWNGRADE
    ,
    INSTALL_FAILED_USER_RESTRICTED
    , or signature mismatch errors.
  • The user wants to grant all runtime permissions at install (
    -g
    ), or grant/revoke a single permission post-install.
  • The user wants to list only third-party packages, find an APK on disk, or dump a Baseline Profile.
  • 用户希望从主机安装 APK,或在每次测试运行之间推送 debug build。
  • 用户询问“如何在测试之间重置应用状态?”或发现旧数据从一次测试延续到下一次测试。
  • 用户遇到
    INSTALL_FAILED_VERSION_DOWNGRADE
    、
    INSTALL_FAILED_USER_RESTRICTED
    或签名不匹配错误。
  • 用户希望在安装时授予所有运行时权限(
    -g
    ),或在安装后授予或撤销单个权限。
  • 用户希望仅列出第三方包、查找磁盘上的 APK,或转储 Baseline Profile。

When NOT to use this skill

何时不应使用此 skill

  • The user wants to actually run instrumented tests against the installed app — use
    ../../tests/running-instrumented-tests-via-adb/SKILL.md
    .
  • The user wants to choose a device or wait for boot — use
    ../../devices/connecting-to-devices/SKILL.md
    .
  • The user wants the architecture / environment / authentication picture — use
    ../../architecture/understanding-adb-architecture/SKILL.md
    .
  • 用户希望针对已安装的应用实际运行插桩测试——请使用
    ../../tests/running-instrumented-tests-via-adb/SKILL.md
    。
  • 用户希望选择设备或等待设备启动完成——请使用
    ../../devices/connecting-to-devices/SKILL.md
    。
  • 用户希望了解架构、环境或认证情况——请使用
    ../../architecture/understanding-adb-architecture/SKILL.md
    。

Prerequisites

前置条件

  • A connected device with state
    device
    (see
    ../../devices/connecting-to-devices/SKILL.md
    ).
  • The APK(s) signed with a debug or release key the developer controls.
  • For test APKs, the manifest sets
    android:testOnly="true"
    (Gradle does this for
    androidTest
    builds) and install requires
    -t
    .
  • For runtime permissions, the device runs API 23+.
  • 已连接状态为
    device
    的设备(参见
    ../../devices/connecting-to-devices/SKILL.md
    )。
  • APK 已使用开发者掌控的 debug key 或 release key 签名。
  • 对于测试 APK,manifest 设置了
    android:testOnly="true"
    (Gradle 会为
    androidTest
    build 自动设置),安装时需要使用
    -t
    。
  • 对于运行时权限,设备需运行 API 23 或更高版本。

Workflow

工作流

  • 1. Install with the right flags. From
    pm install
    (and the
    adb install
    wrapper) options table:
    FlagMeaning
    -r
    "Reinstall an existing app, keeping its data." Required when the package is already installed; without it
    adb install
    returns
    INSTALL_FAILED_ALREADY_EXISTS
    . CI scripts MUST always pass
    -r
    for repeat runs.
    -d
    "Allow version code downgrade." Required when the new APK has a lower
    versionCode
    . Note: per
    adb help
    , restricted to debuggable packages on stock builds.
    -t
    "Allow test APKs to be installed." Required for any APK whose manifest carries
    android:testOnly="true"
    (i.e. all Gradle
    androidTest
    builds).
    -g
    "Grant all permissions listed in the app manifest." Best for hermetic test runs.
    -l
    Forward-lock the application (legacy DRM). Almost never needed today.
    -s
    Install on the SD card / external storage (when device supports it).
    --user <id>
    Install for a specific user profile. Default: all users.
    current
    resolves to the foreground user.
    `--install-location 01
    -f
    Install on internal system memory.
    -i <installer_pkg>
    Tag the install with an installer package name.
    --fastdeploy
    Patch-only install (only changed parts of the APK).
    --incremental
    /
    --no-incremental
    Streamed install; requires APK Signature Scheme v4 sidecar
    .idsig
    .
    --wait
    blocks until full APK is available.
    Common recipes:
    bash
    adb install -r app-debug.apk                       # ordinary reinstall
    adb install -r -d app-debug.apk                    # allow downgrade
    adb install -r -t -g app-androidTest.apk           # test APK with all permissions granted
    adb install -r -d --user current app-debug.apk     # force foreground-user install
  • 2. For split APKs use
    adb install-multiple
    .
    A modern App Bundle produces a base APK plus per-feature/per-config splits —
    adb install
    of just one fails:
    bash
    adb install-multiple -r -t -g \
      base-master.apk base-arm64_v8a.apk base-xxhdpi.apk
  • 3. Reset app state between tests with
    pm clear
    . Do NOT use
    am force-stop
    for hermetic isolation.
    • am force-stop <pkg>
      "Force-stop everything associated with
      package
      ." — kills processes only. SharedPreferences, Room/SQLite databases, files in
      /data/data/<pkg>
      , and cache persist.
    • pm clear <pkg>
      "Delete all data associated with a package." — wipes user data and cache. The next launch starts from a fresh-install state. For genuine hermetic isolation between tests (the default for instrumented suites), use
      pm clear
      .
      am force-stop
      is appropriate only when the explicit goal is to kill the process while preserving state (e.g. testing a "resume from killed" path).
    bash
    adb shell pm clear com.example.app
    Most pipelines wire this up via Test Orchestrator's
    clearPackageData
    instrumentation arg — see
    ../../tests/running-instrumented-tests-via-adb/SKILL.md
    .
  • 4. List, find, and inspect packages with
    pm list packages
    and
    pm path
    .
    pm list packages [-f] [-d] [-e] [-s] [-3] [-i] [-u] [--user <id>] [<filter>]
    FlagMeaning
    -f
    "See 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."
    -u
    "Include uninstalled packages."
    --user <id>
    "The user space to query."
    Recipes:
    bash
    adb shell pm list packages -3 | sort                   # all sideloaded apps
    adb shell pm list packages -f com.example              # APK paths for matching pkgs
    adb shell pm path com.example.app                      # package:/data/app/.../base.apk
    adb shell dumpsys package com.example.app | head -200  # detailed install/permission info
  • 5. Manage runtime permissions with
    pm grant
    /
    pm revoke
    (API 23+).
    "Grant a permission to an app. On devices running Android 6.0 (API level 23) and higher, the permission can be any permission declared in the app manifest."
    bash
    adb shell pm grant  com.example.app android.permission.CAMERA
    adb shell pm revoke com.example.app android.permission.CAMERA
    adb shell pm reset-permissions                         # reset every app to defaults
    Flagging
    -g
    at install time grants every manifest-declared runtime permission in one shot — preferred for hermetic test runs.
  • 6. Enable / disable packages or specific components.
    bash
    adb shell pm disable com.example.app                       # whole package
    adb shell pm disable-user --user 0 com.example.app         # per-user disable (preserves data)
    adb shell pm enable  com.example.app
    adb shell pm enable  com.example.app/.MyTestActivity       # specific component
    Useful for forcing a known accessibility-service / IME state in tests, or for staging A/B comparisons by disabling rivals.
  • 7. Uninstall correctly.
    pm uninstall [-k] [--user <id>] [--versionCode <vc>] <package>
    • -k
      "Keep the data and cache directories after package removal." Useful for upgrade-from-clean tests where the user data simulates an existing install.
    • --user <id>
      per-user removal.
    • --versionCode <vc>
      "Only uninstalls if the app has the given version code." Idempotent uninstall in CI scripts.
    bash
    adb uninstall com.example.app                       # equivalent to: pm uninstall <pkg>
    adb shell pm uninstall -k com.example.app           # uninstall but preserve data
    adb shell pm uninstall --user 0 com.example.app     # only from primary user
  • 8. Dump a Baseline Profile when needed.
    "Starting in Android 7.0 (API level 24), the Android Runtime (ART) collects execution profiles for installed apps...
    adb shell cmd package dump-profiles <package>
    then
    adb pull /data/misc/profman/<package>.prof.txt
    ." The doc warns: "It is only possible to retrieve the execution profile filename if you have root access to the file system, for example, on an emulator." On user builds without root, use
    androidx.benchmark
    Macrobenchmark to extract the profile programmatically.
  • 9. Diagnose common install failures:
    ErrorCauseFix
    INSTALL_FAILED_VERSION_DOWNGRADE
    New APK's
    versionCode
    is lower than the installed one.
    Add
    -d
    .
    INSTALL_FAILED_USER_RESTRICTED
    Multi-user device: the install user (typically the secondary or work profile) blocks installs.
    --user 0
    or
    --user current
    ; have the user disable "Install via USB" restriction in their profile.
    INSTALL_FAILED_UPDATE_INCOMPATIBLE
    (signature mismatch)
    New APK signed with a different key than the installed one.Uninstall first (
    adb uninstall <pkg>
    ); resign with the same key.
    INSTALL_FAILED_TEST_ONLY
    APK has
    android:testOnly="true"
    but
    -t
    was omitted.
    Add
    -t
    (typical for
    app-androidTest.apk
    ).
    INSTALL_FAILED_INVALID_APK
    (split APK only)
    Tried to install one APK of a split set.Use
    adb install-multiple
    .
    INSTALL_FAILED_INSUFFICIENT_STORAGE
    Out of space.
    adb shell df /data
    and cleanup.
  • 10. Quote the shell correctly (Platform Tools 23+ rule). Verbatim from the adb page:
    "With Android Platform Tools 23 and higher,
    adb
    handles arguments the same way that the
    ssh(1)
    command does. ...
    adb shell setprop key 'two words'
    is now an error ... To make the command work, quote twice, once for the local shell and once for the remote shell, as you do with
    ssh(1)
    . For example,
    adb shell setprop key \"'two words'\"
    works because the local shell takes the outer level of quoting and the device still sees the inner level of quoting." Same rule applies for any
    pm
    argument with spaces, special characters, or shell metacharacters.
  • 1. 使用正确的标志安装。 以下选项摘自
    pm install
    (以及
    adb install
    包装器)的选项表:
    标志含义
    -r
    “重新安装现有应用并保留数据。” 当包已安装时必须使用;否则
    adb install
    会返回
    INSTALL_FAILED_ALREADY_EXISTS
    。CI 脚本重复运行时必须始终传入
    -r
    。
    -d
    “允许 versionCode 降级。” 当新 APK 的
    versionCode
    更低时必须使用。注意:根据
    adb help
    ,在标准系统构建中,此选项仅限 debuggable 包使用。
    -t
    “允许安装测试 APK。” 任何 manifest 中包含
    android:testOnly="true"
    的 APK(即所有 Gradle
    androidTest
    build)都必须使用此选项。
    -g
    “授予应用 manifest 中列出的所有权限。” 最适合 hermetic 测试运行。
    -l
    对应用启用 forward lock(旧版 DRM)。如今几乎不需要使用。
    -s
    安装到 SD 卡或外部存储(设备支持时)。
    --user <id>
    为特定用户配置文件安装。默认值为:所有用户。
    current
    表示当前前台用户。
    `--install-location 01
    -f
    安装到内部系统存储。
    -i <installer_pkg>
    使用 installer package name 标记本次安装。
    --fastdeploy
    仅安装发生变化的 APK 部分。
    --incremental
    /
    --no-incremental
    流式安装;需要 APK Signature Scheme v4 sidecar
    .idsig
    。
    --wait
    会阻塞,直到完整 APK 可用。
    常用示例:
    bash
    adb install -r app-debug.apk                       # ordinary reinstall
    adb install -r -d app-debug.apk                    # allow downgrade
    adb install -r -t -g app-androidTest.apk           # test APK with all permissions granted
    adb install -r -d --user current app-debug.apk     # force foreground-user install
  • 2. 对于 split APK,使用
    adb install-multiple
    。
    现代 App Bundle 会生成一个 base APK,以及按功能或配置拆分的 split APK——仅使用
    adb install
    安装其中一个会失败:
    bash
    adb install-multiple -r -t -g \
      base-master.apk base-arm64_v8a.apk base-xxhdpi.apk
  • 3. 使用
    pm clear
    在测试之间重置应用状态。不要将
    am force-stop
    用于 hermetic isolation。
    • am force-stop <pkg>
      :“强制停止与
      package
      关联的所有内容。”——它只会终止进程。SharedPreferences、Room/SQLite 数据库、
      /data/data/<pkg>
      中的文件以及缓存都会继续保留。
    • pm clear <pkg>
      :“删除与包关联的所有数据。”——它会清除用户数据和缓存。下次启动时,应用将处于全新安装状态。 若要在测试之间实现真正的 hermetic isolation(instrumented 测试套件的默认要求),请使用
      pm clear
      。只有在明确目标是在保留状态的同时终止进程时,才适合使用
      am force-stop
      ,例如测试“从被终止状态恢复”的路径。
    bash
    adb shell pm clear com.example.app
    大多数 pipeline 会通过 Test Orchestrator 的
    clearPackageData
    instrumentation arg 完成此操作——参见
    ../../tests/running-instrumented-tests-via-adb/SKILL.md
    。
  • 4. 使用
    pm list packages
    和
    pm path
    列出、查找和检查包。
    pm list packages [-f] [-d] [-e] [-s] [-3] [-i] [-u] [--user <id>] [<filter>]
    标志含义
    -f
    “查看关联文件。”
    -d
    “仅显示已禁用的包。”
    -e
    “仅显示已启用的包。”
    -s
    “仅显示系统包。”
    -3
    “仅显示第三方包。”
    -i
    “查看这些包的 installer。”
    -u
    “包含已卸载的包。”
    --user <id>
    “要查询的用户空间。”
    常用示例:
    bash
    adb shell pm list packages -3 | sort                   # all sideloaded apps
    adb shell pm list packages -f com.example              # APK paths for matching pkgs
    adb shell pm path com.example.app                      # package:/data/app/.../base.apk
    adb shell dumpsys package com.example.app | head -200  # detailed install/permission info
  • 5. 使用
    pm grant
    /
    pm revoke
    管理运行时权限(API 23+)。
    “向应用授予权限。在运行 Android 6.0(API level 23)或更高版本的设备上,该权限可以是应用 manifest 中声明的任何权限。”
    bash
    adb shell pm grant  com.example.app android.permission.CAMERA
    adb shell pm revoke com.example.app android.permission.CAMERA
    adb shell pm reset-permissions                         # reset every app to defaults
    在安装时指定
    -g
    ,可以一次性授予 manifest 中声明的所有运行时权限——这是 hermetic 测试运行的首选方式。
  • 6. 启用或禁用包及特定组件。
    bash
    adb shell pm disable com.example.app                       # whole package
    adb shell pm disable-user --user 0 com.example.app         # per-user disable (preserves data)
    adb shell pm enable  com.example.app
    adb shell pm enable  com.example.app/.MyTestActivity       # specific component
    这有助于在测试中强制应用处于已知的 accessibility service 或 IME 状态,或者通过禁用竞品应用来准备 A/B 对比环境。
  • 7. 正确卸载。
    pm uninstall [-k] [--user <id>] [--versionCode <vc>] <package>
    • -k
      :“移除包后保留数据和缓存目录。”适用于从干净状态升级的测试,其中用户数据用于模拟已有安装。
    • --user <id>
      :按用户卸载。
    • --versionCode <vc>
      :“仅当应用具有指定的 version code 时才卸载。”可在 CI 脚本中实现幂等卸载。
    bash
    adb uninstall com.example.app                       # equivalent to: pm uninstall <pkg>
    adb shell pm uninstall -k com.example.app           # uninstall but preserve data
    adb shell pm uninstall --user 0 com.example.app     # only from primary user
  • 8. 根据需要转储 Baseline Profile。
    “从 Android 7.0(API level 24)开始,Android Runtime (ART) 会为已安装的应用收集 execution profiles……执行
    adb shell cmd package dump-profiles <package>
    ,然后执行
    adb pull /data/misc/profman/<package>.prof.txt
    。” 文档警告:“只有在拥有文件系统 root 访问权限时,才能获取 execution profile 文件名,例如在模拟器上。”在无 root 权限的用户版系统上,可使用
    androidx.benchmark
    Macrobenchmark 以编程方式提取 profile。
  • 9. 诊断常见安装失败:
    错误原因修复方法
    INSTALL_FAILED_VERSION_DOWNGRADE
    新 APK 的
    versionCode
    低于已安装版本。
    添加
    -d
    。
    INSTALL_FAILED_USER_RESTRICTED
    多用户设备:安装目标用户(通常是辅助用户或工作资料)阻止了安装。使用
    --user 0
    或
    --user current
    ;让用户在其用户配置中禁用“通过 USB 安装”限制。
    INSTALL_FAILED_UPDATE_INCOMPATIBLE
    (签名不匹配)
    新 APK 使用的签名 key 与已安装版本不同。先卸载(
    adb uninstall <pkg>
    );然后使用相同的 key 重新签名。
    INSTALL_FAILED_TEST_ONLY
    APK 包含
    android:testOnly="true"
    ,但未指定
    -t
    。
    添加
    -t
    (常见于
    app-androidTest.apk
    )。
    INSTALL_FAILED_INVALID_APK
    (仅限 split APK)
    尝试安装 split APK 集合中的单个 APK。使用
    adb install-multiple
    。
    INSTALL_FAILED_INSUFFICIENT_STORAGE
    存储空间不足。执行
    adb shell df /data
    并清理空间。
  • 10. 正确处理 shell 引用(Platform Tools 23+ 规则)。 以下内容摘自 adb 页面:
    “在 Android Platform Tools 23 及更高版本中,
    adb
    处理参数的方式与
    ssh(1)
    命令相同。...
    adb shell setprop key 'two words'
    现在会报错 ... 若要让命令正常工作,需要进行两次引用,一次用于本地 shell,一次用于远程 shell,就像使用
    ssh(1)
    时一样。例如,
    adb shell setprop key \"'two words'\"
    可以正常工作,因为本地 shell 会处理外层引用,而设备仍会看到内层引用。” 对于任何包含空格、特殊字符或 shell 元字符的
    pm
    参数,也适用相同规则。

Patterns

模式

Pattern: WRONG vs RIGHT — resetting app state between tests

模式:错误与正确——在测试之间重置应用状态

bash
undefined
bash
undefined

WRONG

WRONG

adb shell am force-stop com.example.app
adb shell am force-stop com.example.app

Run next test...

Run next test...

WRONG because: force-stop kills the process but leaves SharedPreferences,

WRONG because: force-stop kills the process but leaves SharedPreferences,

Room DB, files in /data/data/<pkg>, and cache. Tests that read any of those

Room DB, files in /data/data/<pkg>, and cache. Tests that read any of those

carry stale state into the next case. Flake follows.

carry stale state into the next case. Flake follows.


```bash

```bash

RIGHT

RIGHT

adb shell pm clear com.example.app
adb shell pm clear com.example.app

Run next test against fresh-install state.

Run next test against fresh-install state.


For test runs, the equivalent done by the runner itself is `-e clearPackageData true` to AndroidX Test Orchestrator — see `../../tests/running-instrumented-tests-via-adb/SKILL.md`.

对于测试运行,runner 自身执行的等效操作是向 AndroidX Test Orchestrator 传入 `-e clearPackageData true`——参见 `../../tests/running-instrumented-tests-via-adb/SKILL.md`。

Pattern: WRONG vs RIGHT — installing an
androidTest
APK

模式:错误与正确——安装
androidTest
APK

bash
undefined
bash
undefined

WRONG

WRONG

adb install app-androidTest.apk
adb install app-androidTest.apk

adb: failed to install app-androidTest.apk: Failure [INSTALL_FAILED_TEST_ONLY:

adb: failed to install app-androidTest.apk: Failure [INSTALL_FAILED_TEST_ONLY:

installPackageLI]

installPackageLI]

WRONG because: any APK with android:testOnly="true" (every Gradle-built test

WRONG because: any APK with android:testOnly="true" (every Gradle-built test

APK) requires
-t
.

APK) requires
-t
.


```bash

```bash

RIGHT

RIGHT

adb install -r -t -g app-androidTest.apk
adb install -r -t -g app-androidTest.apk

-r: reinstall keeping data (or overwriting); -t: allow test packages;

-r: reinstall keeping data (or overwriting); -t: allow test packages;

-g: grant all runtime permissions for hermetic tests.

-g: grant all runtime permissions for hermetic tests.

undefined
undefined

Pattern: WRONG vs RIGHT — version downgrade after a debug install

模式:错误与正确——debug 安装后发生版本降级

bash
undefined
bash
undefined

WRONG

WRONG

adb install -r app-release.apk
adb install -r app-release.apk

adb: failed to install app-release.apk: Failure [INSTALL_FAILED_VERSION_DOWNGRADE]

adb: failed to install app-release.apk: Failure [INSTALL_FAILED_VERSION_DOWNGRADE]

WRONG because: the installed debug APK has a higher versionCode than the

WRONG because: the installed debug APK has a higher versionCode than the

release APK being installed.

release APK being installed.


```bash

```bash

RIGHT

RIGHT

adb install -r -d app-release.apk # allow downgrade
adb install -r -d app-release.apk # allow downgrade

Or, when signature also changes:

Or, when signature also changes:

adb uninstall com.example.app adb install app-release.apk
undefined
adb uninstall com.example.app adb install app-release.apk
undefined

Pattern: WRONG vs RIGHT — listing only third-party packages

模式:错误与正确——仅列出第三方包

bash
undefined
bash
undefined

WRONG

WRONG

adb shell pm list packages | grep -v 'com.android'
adb shell pm list packages | grep -v 'com.android'

WRONG because: many third-party packages also have names containing

WRONG because: many third-party packages also have names containing

"com.android" (e.g. third-party Android library demos), and Google's own

"com.android" (e.g. third-party Android library demos), and Google's own

pre-installed apps carry varied prefixes (com.google.android.*).

pre-installed apps carry varied prefixes (com.google.android.*).


```bash

```bash

RIGHT

RIGHT

adb shell pm list packages -3
adb shell pm list packages -3

-3
filters to third-party (non-system) packages only.

-3
filters to third-party (non-system) packages only.

undefined
undefined

Mandatory rules

强制规则

  • MUST use
    pm clear <pkg>
    to reset app state between tests;
    am force-stop
    does NOT clear data.
  • MUST add
    -t
    when installing any test APK (
    android:testOnly="true"
    ); add
    -d
    when downgrading; add
    -g
    to grant all manifest runtime permissions.
  • MUST use
    adb install-multiple
    for split APKs (App Bundles). Single-APK install of one split fails with
    INSTALL_FAILED_INVALID_APK
    .
  • MUST NOT assume
    adb install
    of a freshly built debug APK will replace a release-signed install — signing-conflict errors require uninstall first.
  • MUST NOT rely on
    pm list packages | grep
    heuristics; use the documented filters (
    -3
    ,
    -s
    ,
    -d
    ,
    -e
    ,
    -u
    ).
  • PREFERRED: wire
    clearPackageData true
    through Test Orchestrator (
    ../../tests/running-instrumented-tests-via-adb/SKILL.md
    ) instead of running
    pm clear
    manually before every test.
  • PREFERRED: quote string arguments twice when they contain spaces or shell metacharacters — Platform Tools 23+ requires this (ssh-style quoting).
  • 必须使用
    pm clear <pkg>
    在测试之间重置应用状态;
    am force-stop
    不会清除数据。
  • 必须在安装任何测试 APK(
    android:testOnly="true"
    )时添加
    -t
    ;降级时添加
    -d
    ;添加
    -g
    以授予所有 manifest 运行时权限。
  • 必须使用
    adb install-multiple
    安装 split APK(App Bundle)。仅安装其中一个 split 会因
    INSTALL_FAILED_INVALID_APK
    而失败。
  • 不得假设
    adb install
    新构建的 debug APK 会覆盖使用 release key 签名的安装版本——签名冲突错误需要先卸载。
  • 不得依赖
    pm list packages | grep
    启发式方法;应使用已文档化的过滤器(
    -3
    、
    -s
    、
    -d
    、
    -e
    、
    -u
    )。
  • **首选:**通过 Test Orchestrator 配置
    clearPackageData true
    (
    ../../tests/running-instrumented-tests-via-adb/SKILL.md
    ),而不是在每次测试前手动运行
    pm clear
    。
  • **首选:**当字符串参数包含空格或 shell 元字符时进行两次引用——Platform Tools 23+ 要求采用此方式(ssh 风格引用)。

Verification

验证

  • adb install -r -t -g app-androidTest.apk
    returns
    Success
    .
  • adb shell pm path com.example.app
    prints a
    package:/data/app/...base.apk
    line for the just-installed package.
  • adb shell pm clear com.example.app
    returns
    Success
    , and
    adb shell run-as com.example.app ls /data/data/com.example.app
    shows only freshly created
    cache/
    and
    code_cache/
    entries.
  • adb shell pm list packages -3 | grep com.example.app
    lists the package after install.
  • After
    adb shell pm grant com.example.app android.permission.CAMERA
    ,
    adb shell dumpsys package com.example.app | grep CAMERA
    shows the permission as granted.
  • adb uninstall com.example.app
    returns
    Success
    and
    pm list packages com.example
    no longer shows the app.
  • CI run wipes app state between tests via Orchestrator (
    -e clearPackageData true
    ) or an explicit
    pm clear
    step in
    @Before
    /
    @After
    .
  • adb install -r -t -g app-androidTest.apk
    返回
    Success
    。
  • adb shell pm path com.example.app
    为刚安装的包输出一行
    package:/data/app/...base.apk
    。
  • adb shell pm clear com.example.app
    返回
    Success
    ,且
    adb shell run-as com.example.app ls /data/data/com.example.app
    仅显示新创建的
    cache/
    和
    code_cache/
    条目。
  • 安装后,
    adb shell pm list packages -3 | grep com.example.app
    会列出该包。
  • 执行
    adb shell pm grant com.example.app android.permission.CAMERA
    后,
    adb shell dumpsys package com.example.app | grep CAMERA
    会显示该权限已授予。
  • adb uninstall com.example.app
    返回
    Success
    ,并且
    pm list packages com.example
    不再显示该应用。
  • CI 运行通过 Orchestrator(
    -e clearPackageData true
    )或
    @Before
    /
    @After
    中显式的
    pm clear
    步骤,在测试之间清除应用状态。

References

参考资料

  • ADB user guide (
    pm install
    ,
    pm uninstall
    ,
    pm clear
    ,
    pm grant
    ,
    pm list packages
    ): https://developer.android.com/tools/adb#pm
  • AGP/Studio command-line testing (
    am instrument
    and orchestrator wiring): https://developer.android.com/studio/test/command-line
  • Baseline Profile dump via
    cmd package dump-profiles
    : https://developer.android.com/topic/performance/baselineprofiles
  • App Bundle / split APK install: https://developer.android.com/studio/build/build-variants
  • tasks/research/A2-adb-shell-commands.md
    — verbatim
    pm install
    /
    pm list
    /
    pm clear
    flag tables,
    force-stop
    vs
    pm clear
    distinction, ssh-style double-shell quoting since Platform Tools 23.
  • docs/CORPUS.md
    §I.10 — "force-stop does NOT clear data — pair with
    pm clear
    for hermetic reset."
  • Sibling skills:
    • High-level architecture:
      ../../architecture/understanding-adb-architecture/SKILL.md
    • Connect a device:
      ../../devices/connecting-to-devices/SKILL.md
    • Wireless ADB:
      ../../devices/connecting-over-wifi/SKILL.md
    • Run tests via
      am instrument
      (and Orchestrator
      clearPackageData
      ):
      ../../tests/running-instrumented-tests-via-adb/SKILL.md
  • Cross-set neighbours:
    • Run instrumented tests with
      AndroidJUnit4
      :
      ../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md
    • Configure JUnit4 on Android:
      ../../../jvm-tests/runner/configuring-junit4-on-android/SKILL.md
    • Source-set strategy:
      ../../../fundamentals/strategies/organizing-test-source-sets/SKILL.md
  • ADB 用户指南(
    pm install
    、
    pm uninstall
    、
    pm clear
    、
    pm grant
    、
    pm list packages
    ):https://developer.android.com/tools/adb#pm
  • AGP/Studio 命令行测试(
    am instrument
    与 Orchestrator 配置):https://developer.android.com/studio/test/command-line
  • 通过
    cmd package dump-profiles
    转储 Baseline Profile:https://developer.android.com/topic/performance/baselineprofiles
  • App Bundle / split APK 安装:https://developer.android.com/studio/build/build-variants
  • tasks/research/A2-adb-shell-commands.md
    ——逐字记录
    pm install
    /
    pm list
    /
    pm clear
    标志表、
    force-stop
    与
    pm clear
    的区别,以及 Platform Tools 23 起的 ssh 风格双层 shell 引用。
  • docs/CORPUS.md
    §I.10——“force-stop 不会清除数据——应与
    pm clear
    配合使用以实现 hermetic reset。”
  • 同级 skill:
    • 高层架构:
      ../../architecture/understanding-adb-architecture/SKILL.md
    • 连接设备:
      ../../devices/connecting-to-devices/SKILL.md
    • 无线 ADB:
      ../../devices/connecting-over-wifi/SKILL.md
    • 通过
      am instrument
      运行测试(以及 Orchestrator
      clearPackageData
      ):
      ../../tests/running-instrumented-tests-via-adb/SKILL.md
  • 跨集合关联项:
    • 使用
      AndroidJUnit4
      运行插桩测试:
      ../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md
    • 配置 Android 上的 JUnit4:
      ../../../jvm-tests/runner/configuring-junit4-on-android/SKILL.md
    • Source-set 策略:
      ../../../fundamentals/strategies/organizing-test-source-sets/SKILL.md