Loading...
Loading...
Use this skill to stand up an Android instrumentation test source set with the canonical `AndroidJUnit4` runner, the correct `AndroidJUnitRunner` Gradle wiring, and supporting infrastructure (Test Orchestrator, runtime permission grants, size annotations, SDK suppression, hermetic animation defaults). Covers `androidx.test:core:1.7.0` / `:runner:1.7.0` / `:rules:1.7.0` / `androidx.test.ext:junit:1.3.0` coordinates, `androidx.test.platform.app.InstrumentationRegistry`, and the `androidTestUtil` configuration trap for `androidx.test:orchestrator`. Use when the user reports `Test runner not found`, `AndroidJUnit4 deprecated`, `InstrumentationRegistry deprecated`, asks "why is my orchestrator not running", "how do I pass `-e` args to the test", "how do I grant a runtime permission in a test", or "instrumented test won't compile".
npx skill4agent add skydoves/android-testing-skills running-instrumented-tests-with-androidjunit4AndroidJUnit4androidTestImplementationandroidTestUtilandroidx.test.InstrumentationRegistryandroidx.test.platform.app.InstrumentationRegistryandroidx.test.runner.AndroidJUnit4@Deprecatedam instrumentINSTRUMENTATION_FAILED: <pkg>/androidx.test.runner.AndroidJUnitRunnerTest runner not found-e size small-e package com.fooam instrumentRunnerArgsACCESS_FINE_LOCATIONRECORD_AUDIOadbActivity../../scenarios/launching-activities-with-activityscenario/SKILL.md../../espresso/writing-espresso-tests/SKILL.md../../uiautomator/cross-app-tests-with-uiautomator/SKILL.md../../../jvm-tests/runner/configuring-junit4-on-android/SKILL.md../../../fundamentals/strategies/applying-testing-strategies/SKILL.mdcom.android.applicationcom.android.librarysrc/androidTest/./gradlew :module:connectedDebugAndroidTestjunit:junit:4.13.2androidTestImplementationdocs/CORPUS.mddependencies {
androidTestImplementation("androidx.test:core:1.7.0")
androidTestImplementation("androidx.test:runner:1.7.0")
androidTestImplementation("androidx.test:rules:1.7.0")
androidTestImplementation("androidx.test.ext:junit:1.3.0")
androidTestImplementation("androidx.test.ext:junit-ktx:1.3.0")
androidTestImplementation("androidx.test.ext:truth:1.7.0")
}androidx.test.ext:junitAndroidJUnit4ActivityScenarioRuleandroidx.test:runnerAndroidJUnit4tasks/research/R1-androidx-test-core.mdtestInstrumentationRunnerandroid.defaultConfigandroidx.test.runner.AndroidJUnitRunnerAndroidJUnit4tasks/research/R1-androidx-test-core.mdandroid {
defaultConfig {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
// Optional: pass `-e key value` args to every run via Gradle
testInstrumentationRunnerArguments["clearPackageData"] = "true"
}
testOptions {
animationsDisabled = true // hermetic — see Pattern below
}
}@RunWith(AndroidJUnit4::class)androidx.test.ext.junit.runnersimport androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class LoginActivityTest {
@Test fun loginSucceeds() { /* ... */ }
}androidx.test.runner.AndroidJUnit4@Deprecatedrunner-1.7.0/androidx/test/runner/AndroidJUnit4.java:43-44androidx.test.platform.app.InstrumentationRegistryandroidx.test.InstrumentationRegistryplatform.app@Deprecated@InlineMetasks/research/R1-androidx-test-core.mdimport androidx.test.platform.app.InstrumentationRegistry
import androidx.test.core.app.ApplicationProvider
val instrumentation = InstrumentationRegistry.getInstrumentation()
val args: Bundle = InstrumentationRegistry.getArguments() // -e key value extras
val targetContext: Context = ApplicationProvider.getApplicationContext()
val testContext: Context = instrumentation.context // package context of the test APKInstrumentationRegistry.getArguments()-eam instrument -e key value-e size small -e numShards 4runner-1.7.0/androidx/test/internal/runner/RunnerArgs.javatasks/research/R1-androidx-test-core.md@SmallTest@MediumTest@LargeTestandroidx.test.filtersam instrument -e size smallSmallTestMediumTestLargeTest@FlakyTest@RequiresDevice@Suppressimport androidx.test.filters.SmallTest
import androidx.test.filters.SdkSuppress
@RunWith(AndroidJUnit4::class)
@SmallTest
class LoginViewModelTest { /* ... */ }
@Test
@SdkSuppress(minSdkVersion = 26)
fun usesApi26OnlyApi() { /* ... */ }@SdkSuppress(minSdkVersion, maxSdkVersion, excludedSdks, codeName)runner-1.7.0/androidx/test/filters/SdkSuppress.java:39-58-4androidTestUtilandroidTestImplementation@TestclearPackageDatadependencies {
androidTestUtil("androidx.test:orchestrator:1.6.1") // installs orchestrator APK on device
androidTestUtil("androidx.test:services:1.6.0") // required for clearPackageData
}
android {
testOptions {
execution = "ANDROIDX_TEST_ORCHESTRATOR"
}
defaultConfig {
testInstrumentationRunnerArguments["clearPackageData"] = "true"
}
}androidTestUtilandroidTestImplementationtasks/research/R1-androidx-test-core.mdGrantPermissionRule.grant(...)adb shell pm grantandroidx.test:rulesimport androidx.test.rule.GrantPermissionRule
import org.junit.Rule
@get:Rule
val permissionRule: GrantPermissionRule = GrantPermissionRule.grant(
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.RECORD_AUDIO,
)rules-1.7.0/androidx/test/rule/GrantPermissionRule.javaUiAutomation.grantRuntimePermissiontestOptions { animationsDisabled = true }window_animation_scaletransition_animation_scaleanimator_duration_scale0@Rule@Rule(order = N)@HiltAndroidRuleorder = 0@get:Rule(order = 0) val hiltRule = HiltAndroidRule(this)
@get:Rule(order = 1) val activityRule = ActivityScenarioRule(LoginActivity::class.java)./gradlew :app:connectedDebugAndroidTest
# or via adb directly:
adb shell am instrument -w -r \
-e size small \
-e clearPackageData true \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner-w$?0docs/CORPUS.mdAndroidJUnit4// WRONG
import androidx.test.runner.AndroidJUnit4 // @Deprecated since androidx.test:runner 1.x
@RunWith(AndroidJUnit4::class)
class FooTest
// WRONG because: this class is @Deprecated at runner-1.7.0/.../AndroidJUnit4.java:43-44.
// The IDE flags it; codemods replace it; the doc explicitly redirects to ext.junit.// RIGHT
import androidx.test.ext.junit.runners.AndroidJUnit4
@RunWith(AndroidJUnit4::class)
class FooTestInstrumentationRegistry// WRONG
import androidx.test.InstrumentationRegistry // @Deprecated, every member @InlineMe'd
val ctx = InstrumentationRegistry.getTargetContext() // returns target package context, NOT the application
// WRONG because: monitor-1.8.0/androidx/test/InstrumentationRegistry.java:34 is @Deprecated.
// Use androidx.test.platform.app.InstrumentationRegistry + ApplicationProvider.// RIGHT
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.core.app.ApplicationProvider
val instrumentation = InstrumentationRegistry.getInstrumentation()
val ctx: Context = ApplicationProvider.getApplicationContext()// WRONG
dependencies {
androidTestImplementation("androidx.test:orchestrator:1.6.1")
}
android.testOptions.execution = "ANDROIDX_TEST_ORCHESTRATOR"
// WRONG because: orchestrator must be installed as a separate APK on the device.
// `androidTestImplementation` puts it on the test APK classpath instead, where it does
// nothing. Tests still run in one shared process. No error, just silent inactivation.// RIGHT
dependencies {
androidTestUtil("androidx.test:orchestrator:1.6.1")
androidTestUtil("androidx.test:services:1.6.0") // required for clearPackageData
}
android.testOptions.execution = "ANDROIDX_TEST_ORCHESTRATOR"RunnerArgs# Run a single method
adb shell am instrument -w -r \
-e class com.example.LoginActivityTest#loginSucceeds \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner
# Filter by size annotation
adb shell am instrument -w -r -e size small com.example.app.test/androidx.test.runner.AndroidJUnitRunner
# Shard across N runners (CI fan-out)
adb shell am instrument -w -r -e numShards 4 -e shardIndex 0 \
com.example.app.test/androidx.test.runner.AndroidJUnitRunner-erunner-1.7.0/androidx/test/internal/runner/RunnerArgs.java:54-99classpackagenotClasssizeannotationnotAnnotationnumShardsshardIndexclearPackageDatacoveragedebuglistenerInstrumentationRegistry.getArguments()androidx.test.ext.junit.runners.AndroidJUnit4@RunWith(...)androidx.test.runner.AndroidJUnit4@Deprecatedandroidx.test.platform.app.InstrumentationRegistryandroidx.test.InstrumentationRegistry@Deprecatedandroidx.test:orchestratorandroidTestUtilandroidTestImplementationtestInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"-wam instrument$?Thread.sleeponCreateonStartActivityScenario.moveToState../../scenarios/launching-activities-with-activityscenario/SKILL.mdIdlingResourceadb shell pm grantGrantPermissionRule.grant(...)@Rule public ActivityTestRule@Deprecated@get:Rule val rule = ActivityScenarioRule(...)@SmallTest@MediumTest@LargeTesttestOptions.animationsDisabled = true./gradlew :<module>:connectedDebugAndroidTestTest runner not foundINSTRUMENTATION_FAILEDandroidx.test.runner.AndroidJUnit4androidx.test.InstrumentationRegistrygrep -r "androidTestImplementation.*orchestrator" build.gradle*androidTestUtiladb shell am instrument -w -r -e size small <pkg>.test/androidx.test.runner.AndroidJUnitRunneradb logcat | grep AndroidTestOrchestratortestOptions.animationsDisabled = trueandroid@get:Rule val rule = GrantPermissionRule.grant(...)adb shell pm grantrunner-1.7.0/androidx/test/runner/AndroidJUnit4.java@Deprecated public final class AndroidJUnit4 extends Runnerjunit-1.3.0/androidx/test/ext/junit/runners/AndroidJUnit4.javaAndroidJUnit4ClassRunnerrunner-1.7.0/androidx/test/runner/AndroidJUnitRunner.javaam instrumentrunner-1.7.0/androidx/test/internal/runner/RunnerArgs.java-emonitor-1.8.0/androidx/test/InstrumentationRegistry.java@Deprecated@InlineMemonitor-1.8.0/androidx/test/platform/app/InstrumentationRegistry.javarules-1.7.0/androidx/test/rule/GrantPermissionRule.javastatic GrantPermissionRule grant(String...)runner-1.7.0/androidx/test/filters/SdkSuppress.javaminSdkVersionmaxSdkVersionexcludedSdkscodeNametasks/research/R1-androidx-test-core.mdAndroidJUnit4docs/CORPUS.md../../managed-devices/running-tests-on-gradle-managed-devices/SKILL.md../../../adb/tests/running-instrumented-tests-via-adb/SKILL.mdadb shell am instrument -w -r../../../platform/legacy/migrating-from-android-test-classes/SKILL.mdandroid.test.*InstrumentationTestRunner