Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

Set up project for Android Test

In order to use Android Test, you need to include several testing specific libraries. This guide explains how to declare these libraries in your app's project files and shows how Android Test libraries are organized.

Add Android Test libraries

In order to use Android Test, you must modify your app project's classpath dependencies and manifest within your development environment, as shown in the following sections.

Add Gradle dependencies

To modify your app project's classpath dependencies, complete the following steps:

  1. Open the build.gradle file for your app.
  2. In the repositories section, add Google's Maven repository. To do so, add a maven section with the "https://maven.google.com" endpoint. For example:

    allprojects {
        repositories {
            jcenter()
            maven {
                url "https://maven.google.com"
            }
        }
    }
  3. For each Android 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:

    dependencies {
        ...
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }

    Caution: Using dynamic dependencies (for example, espresso-core:3.0.0+) can cause unexpected version updates and regression incompatibilities. We recommend that you explicitly specify a particular version number (for example, espresso-core:3.0.0).

  4. If your app builds tests that rely on JUnit-based classes, such as Assert and TestSuiteLoader, add the following lines in the android section of the file:
    android {
        ...
    
        // Gradle automatically adds 'android.test.runner' as a dependency.
        useLibrary 'android.test.runner'
    
        useLibrary 'android.test.base'
        useLibrary 'android.test.mock'
    }

    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.

Add manifest declarations

To run tests that rely on JUnit-based classes, add the necessary <uses-library> elements to your test app's manifest. For example, if you add tests that depend on the android.test.runner library, add the following element to your app's manifest:

<!-- You don't need to include android:required="false" if your app's
     minSdkVersion is 28 or higher. -->
<uses-library android:name="android.test.runner"
              android:required="false" />

To determine the library that contains a given JUnit-based class, see JUnit-based libraries.

Considerations when targeting Android 9.0 or higher

The guidance in this section applies only if you target Android 9.0 (API level 28) or higher and the minimum SDK version for your app is set to Android 9.0 (API level 28) or higher.

The android.test.runner library implicitly depends on the android.test.base and android.test.mock libraries. If your app only uses classes from android.test.base or android.test.mock, you can include the libraries by themselves:

<!-- For both of these declarations, you don't need to include
     android:required="false" if your app's minSdkVersion is 28
     or higher. -->
<uses-library android:name="android.test.base"
              android:required="false" />
<uses-library android:name="android.test.mock"
              android:required="false" />

List of Android Test dependencies

Android Test includes many dependencies, as shown in the following sections.

Gradle dependencies

The following Gradle-based dependencies are available in Android Test:

dependencies {
  // AndroidJUnitRunner and JUnit Rules
  androidTestImplementation 'com.android.support.test:runner:1.0.2'
  androidTestImplementation 'com.android.support.test:rules:1.0.2'

  // Espresso dependencies
  androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
  androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
  androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
  androidTestImplementation 'com.android.support.test.espresso:espresso-accessibility:3.0.2'
  androidTestImplementation 'com.android.support.test.espresso:espresso-web:3.0.2'
  androidTestImplementation 'com.android.support.test.espresso.idling:idling-concurrent:3.0.2'

  // 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 'com.android.support.test.espresso:espresso-idling-resource:3.0.2'
}

As discussed in Adding Gradle dependencies from a Maven repository, you can add these dependencies to your development environment by adding each dependency within your module-level build.gradle file.

JUnit-based libraries

The lists in this section show how the platform organizes JUnit-based classes in Android 9.0 (API level 28) and higher.

android.test.base

This library consists of the following classes, each of which is in the junit.framework package:

The library also consists of the following classes that are deprecated as of Android 9.0 (API level 28):

android.test package

android.test.suitebuilder.annotation package

com.android.internal.util package

android.test.runner

This library consists of the following classes:

android.test package

junit.runner package

The library also consists of the following classes that are deprecated as of Android 9.0 (API level 28):

android.test package

android.test.suitebuilder package

android.test.mock

This library consists of the following classes, each of which is in the android.test.mock package:

The library also consists of the following classes that are deprecated as of Android 9.0 (API level 28). Each class is located in the android.test.mock package: