java.lang.NoSuchMethodError: com.google.common.base.CharMatcher.whitespace()

java.lang.NoSuchMethodError: com.google.common.base.CharMatcher.whitespace()

我正在用 Robolectric 编写 android 单元测试。当我 运行 当前测试时,我不断收到上述错误。

我从这里查看并尝试了大量建议的解决方案,但其中 none 已经解决了这个错误。

到目前为止,我查看了依赖关系树,可以看到正在使用两个版本的番石榴:

+--- com.google.guava:guava-jdk5:13.0@jar
+--- com.google.guava:guava:27.0.1-android@jar

注意到这一点后,我尝试:

  1. 排除第一个依赖项

    buildscript {
      repositories {
      google()
      jcenter()
     }
       dependencies {
       classpath 'com.android.tools.build:gradle:3.0.1'
       classpath 'com.google.gms:google-services:4.0.1'
       classpath ('org.robolectric:robolectric:4.1') {
        exclude group: 'com.google.guava', module: 'guava-jdk5'
       }
    
      }
    }
    
  2. 升级guava版本到最新

    api 'com.google.guava:guava:27.0.1-android'
    

这是错误的样子:

java.lang.NoSuchMethodError: com.google.common.base.CharMatcher.whitespace()Lcom/google/common/base/CharMatcher;

at org.robolectric.res.StringResources.processStringResources(StringResources.java:19)
at org.robolectric.res.StaxValueLoader.onEnd(StaxValueLoader.java:33)
at org.robolectric.res.StaxDocumentLoader.doParse(StaxDocumentLoader.java:66)
at org.robolectric.res.StaxDocumentLoader.loadResourceXmlFile(StaxDocumentLoader.java:30)
at org.robolectric.res.DocumentLoader.loadFile(DocumentLoader.java:48)
at org.robolectric.res.DocumentLoader.load(DocumentLoader.java:27)
at org.robolectric.res.ResourceTableFactory.parseResourceFiles(ResourceTableFactory.java:149)
at org.robolectric.res.ResourceTableFactory.lambda$newFrameworkResourceTable[=13=](ResourceTableFactory.java:26)
at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:50)
at org.robolectric.res.ResourceTableFactory.newFrameworkResourceTable(ResourceTableFactory.java:12)
at org.robolectric.internal.SdkEnvironment.getSystemResourceTable(SdkEnvironment.java:35)
at org.robolectric.ApkLoader.getSystemResourceTable(ApkLoader.java:32)
at org.robolectric.android.internal.ParallelUniverse.injectResourceStuffForLegacy(ParallelUniverse.java:252)
at org.robolectric.android.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:126)
at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:379)
at org.robolectric.internal.SandboxTestRunner.evaluate(SandboxTestRunner.java:252)
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[=13=]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)

这是我目前的测试class:

@RunWith(RobolectricTestRunner.class)
public class MyTestClass {

MyTestFragment myTestFragment;

 @Test
public void fragmentIsNotNull() throws Exception{
    myTestFragment = MyTestFragment.newInstance();
    assertNotNull(myTestFragment);
  }
}

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

我终于能够按照 SO 上有类似问题的人给出的答案来解决这个问题。

问题是我上面指出的两个相互冲突的番石榴依赖项。

+--- com.google.guava:guava-jdk5:13.0@jar
+--- com.google.guava:guava:27.0.1-android@jar

这是我之前试过的,排除番石榴,

configurations {
        all*.exclude group: 'com.google.guava', module:'guava-jdk5'
      }

但 guava-jdk-5 依赖关系仍在维护中。

所以我最终将其添加到我的 gradle 文件中:

compile('com.google.guava:guava-jdk5:17.0') { force = true }

终于解决了这个bug。