Robolectric - Package targetSdkVersion=30 > maxSdkVersion=29

Both the above answers work fine, but the latest version of robolectric supports android sdk 30.

So just upgrade the robolectric dependency to fix the issue.

At the time of writing, the latest version was 4.5.1.

For the up to date version please refer the official github site.


There are two ways to achieve this:

  1. Creating prop file (mentioned in Abhimanyu's answer)

    • Create robolectric.properties in app/src/test/resources
    • Add sdk=29 in it
  2. Limit your test target SDK using Config annotation. This makes you consider different configs for different tests or avoid creating extra config files.

@Config(sdk = [29])
class Test {
 // ...
}

Found this post with the same issue, with a different error message.
This answer helped me to solve the issue.

Posting the same here.

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

sdk=29  

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

Note: Robolectric supports up to SDK 29 now (As on Sep 4th, 2020).