Robolectric 4.0 is released! Here’s what’s new!


The different idioms for testing using Robolectric and instrumentation tests has long been a headache. With today’s release of Robolectric 4.0 and androidx.test 1.0.0, both testing environments are converging on a set of common test APIs. Robolectric now supports the AndroidJUnit4 test runner, ActivityTestRule, and Espresso for interacting with UI components.

A Robolectric 3.x style test:

class RobolectricTest {
  @Test fun clickingOnTitle_shouldLaunchEditAction() {
    val activity = Robolectric.setupActivity(

Robolectric 4.x/instrumentation test:

class OnDeviceTest {
  @get:Rule val rule = ActivityTestRule(

  @Test fun clickingOnTitle_shouldLaunchEditAction() {

As you can see, many of the idioms common in instrumentation tests are now supported by Robolectric tests. In future releases we’ll be expanding androidx.test support on Robolectric, as well as introducing some Robolectric-originated testing paradigms to traditional instrumentation tests.

Binary Resources

In conjunction with Android Studio 3.2, Robolectric can now use resources processed using the Android build toolchain, and loads and handles those resources using the same logic as on an actual Android device. Robolectric’s old idiosyncratic resource handling mode is still available for projects not yet using the latest version of the build toolchain, but is now deprecated and will be removed in a future release.

To enable the use of toolchain-processed resources in Robolectric tests, make sure you’re using Android Gradle Plugin version or higher, and add the following to your

android {
  testOptions {
    unitTests {
      includeAndroidResources = true

dependencies {
  testImplementation 'org.robolectric:robolectric:4.0.2'

Add this line to your (no longer necessary with Android Studio 3.3+):


Migration Tool

Robolectric 4.0 contains a number of API changes that may require modifications to your test code. Check out the migration notes and use the new automated migration tool to help convert your existing tests to be compatible with Robolectric 4.0.

Release Notes

Release notes are available here.

As always, thanks for your pull requests, bug reports, ideas and questions! 💯

Your Robolectric maintainers,,, and