New features in Android Studio Preview

Android Studio Arctic Fox | 2020.3.1 has been released to the stable channel. Download it here.

Android Studio Bumblebee | 2021.1.1 is currently in the Canary and Dev channels.

Android Gradle plugin 7.0 has been released to the stable channel. For more information, see the AGP release notes.

For the latest news on releases, including a list of notable fixes in each release, also see the Release updates.

If you encounter any problems using a preview version of Android Studio, please let us know. Your bug reports help to make Android Studio better.

Android Studio Bumblebee | 2021.1.1

Support for Apple silicon

Android Studio and Android Emulator now contain initial support for core developer workflows when running the Apple silicon (arm64) hardware architecture, including corresponding emulator system images.

Support is not yet available for all SDK and NDK command-line tools. You may have to run Rosetta 2 on your machine to run some tools.

Android Testing

Android Studio Bumblebee introduces several new features and improvements to help you more reliably and scalably run your automated instrumentation tests and see useful results you can use to debug issues. These are features in addition to those launched in Android Studio Arctic Fox, such as parallel device testing and Gradle test runner for unit tests.

Unified Gradle test runner

When running instrumented tests, previous versions of Android Studio used the IntelliJ Android instrumented test runner. Meanwhile, the Android Gradle plugin uses Gradle’s own implementation of the Android instrumented test runner. So, depending on whether you run your tests from Android Studio or from the command line using the Gradle plugin, such as on your continuous integration server, you might see different test results, such as tests passing using one runner and failing on another.

Discrete test runners in older versions of Android Studio

To resolve this issue, Android Studio Bumblebee now also uses Gradle’s Android instrumented test runner when running your tests. Now, when running your automated instrumentation tests from Android Studio, your results are more likely to be consistent with those you run using the Android Gradle plugin from the command line.

Unified Gradle test runner in Bumblebee

To enable using Gradle test runner from Android Studio, do the following:

  1. Select File > Settings > Build, Execution, Deployment > Testing (or Android Studio > Preferences > Build, Execution, Deployment > Testing on MacOS.)
  2. Check the box next to Run Android instrumented tests using Gradle and click OK.

If you already have instrumented test configurations saved to your project, they’ll now use Gradle to run tests on your connected device. Otherwise, you can create a new instrumented test configuration using the gutter action next to your test class or method, as shown below.

Run tests from gutter action

When running your instrumented tests, you can confirm that Android Studio is using the Gradle test runner by inspecting the test output in the Test Matrix for Gradle task output. Although we are improving this feature with each release of Android Studio, there are some known issues, as documented below. If you experience any issues, please report a bug.

Gradle Managed Virtual Devices

In order to improve consistency, performance, and reliability when using Android Virtual Devices for your automated instrumented tests, we’re introducing Gradle Managed Virtual Devices. This feature allows you to configure virtual test devices in your project's Gradle files that the build system uses to fully manage—that is, create, deploy, and tear down—those devices to execute your automated tests.

Because this feature grants Gradle visibility into not only the tests you’re running, but also the devices’ lifecycle, it’s able to improve the quality of your testing experience in the following ways:

  • Handles device-related issues in order to ensure your tests are executed
  • Utilizes emulator snapshots to improve device startup time and memory usage, and restore devices to a clean state between tests
  • Caches test results and reruns only tests that are likely to provide different results
  • Provides a consistent environment for running your tests between local and remote test runs
Get started

You can specify a virtual device that you want Gradle to use for testing your app in your module-level build.gradle file. The following code sample creates a Pixel 2 running API level 29 as a Gradle managed device.

android {
  testOptions {
    devices {
      pixel2api29 (com.android.build.api.dsl.ManagedVirtualDevice) {
        // Use device profiles you typically see in Android Studio
        device = "Pixel 2"
        apiLevel = 29
        // You can also specify "aosp" if you don’t require Google Play Services.
        systemImageSource = "google"
        abi = "x86"
      }
    }
  }
}

To run your tests using the Gradle managed devices you configured, use the following command. device-name is the name of the device you configured in your Gradle build script (such as pixel2api29), and BuildVariant is the build variant of your app you want to test.

gradlew
-Pandroid.experimental.androidTest.useUnifiedTestPlatform=true
device-nameBuildVariantAndroidTest
Define groups of devices

To help you scale your tests across multiple device configurations, such as different API levels and form factors, you can define multiple Gradle managed devices and add them to a named group. Gradle can then execute your tests across all the devices in the group in parallel.

The example below shows two managed devices added to a device group called phoneAndTablet.

testOptions {
    devices {
      pixel2api29 (com.android.build.api.dsl.ManagedVirtualDevice) { ... }
      nexus9api30 (com.android.build.api.dsl.ManagedVirtualDevice) { ... }
    deviceGroups {
      phoneAndTablet {
        targetDevices.addAll(devices.pixel2api29, devices.nexus9api30)
      }
    }
  }
}

To run your tests using the group of Gradle managed devices, use the following command.

gradlew -Pandroid.experimental.androidTest.useUnifiedTestPlatform=true
group-nameGroupBuildVariantAndroidTest
Compatibility with Emulator Snapshots for test failures

Test runs using Gradle managed devices are compatible with Emulator snapshots for test failures. That is, when you enable snapshots for test failures when running tests with managed devices, Gradle will also generate a snapshot for each test assertion failure, so that you can use them to debug the issue later. To use managed virtual devices with Emulator snapshots for test failures enabled, simply include the command-line flag to enable snapshots.

$ gradlew
-Pandroid.experimental.androidTest.useUnifiedTestPlatform=true
-Pandroid.experimental.testOptions.emulatorSnapshots.maxSnapshotsForTestFailures=2
phoneAndTabletGroupDebugAndroidTest

Keep in mind, to use the snapshots, you’ll need to import the test results into Android Studio.

Emulator Snapshots for test failures

When you inspect failed assertions after running instrumented tests, it's sometimes difficult or time consuming to reproduce that test failure again. However, reproducing those failures on your connected device is critical in helping you diagnose the issue, either by attaching a debugger or interacting with the app at the time of failure.

When using Android Studio Bumblebee with In Android Gradle plugin 7.0-alpha13 and higher, you can use Emulator Snapshots for test failures – a quicker way to reproduce test failures when using the Android Emulator. In other words, when your instrumented tests running on a connected Emulator encounter a Java or Kotlin assertion that fails, the device takes a snapshot at the time of failure. After your test run completes, you can then reload the snapshot on the same device in order to reproduce and investigate that failure.

To get started, first update the Android Emulator to 30.4.3 or higher, then follow these steps:

  1. Select File > Settings > Build, Execution, Deployment > Testing (or Android Studio > Preferences > Build, Execution, Deployment > Testing on a MacOS.)
  2. Check the box next to Run Android instrumented tests using Gradle and click OK.
  3. Select Run > Edit Configurations … from the menu bar.
  4. If you don’t already have an instrumented test configuration, create an instrumented test configuration you want to run.
  5. Open the instrumented test configuration and select Yes from the dropdown next to Enable Emulator Snapshots for test failures.
  6. By default, Android Studio generates up to two snapshots per device, per test run. To change this maximum number of snapshots, enter the desired amount next to Max Snapshots per test run.
  7. If you want to compress snapshots, check the box next to Compress snapshots.
  8. Click OK.
  9. Deploy your tests to a virtual device as you normally would.

Enable Emulator Snapshots for test failures

The Run window should appear and show you the results of your test run. To load a snapshot, select a failed test that indicates Test results in the column under the device name. In the right side of the tool window, select the Retention tab. After Android Studio validates the snapshot for that test failure, click the Start retention debug button to load the snapshot to a device. You can then attach a debugger to investigate the cause of the issue.

Automated Test Snapshots window

Enable snapshots for command-line tests

You can enable emulator snapshots for tests you run from the command line in one of two ways: by enabling it in your app module’s build.gradle file or by passing parameters to the Gradle connectedCheck task. To enable the feature in your Gradle buildscript, include the following in your app module’s build.gradle file.

android {
      ...
    testOptions {
      emulatorSnapshots {
        // Enables the feature
        enableForTestFailures true
        // The number of snapshots to take during a test run. When not
        // specified, the default value is 2. After reaching the max number
        // of snapshots, subsequent assertion failures in a test run do not
        // generate additional snapshots. Keep in mind, snapshots can consume
        // a significant amount of disk space.
        maxSnapshotsForTestFailures 2
      }
      ...
    }
}

Next, start the Android Virtual Device you'd like to use and run your app's instrumented tests from the command line, as follows:

Linux and Mac

./gradlew connectedCheck

Windows

gradlew connectedCheck

Alternatively, if you don’t want to enable the feature in your app's build.gradle file, you can simply pass the following properties in the command line:

./gradlew
-Pandroid.experimental.androidTest.useUnifiedTestPlatform=true
-Pandroid.experimental.testOptions.failureRetention=2
connectedCheck
Import test failures into Android Studio

If you’re running your tests from outside of Android Studio, you need to import your test results into the IDE in order to use any of the generated snapshots.After the tests complete, you should see test results in the <project>/<module>/build/outputs/ androidTest-results/connected/ folder. If your test run encountered an assertion failure, you should see a tar snapshot file for each snapshot that was taken. To see the results in Android Studio and load the snapshots to your running virtual device, select Run > Import Tests from File from the menu bar, navigate to the test-result.pb file from the test results directory, and click Open.

The Run window should appear and show you the results of your test run.

Compress snapshots

Snapshots provide high fidelity in reproducing your test failures, but they each consume a large amount of disk space. If you want to instead generate compressed snapshots, add the following to your build.gradle file. Keep in mind, compressing snapshots increases the time it takes to complete the process and continue with the test execution.

android {
      ...
    testOptions {
      failureRetention {
        ...
        // Generates snapshots that are compressed into a single TAR file.
        compressSnapshots true
      }
      ...
    }
}

Alternatively, you can pass the following property from the command line.

gradlew
-Pandroid.experimental.testOptions.compressSnapshots=true
connectedCheck