43

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?

Abhimanyu
  • 11,351
  • 7
  • 51
  • 121

5 Answers5

54

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

Abhimanyu
  • 11,351
  • 7
  • 51
  • 121
  • 5
    When you implement Android 12. It's the same solution for the newest error `Package targetSdkVersion=31 > maxSdkVersion=30` Robolectric does not support the prerelease Android S. compile https://github.com/robolectric/robolectric/issues/6635 – kuzdu Aug 19 '21 at 14:10
33

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 {
 // ...
}
Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117
11

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.

rajkumar21
  • 183
  • 3
  • 9
2

Just update your robolectric version to 4.5 or newer, their repo notified this change:

Robolectric 4.5 adds support for Android API 30 (R final) and contains many bug fixes and other enhancements.

Source: https://github.com/robolectric/robolectric/releases

0

failed to configure com.example.android.architecture.blueprints.todoapp.tasks.TasksViewModelTest.addNewTask_setsNewTaskEvent: Package targetSdkVersion=31 > maxSdkVersion=30 java.lang.IllegalArgumentException: failed to configure com.example.android.architecture.blueprints.todoapp.tasks.TasksViewModelTest.addNewTask_setsNewTaskEvent: Package targetSdkVersion=31 > maxSdkVersion=30

Had an almost similar error as above. The error was generated when I was using AndroidX Test and Robolectic APIs to execute my local unit tests. I also had included the below code to allow The testing API use the right Android manifest file during testing:

 testOptions.unitTests {
    includeAndroidResources = true
}

I solved the above error by downgrading my compile and target sdk from 31 to 30