Loading...
Loading...
Use this skill to run instrumented Android tests directly through `adb shell am instrument -w -r` without going through Gradle. Covers the required `-w` (wait — REQUIRED for meaningful exit codes) and `-r` (raw output) flags, the `-e` argument table (`class`, `class#method`, `package`, `size`, `numShards`/`shardIndex`, `debug`, `annotation` / `notAnnotation`, `listener`, `clearPackageData`, `targetInstrumentation`), the canonical runners `AndroidJUnitRunner` and `AndroidTestOrchestrator`, the orchestrator wrapping pattern (target = orchestrator, `-e targetInstrumentation <pkg>/<runner>`), and the output framing (`INSTRUMENTATION_STATUS_CODE` 1=start, 0=ok, -1=error, -2=failure, -3=ignored, -4=assumption-failure; `INSTRUMENTATION_RESULT`; `INSTRUMENTATION_CODE`). Use when the user mentions `am instrument`, `AndroidJUnitRunner`, "run tests from CI without Gradle", "Orchestrator", `clearPackageData`, `targetInstrumentation`, exit codes from `am instrument`, or `INSTRUMENTATION_STATUS_CODE`.
npx skill4agent add skydoves/android-testing-skills running-instrumented-tests-via-adbam instrumentadb shell am instrument-wadb shell am instrument -w -r -e class com.example.MyTest#myMethod ...numShardsshardIndexclearPackageData truetargetInstrumentation$?0INSTRUMENTATION_STATUS_CODE: -2../../apps/installing-and-managing-apps/SKILL.md../../devices/connecting-to-devices/SKILL.md../../devices/connecting-over-wifi/SKILL.mdAndroidJUnit4../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md./gradlew connectedDebugAndroidTesttestInstrumentationRunnerArgumentscom.example.appcom.example.app.test../../apps/installing-and-managing-apps/SKILL.md-tandroid:testOnly="true"androidx.test.orchestratorandroidTestUtil("androidx.test:orchestrator:1.6.1")adb install -r androidx.test.orchestrator.apkdevicedocs/CORPUS.mdam helpadb shell am instrument -w -r \
[-e <key> <value>] ... \
<pkg>/<runner>-w$?-rreport_key_streamresult-wandroidx.test.runner.AndroidJUnitRunnerandroidTestandroidx.test.orchestrator.AndroidTestOrchestratoradb shell am instrument -w -r \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner-e| Key | Value | Meaning |
|---|---|---|
| | "Fully qualified Java package name. Any test class using this package executes." Takes precedence over |
| | "Only this test case class executes." |
| | "Only this method executes." Hash separator. |
| | Comma-separate to combine multiple selectors. |
| | "Runs test methods annotated with |
| | Filter to tests carrying this annotation. |
| | Exclude tests carrying this annotation. |
| | Total shard count for parallel runs. |
| | This shard's 0-based index (0 ≤ i < N). |
| | "Runs tests in debug mode." Waits for debugger attach before each test. |
| | "Loads and logs all specified tests but doesn't run them." Use to verify filter combinations. |
| | Register a |
| | Register a custom JUnit |
| | Custom |
| | Disable AndroidJUnitRunner usage stats. |
| | AndroidX Orchestrator only: |
| | AndroidX Orchestrator only: the wrapped instrumentation. |
| | Collect JaCoCo coverage. |
| | Override device coverage file location. |
# One class.
adb shell am instrument -w -r \
-e class com.example.app.LoginTests \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner
# One method.
adb shell am instrument -w -r \
-e class com.example.app.LoginTests#login_succeeds \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner
# @LargeTest only.
adb shell am instrument -w -r -e size large \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner
# Shard 0 of 4 (run on 4 devices in parallel for 4× speedup).
adb shell am instrument -w -r -e numShards 4 -e shardIndex 0 \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner
# Wait for debugger before each test.
adb shell am instrument -w -e debug true \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner
# Custom RunListener.
adb shell am instrument -w -r \
-e listener com.example.app.MyRunListener \
com.example.app.test/androidx.test.runner.AndroidJUnitRunnerclearPackageDataandroidx.test.orchestrator/.AndroidTestOrchestrator-e targetInstrumentation <pkg>/<runner>adb shell am instrument -w -r \
-e clearPackageData true \
-e targetInstrumentation com.example.app.test/androidx.test.runner.AndroidJUnitRunner \
androidx.test.orchestrator/.AndroidTestOrchestratortargetInstrumentationINSTRUMENTATION_STATUS: <key>=<value> # repeated per Bundle entry per status frame
INSTRUMENTATION_STATUS_CODE: <int> # one per status frame
...
INSTRUMENTATION_RESULT: <key>=<value> # repeated per Bundle entry of the final result
INSTRUMENTATION_CODE: <int> # final, exactly onceInstrumentation.REPORT_VALUE_RESULT_*| Code | Meaning |
|---|---|
| Test started ( |
| Test passed ( |
| Process error / unexpected throw ( |
| Assertion failure ( |
| Ignored ( |
| Assumption failure ( |
-3-410-1-2INSTRUMENTATION_STATUS_CODEINSTRUMENTATION_CODEActivity.RESULT_*-1RESULT_OK0RESULT_CANCELED$?finish(Activity.RESULT_OK, results)frameworks/base/cmds/am/.../Instrument.javarun()System.exit(0)$?0INSTRUMENTATION_STATUS_CODE: -2INSTRUMENTATION_STATUS_CODE: -1InstrumentationREPORT_KEY_NAME_CLASSREPORT_KEY_NAME_TESTREPORT_KEY_NUM_CURRENTREPORT_KEY_NUM_TOTALREPORT_KEY_STACKREPORT_KEY_STREAMRESULT-r-w$?0am instrumentSystem.exit(0)finish()RESULT_OK-w$?INSTRUMENTATION_STATUS_CODE: -2-1-w -radb shell am instrument -w -r ... \
| tee instrument.log
grep -E '^INSTRUMENTATION_(CODE|STATUS_CODE|RESULT)' instrument.log
grep '^INSTRUMENTATION_STATUS_CODE: -2' instrument.log && exit 1app/build/outputs/androidTest-results/connected/<variant>/TEST-*.xml./gradlew connectedDebugAndroidTestam instrumentdocs/CORPUS.mdadb shell settings put global window_animation_scale 0
adb shell settings put global transition_animation_scale 0
adb shell settings put global animator_duration_scale 0
adb shell pm clear com.example.app # before each run; orchestrator does this per-testtestOptions.animationsDisabled = true-e clearPackageData trueadb shell$?adb shell <cmd>echo $? > /sdcard/exitadb shell <cmd>; echo $?am instrument0-w# WRONG
adb shell am instrument -r \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner
echo $?
# 0
# WRONG because: -w is omitted, so the adb shell returns immediately. $?
# reflects the launch, not the test outcome. Failures in the test run are
# silently lost.# RIGHT — gate on stdout, NOT on $?
output=$(adb shell am instrument -w -r \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner)
if echo "$output" | grep -qE 'INSTRUMENTATION_STATUS_CODE: -[12]$'; then
echo "FAILED"; exit 1
fi
# am instrument exits 0 even when tests fail (System.exit(0) in AOSP Instrument.java),
# so the only reliable signal is parsing INSTRUMENTATION_STATUS_CODE: -2 (failure) or -1 (error).targetInstrumentation# WRONG
adb shell am instrument -w -r \
-e clearPackageData true \
-e targetInstrumentation androidx.test.orchestrator/.AndroidTestOrchestrator \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner
# WRONG because: the orchestrator is the runner, not the target. AndroidJUnitRunner
# does not know how to invoke an "orchestrator target", and clearPackageData is
# not a recognised arg of AndroidJUnitRunner — only of the orchestrator.# RIGHT
adb shell am instrument -w -r \
-e clearPackageData true \
-e targetInstrumentation com.example.app.test/androidx.test.runner.AndroidJUnitRunner \
androidx.test.orchestrator/.AndroidTestOrchestrator
# Orchestrator is the runner (last positional arg).
# AndroidJUnitRunner is the wrapped target (passed via -e targetInstrumentation).# WRONG
adb -s emulator-5554 shell am instrument -w -r -e numShards 2 -e shardIndex 0 \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner &
adb -s emulator-5554 shell am instrument -w -r -e numShards 2 -e shardIndex 1 \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner &
wait
# WRONG because: both shards run on the same device (-s emulator-5554). They
# serialize against each other through am, defeating the parallelism goal of
# sharding. Worse, they may collide on the same app under test.# RIGHT
adb -s emulator-5554 shell am instrument -w -r -e numShards 2 -e shardIndex 0 \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner &
adb -s emulator-5556 shell am instrument -w -r -e numShards 2 -e shardIndex 1 \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner &
wait
# One shard per device. Each device runs roughly half the suite.# WRONG
adb shell am instrument -w -r -e class com.example.app.LoginTests:login_succeeds \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner
# WRONG because: the separator between class and method is `#`, not `:`.
# AndroidJUnitRunner treats the whole string as a class name and finds nothing.# RIGHT
adb shell am instrument -w -r -e class com.example.app.LoginTests#login_succeeds \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner-wam instrument$?am instrumentSystem.exit(0)-w-rreport_key_streamresult<class>#<method>-e targetInstrumentationam instrument-wclearPackageData trueclearPackageData truetestOptions.execution = "ANDROIDX_TEST_ORCHESTRATOR"settings put global *_animation_scale 0testOptions.animationsDisabled = true$?am instrument0System.exit(0)Instrument.javafinish(RESULT_OK, …)INSTRUMENTATION_STATUS_CODE: -1-2INSTRUMENTATION_CODE: <int>INSTRUMENTATION_STATUS_CODE: 10-1-2-3-4androidx.test.orchestrator/.AndroidTestOrchestrator-e targetInstrumentation-e numShards N -e shardIndex i-s-e class <fqcn>#<method>INSTRUMENTATION_STATUS: numtests=10.0adb shell settings get global window_animation_scaleam instrumentam instrumentInstrumentationtasks/research/A2-adb-shell-commands.md-e-3-4-wdocs/CORPUS.mdam instrument-w../../architecture/understanding-adb-architecture/SKILL.md../../devices/connecting-to-devices/SKILL.md../../devices/connecting-over-wifi/SKILL.md../../apps/installing-and-managing-apps/SKILL.mdAndroidJUnit4../../../instrumentation/runner/running-instrumented-tests-with-androidjunit4/SKILL.md../../../jvm-tests/runner/configuring-junit4-on-android/SKILL.md../../../fundamentals/strategies/organizing-test-source-sets/SKILL.md