Robolectric 4.0.2 test are not running in gradle 3.2.1 with NoSuchMethodError: setUseLegacyResources(Z)V

Robolectric 4.0.2 test are not running in gradle 3.2.1 with NoSuchMethodError: setUseLegacyResources(Z)V

我已将我的 Gradle 插件版本从 2.3.3 升级到 3.2.1,但我的 robolectric 测试开始失败。所以我将 robolectric 版本升级到 4.0.2,并在 Documentation.

中进行了给定的更改

但是我的所有测试仍然无法执行并出现此错误:

java.lang.NoSuchMethodError: org.robolectric.RuntimeEnvironment.setUseLegacyResources(Z)V

    at org.robolectric.android.internal.ParallelUniverse.setResourcesMode(ParallelUniverse.java:81)
    at org.robolectric.RobolectricTestRunner.configureSandbox(RobolectricTestRunner.java:235)
    at org.robolectric.internal.SandboxTestRunner.evaluate(SandboxTestRunner.java:230)
    at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:130)
    at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:42)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=10=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.robolectric.internal.SandboxTestRunner.evaluate(SandboxTestRunner.java:84)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

这是我迄今为止尝试过的方法,但没有任何效果。

1.https://github.com/robolectric/robolectric/issues/4271

2.Robolectric: NullPointerException in setupActivity()

3.https://github.com/robolectric/robolectric/issues/2579

我已经提交了 issue

如有任何帮助,我们将不胜感激。

我的问题通过这个答案解决了

Gradle 配置正在获取不同版本的 robolectric 依赖项

预计我们不需要显式依赖资源或影子核心,因为它们会被不及物地引入。所以尝试以下

robolectricVersion = '4.0.2'
robolectric : "org.robolectric:robolectric:${robolectricVersion}",
shadowsSupport : "org.robolectric:shadows-support-v4:${robolectricVersion}",
shadowsMultidex: "org.robolectric:shadows-multidex:${robolectricVersion}",