Question

Robolectric - Package targetSdkVersion=30 > maxSdkVersion=29

Error:

java.lang.IllegalArgumentException: failed to configure : Package targetSdkVersion=30 > maxSdkVersion=29

at org.robolectric.RobolectricTestRunner.getChildren(RobolectricTestRunner.java:247) at org.junit.runners.ParentRunner.getFilteredChildren(ParentRunner.java:534) at org.junit.runners.ParentRunner.getDescription(ParentRunner.java:400) at androidx.test.ext.junit.runners.AndroidJUnit4.getDescription(AndroidJUnit4.java:149) at org.junit.runners.model.RunnerBuilder.configureRunner(RunnerBuilder.java:81) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:72) at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:37) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70) at org.junit.internal.requests.ClassRequest.createRunner(ClassRequest.java:28) at org.junit.internal.requests.MemoizingRequest.getRunner(MemoizingRequest.java:19) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:50) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53) Caused by: java.lang.IllegalArgumentException: Package targetSdkVersion=30 > maxSdkVersion=29 at org.robolectric.plugins.DefaultSdkPicker.configuredSdks(DefaultSdkPicker.java:118) at org.robolectric.plugins.DefaultSdkPicker.selectSdks(DefaultSdkPicker.java:69) at org.robolectric.RobolectricTestRunner.getChildren(RobolectricTestRunner.java:213) ... 13 more

How to fix this issue?

 46  14378  46
1 Jan 1970

Solution

 56

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).

2020-09-04

Solution

 34

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 {
 // ...
}
2020-10-28