Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.3: NoSuchMethodError ImmutableList.sortedCopyOf #5147

Closed
schroederm opened this issue Jun 27, 2019 · 19 comments
Closed

4.3: NoSuchMethodError ImmutableList.sortedCopyOf #5147

schroederm opened this issue Jun 27, 2019 · 19 comments

Comments

@schroederm
Copy link

Description

We've been running robolectric 4.1 flawlessly in our project. When we try to upgrade to 4.3, a lot of tests fail with the following output:

java.lang.NoSuchMethodError: com.google.common.collect.ImmutableList.sortedCopyOf(Ljava/util/Comparator;Ljava/lang/Iterable;)Lcom/google/common/collect/ImmutableList;

at org.robolectric.internal.bytecode.ShadowProviders.<init>(ShadowProviders.java:25)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.robolectric.util.inject.Injector.inject(Injector.java:239)
at org.robolectric.util.inject.Injector.lambda$memoized$1(Injector.java:221)
at org.robolectric.util.inject.Injector$MemoizingProvider.get(Injector.java:485)
at org.robolectric.util.inject.Injector.getInstanceInternal(Injector.java:213)
at org.robolectric.util.inject.Injector.getInstance(Injector.java:197)
at org.robolectric.util.inject.Injector.getInstance(Injector.java:191)
at org.robolectric.internal.SandboxTestRunner.<init>(SandboxTestRunner.java:75)
at org.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:101)
at org.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:96)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
at com.intellij.junit4.JUnit46ClassesRequestBuilder.collectWrappedRunners(JUnit46ClassesRequestBuilder.java:86)
at com.intellij.junit4.JUnit46ClassesRequestBuilder.getClassesRequest(JUnit46ClassesRequestBuilder.java:47)
at com.intellij.junit4.JUnit4TestRunnerUtil.buildRequest(JUnit4TestRunnerUtil.java:90)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:46)
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)

Is this a problem on our side; if so, any suggestions to fix it?

Steps to Reproduce

Change our build.gradle to include testImplementation 'org.robolectric:robolectric:4.3'

Robolectric & Android Version

Robolectric 4.3
compileSdkVersion 28
minSdkVersion 16
targetSdkVersion 28

@brettchabot
Copy link
Contributor

Looks like .ImmutableList.sortedCopyOf API was introduced in guava 21
https://guava.dev/releases/snapshot-jre/api/docs/com/google/common/collect/ImmutableList.html#sortedCopyOf-java.lang.Iterable-

Try to ensure your build is picking up at least com.google.guava:guava:27.0.1-jre.

@RishabhTayal
Copy link

RishabhTayal commented Jul 11, 2019

I am facing this same issue. I don't have any dependency on com.google.guava:guava in my project.

@schroederm were you able to resolve this issue?

@schroederm
Copy link
Author

Sorry for not updating this here.
Yes, I was able to resolve it by adding the dependency to guava by hand (we didn't use it before either).

Seems a bit strange to me that a dependency to it was seemingly added without resolving it at framework level.

@RishabhTayal
Copy link

RishabhTayal commented Jul 12, 2019

So are you saying we should add testImplementation 'com.google.guava:guava:27.0.1-jre' and androidTestImplementation 'com.google.guava:guava:27.0.1-jre' to our app/build.gradle file and it should resolve this issue?

@schroederm
Copy link
Author

What I'm saying is I added api 'com.google.guava:guava:28.0-android' to my gradle file and it resolved the issue. No guarantees here, but not improbable.

@RishabhTayal
Copy link

wow, looks like this is working. I have been banging my head for 3 days. Thanks for the solution 👍 🎆

@schroederm
Copy link
Author

Sure thing, thanks for reminding me to add the answer here.

@kmadhuri18
Copy link

super man.. u saved my life...

@UmeshChakre
Copy link

I am facing this same issue. I also don't have any dependency on com.google.guava:guava in my project.
and i have added
api 'com.google.guava:guava:28.0-android' dependency now, but it is still not working,
it says initialization error.

java.lang.NoSuchMethodError: com.google.common.collect.ImmutableList.sortedCopyOf(Ljava/util/Comparator;Ljava/lang/Iterable;)Lcom/google/common/collect/ImmutableList; at org.robolectric.internal.bytecode.ShadowProviders.<init>(ShadowProviders.java:25) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.robolectric.util.inject.Injector.inject(Injector.java:239) at org.robolectric.util.inject.Injector.lambda$memoized$1(Injector.java:221) at org.robolectric.util.inject.Injector$MemoizingProvider.get(Injector.java:485) at org.robolectric.util.inject.Injector.getInstanceInternal(Injector.java:213) at org.robolectric.util.inject.Injector.getInstance(Injector.java:197) at org.robolectric.util.inject.Injector.getInstance(Injector.java:191) at org.robolectric.internal.SandboxTestRunner.<init>(SandboxTestRunner.java:75) at org.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:101) at org.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:96) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104) at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

@hoisie
Copy link
Contributor

hoisie commented Aug 17, 2020

Does your project depend on jar files without using gradle? Seems like there could be multiple gradle versions on your classpath and it is using an older one.

@Prat-JD
Copy link

Prat-JD commented Nov 8, 2021

I'm able run unit test through test runner and getting coverage as well. However when I try to run with JaCoCo through CLI gradlew jacocoTestReportDebug --debug, getting error.

  java.lang.NoSuchMethodError: com.google.common.collect.ImmutableList.sortedCopyOf(Ljava/util/Comparator;Ljava    /lang/Iterable;)Lcom/google/common/collect/ImmutableList;

   at org.robolectric.internal.bytecode.ShadowProviders.<init>(ShadowProviders.java:25)
   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
   at org.robolectric.util.inject.Injector.inject(Injector.java:250)
   at org.robolectric.util.inject.Injector.lambda$memoized$1(Injector.java:232)
   at org.robolectric.util.inject.Injector$MemoizingProvider.get(Injector.java:498)
   at org.robolectric.util.inject.Injector.getInstanceInternal(Injector.java:224)
   at org.robolectric.util.inject.Injector.getInstance(Injector.java:208)
   at org.robolectric.util.inject.Injector.getInstance(Injector.java:202)
   at org.robolectric.internal.SandboxTestRunner.<init>(SandboxTestRunner.java:78)
   at org.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:103)
   at org.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:98)
   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
   at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
   at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
   at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
   at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
   at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
   at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
   at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
   at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
   at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
   at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

I'm using

dependencies {
        classpath ("com.android.tools.build:gradle:4.1.3") {
        }
}
    testImplementation 'androidx.test:core:1.3.0'
    testImplementation 'org.mockito:mockito-core:3.4.0'
    testImplementation 'org.mockito:mockito-inline:3.4.0'
    testImplementation 'androidx.test.ext:junit:1.1.2'
    testImplementation('org.robolectric:robolectric:4.5.1') {
       // exclude group: 'com.google.guava', module: 'guava'
    }
   testImplementation 'androidx.test.ext:truth:1.3.0'
testOptions {

        animationsDisabled true

        unitTests {
            includeAndroidResources = true
        }
        unitTests.all {
            //systemProperty 'robolectric.enabledSdks', '28'
            // jvmArgs '-noverify'
            //systemProperty 'user.home', System.getenv('HOME')

            jacoco {
                includeNoLocationClasses = true
            }
        }
        unitTests.returnDefaultValues = true
    }

compileSdkVersion 28
    //buildToolsVersion "30.0.3"

    //testOptions.unitTests.includeAndroidResources = true

    defaultConfig {
        applicationId "com.cepheid.epsilon"
        minSdkVersion 25
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
       
    }

Please help me resolve!

@hoisie
Copy link
Contributor

hoisie commented Nov 8, 2021

This is happening because there is more than one version of Guava on your classpath. One of them is being included by Gradle, and the other is likely part of some other jar file. You need to iterate over your jar file dependencies and figure out where the other guava is coming from, and then remove that.

@utzcoz
Copy link
Member

utzcoz commented Nov 8, 2021

This is happening because there is more than one version of Guava on your classpath. One of them is being included by Gradle, and the other is likely part of some other jar file. You need to iterate over your jar file dependencies and figure out where the other guava is coming from, and then remove that.

@Prat-JD you can follow https://stackoverflow.com/questions/21645071/using-gradle-to-find-dependency-tree to check your app's dependencies, and find which dependency uses different guava version. You can try to update this dependency or exclude specific guava from this dependency.

@manishkummar21
Copy link

I am also facing Issue

Caused by: java.lang.NoSuchMethodError: 'com.google.common.collect.ImmutableList com.google.common.collect.ImmutableList.sortedCopyOf(java.util.Comparator, java.lang.Iterable)'

app/build.gradle

testImplementation "org.robolectric:robolectric:4.7.3"

Even I added the Solution proposed by adding

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

Then I also faced the following the issue

Duplicate class com.google.common.annotations.Beta found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Duplicate class com.google.common.annotations.GwtCompatible found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Duplicate class com.google.common.annotations.GwtIncompatible found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Duplicate class com.google.common.annotations.VisibleForTesting found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Duplicate class com.google.common.base.Ascii found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Duplicate class com.google.common.base.CaseFormat found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Duplicate class com.google.common.base.CaseFormat$1 found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Duplicate class com.google.common.base.CharMatcher found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Duplicate class com.google.common.base.CharMatcher$1 found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar)
Help me on this

@manishkummar21
Copy link

@hoisie @schroederm

@Prat-JD
Copy link

Prat-JD commented Dec 14, 2021

I am also facing Issue

Caused by: java.lang.NoSuchMethodError: 'com.google.common.collect.ImmutableList com.google.common.collect.ImmutableList.sortedCopyOf(java.util.Comparator, java.lang.Iterable)'

app/build.gradle

testImplementation "org.robolectric:robolectric:4.7.3"

Even I added the Solution proposed by adding

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

Then I also faced the following the issue

Duplicate class com.google.common.annotations.Beta found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Duplicate class com.google.common.annotations.GwtCompatible found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Duplicate class com.google.common.annotations.GwtIncompatible found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Duplicate class com.google.common.annotations.VisibleForTesting found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Duplicate class com.google.common.base.Ascii found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Duplicate class com.google.common.base.CaseFormat found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Duplicate class com.google.common.base.CaseFormat$1 found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Duplicate class com.google.common.base.CharMatcher found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Duplicate class com.google.common.base.CharMatcher$1 found in modules jetified-guava-28.0-android (com.google.guava:guava:28.0-android) and jetified-guava-r07 (guava-r07.jar) Help me on this

@manishkummar21 I was getting this issue because there were multiple different version of guava available in my project. One of them was from .jar file and few from gradle dependencies. As @utzcoz and @hoisie mentioned, you need to find out through dependency tree and remove. Try to keep only one guava version if possible.

@manishkummar21
Copy link

@utzcoz and @hoisie Is there any way to exclude?

@utzcoz
Copy link
Member

utzcoz commented Dec 14, 2021

@utzcoz and @hoisie Is there any way to exclude?

What about trying https://stackoverflow.com/questions/30648172/gradle-library-duplicates-in-dependencies?

@UmeshChakre
Copy link

Does your project depend on jar files without using gradle? Seems like there could be multiple gradle versions on your classpath and it is using an older one.

Yes my project depend on jar files, i could not figure out whether there is different version of Guava or not . i looked at previous answers but i could not figure out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants