Robolectric and Android SDK 29

If anyone is wondering, the solution for me was to annotate my test classes with

@Config(sdk = Build.VERSION_CODES.O_MR1)

With Robolectric 4.3.1 we can use the following alternatives:

  1. Run our test with Java 9 or newer. (We can edit the JRE in the run test configuration) enter image description here

If we like to run our test with Java 8, we can:

  1. Annotate our test class with @Config in order to emulate a lower SDK (as mentioned by @bencri and @julio-mendoza).

Like this:

@RunWith(AndroidJUnit4::class)
@Config(sdk = Build.VERSION_CODES.P)
class LoginRobolectricTest {
//...
}
  1. Or, as mentioned by @farmerbb, add the robolectric.properties file inside the app/src/test/resources folder with the SDK we like to run, by example:

sdk=28

enter image description here


Create a robolectric.properties file inside the app/src/test/resources directory with the following line:

sdk=28

This will force Robolectric to use API 28 instead of 29.