dependencies{// Core libraryandroidTestImplementation"androidx.test:core:$androidXTestVersion0"// AndroidJUnitRunner and JUnit RulesandroidTestImplementation"androidx.test:runner:$testRunnerVersion"androidTestImplementation"androidx.test:rules:$testRulesVersion"// AssertionsandroidTestImplementation"androidx.test.ext:junit:$testJunitVersion"androidTestImplementation"androidx.test.ext:truth:$truthVersion"// Espresso dependenciesandroidTestImplementation"androidx.test.espresso:espresso-core:$espressoVersion"androidTestImplementation"androidx.test.espresso:espresso-contrib:$espressoVersion"androidTestImplementation"androidx.test.espresso:espresso-intents:$espressoVersion"androidTestImplementation"androidx.test.espresso:espresso-accessibility:$espressoVersion"androidTestImplementation"androidx.test.espresso:espresso-web:$espressoVersion"androidTestImplementation"androidx.test.espresso.idling:idling-concurrent:$espressoVersion"// The following Espresso dependency can be either "implementation",// or "androidTestImplementation", depending on whether you want the// dependency to appear on your APK’"s compile classpath or the test APK// classpath.androidTestImplementation"androidx.test.espresso:espresso-idling-resource:$espressoVersion"}
Kotlin
dependencies{// Core libraryandroidTestImplementation("androidx.test:core:$androidXTestVersion")// AndroidJUnitRunner and JUnit RulesandroidTestImplementation("androidx.test:runner:$testRunnerVersion")androidTestImplementation("androidx.test:rules:$testRulesVersion")// AssertionsandroidTestImplementation("androidx.test.ext:junit:$testJunitVersion")androidTestImplementation("androidx.test.ext:truth:$truthVersion")// Espresso dependenciesandroidTestImplementation("androidx.test.espresso:espresso-core:$espressoVersion")androidTestImplementation("androidx.test.espresso:espresso-contrib:$espressoVersion")androidTestImplementation("androidx.test.espresso:espresso-intents:$espressoVersion")androidTestImplementation("androidx.test.espresso:espresso-accessibility:$espressoVersion")androidTestImplementation("androidx.test.espresso:espresso-web:$espressoVersion")androidTestImplementation("androidx.test.espresso.idling:idling-concurrent:$espressoVersion")// The following Espresso dependency can be either "implementation",// or "androidTestImplementation", depending on whether you want the// dependency to appear on your APK"s compile classpath or the test APK// classpath.androidTestImplementation("androidx.test.espresso:espresso-idling-resource:$espressoVersion")}
<!--Youdon't need to include android:required="false" if your app'sminSdkVersionis28orhigher.-->
<uses-libraryandroid:name="android.test.runner"android:required="false"/>
<!--Forbothofthesedeclarations,youdon't need to include android:required="false" if your app'sminSdkVersionis28orhigher.-->
<uses-libraryandroid:name="android.test.base"android:required="false"/>
<uses-library android:name="android.test.mock" android:required="false" />
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-08。"],[],[],null,["# Set up project for AndroidX Test\n\nAndroidX Test is a collection of Jetpack libraries that lets you run tests\nagainst Android apps. It also provides a series of tools to help you write these\ntests.\n\nFor example, AndroidX Test provides JUnit4 rules to start activities and\ninteract with them in JUnit4 tests. It also contains UI Testing frameworks such\nas Espresso, UI Automator and the Robolectric simulator.\n\nAdd AndroidX Test libraries\n---------------------------\n\nIn order to use AndroidX Test, you must modify your app project's dependencies\nwithin your development environment.\n\n### Add Gradle dependencies\n\nTo modify your app project's dependencies, complete the following steps:\n\n- **Step 1** : Open the `build.gradle` file for your Gradle module.\n- **Step 2**: In the repositories section, make sure Google's Maven repository appears:\n\n allprojects {\n repositories {\n jcenter()\n google()\n }\n }\n\n- **Step 3** : For each AndroidX Test package you want to use, add its package name to the dependencies section. For example, to add the `espresso-core` package, add the following lines:\n\n### Groovy\n\n```groovy\ndependencies {\n ...\n androidTestImplementation \"androidx.test.espresso:espresso-core:$espressoVersion\"\n }\n```\n\n### Kotlin\n\n```kotlin\ndependencies {\n ...\n androidTestImplementation('androidx.test.espresso:espresso-core:$espressoVersion')\n }\n```\n\nThese are the most common AndroidX Test dependencies available: \n\n### Groovy\n\n```groovy\ndependencies {\n // Core library\n androidTestImplementation \"androidx.test:core:$androidXTestVersion0\"\n\n // AndroidJUnitRunner and JUnit Rules\n androidTestImplementation \"androidx.test:runner:$testRunnerVersion\"\n androidTestImplementation \"androidx.test:rules:$testRulesVersion\"\n\n // Assertions\n androidTestImplementation \"androidx.test.ext:junit:$testJunitVersion\"\n androidTestImplementation \"androidx.test.ext:truth:$truthVersion\"\n\n // Espresso dependencies\n androidTestImplementation \"androidx.test.espresso:espresso-core:$espressoVersion\"\n androidTestImplementation \"androidx.test.espresso:espresso-contrib:$espressoVersion\"\n androidTestImplementation \"androidx.test.espresso:espresso-intents:$espressoVersion\"\n androidTestImplementation \"androidx.test.espresso:espresso-accessibility:$espressoVersion\"\n androidTestImplementation \"androidx.test.espresso:espresso-web:$espressoVersion\"\n androidTestImplementation \"androidx.test.espresso.idling:idling-concurrent:$espressoVersion\"\n\n // The following Espresso dependency can be either \"implementation\",\n // or \"androidTestImplementation\", depending on whether you want the\n // dependency to appear on your APK'\"s compile classpath or the test APK\n // classpath.\n androidTestImplementation \"androidx.test.espresso:espresso-idling-resource:$espressoVersion\"\n}\n```\n\n### Kotlin\n\n```kotlin\ndependencies {\n // Core library\n androidTestImplementation(\"androidx.test:core:$androidXTestVersion\")\n\n // AndroidJUnitRunner and JUnit Rules\n androidTestImplementation(\"androidx.test:runner:$testRunnerVersion\")\n androidTestImplementation(\"androidx.test:rules:$testRulesVersion\")\n\n // Assertions\n androidTestImplementation(\"androidx.test.ext:junit:$testJunitVersion\")\n androidTestImplementation(\"androidx.test.ext:truth:$truthVersion\")\n\n // Espresso dependencies\n androidTestImplementation( \"androidx.test.espresso:espresso-core:$espressoVersion\")\n androidTestImplementation( \"androidx.test.espresso:espresso-contrib:$espressoVersion\")\n androidTestImplementation( \"androidx.test.espresso:espresso-intents:$espressoVersion\")\n androidTestImplementation( \"androidx.test.espresso:espresso-accessibility:$espressoVersion\")\n androidTestImplementation( \"androidx.test.espresso:espresso-web:$espressoVersion\")\n androidTestImplementation( \"androidx.test.espresso.idling:idling-concurrent:$espressoVersion\")\n\n // The following Espresso dependency can be either \"implementation\",\n // or \"androidTestImplementation\", depending on whether you want the\n // dependency to appear on your APK\"s compile classpath or the test APK\n // classpath.\n androidTestImplementation( \"androidx.test.espresso:espresso-idling-resource:$espressoVersion\")\n}\n```\n\nThe [Release Notes](/jetpack/androidx/releases/test) page contains a table with the latest versions per\nartifact.\n| **Note:** It's important to ensure these testing dependencies point to the correct source set. Usually AndroidX Test is needed in instrumentation tests only, so you would use `androidTestImplementation()`. However, in cases such as with `espresso-idling-resource`, the APIs are used from production code, requiring you to use the implementation function.\n\nRefer to the [Package Index](/reference/androidx/test/packages) or [Class Index](/reference/androidx/test/classes) for specific reference\ndocumentation on these libraries.\n\nProjects using deprecated classes\n---------------------------------\n\n| **Warning:** If you build instrumentation tests using Gradle, you receive additional support. When auto-generating the test manifest, the Android Gradle Plugin adds the following libraries and manifest elements to your project automatically so you don't need to take these steps.\n\nIf your app uses tests that rely on deprecated JUnit3-based `android.test`\nclasses , such as [`InstrumentationTestCase`](/reference/android/test/InstrumentationTestCase) and [`TestSuiteLoader`](/reference/junit/runner/TestSuiteLoader), add\nthe following lines in the `android` section of the file: \n\n android {\n ...\n useLibrary 'android.test.runner'\n\n useLibrary 'android.test.base'\n useLibrary 'android.test.mock'\n }\n\n| **Note:** You only need to include the libraries that contain the classes used in your app. For a list of the classes that appear in each library, see [JUnit-based libraries](/training/testing/instrumented-tests/androidx-test-libraries/test-setup).\n\n### Add manifest declarations\n\nTo run tests that rely on deprecated JUnit3-based `android.test` classes, add\nthe necessary [`\u003cuses-library\u003e`](/guide/topics/manifest/uses-library-element) elements to your test app's manifest. For\nexample, if you add tests that depend on the `android.test.runner` library, add\nthe following element to your app's manifest: \n\n \u003c!-- You don't need to include android:required=\"false\" if your app's\n\n minSdkVersion is 28 or higher. --\u003e\n\n \u003cuses-library android:name=\"android.test.runner\"\n\n android:required=\"false\" /\u003e\n\nTo determine the library that contains a given JUnit-based class, see\n[JUnit-based libraries](/training/testing/instrumented-tests/androidx-test-libraries/test-setup).\n| **Caution:** Avoid deprecated classes where possible. Use supported JUnit-based classes in their place.\n\n#### Considerations when using deprecated classes and targeting Android 9 or\n\nhigher\n\nThe guidance in this section applies only if you target Android 9 (API level 28)\nor higher *and* the minimum SDK version for your app is set to Android 9.\n\nThe `android.test.runner` library implicitly depends on the `android.test.base`\nand `android.test.mock` libraries. If your app only uses classes from\n`android.test.base` or `android.test.mock`, you can include the libraries by\nthemselves: \n\n \u003c!-- For both of these declarations, you don't need to include\n android:required=\"false\" if your app's minSdkVersion is 28\n or higher. --\u003e\n\n \u003cuses-library android:name=\"android.test.base\"\n android:required=\"false\" /\u003e\n \u003cuses-library android:name=\"android.test.mock\"\n android:required=\"false\" /\u003e"]]