Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 989
Abhimanyu Avatar asked Sep 04 '20 07:09

Abhimanyu


3 Answers

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

like image 197
Abhimanyu Avatar answered Nov 07 '22 16:11

Abhimanyu


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 {
 // ...
}
like image 31
Mahdi-Malv Avatar answered Nov 07 '22 16:11

Mahdi-Malv


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.

like image 11
rajkumar21 Avatar answered Nov 07 '22 14:11

rajkumar21