Search code examples
androidguavadagger-2noclassdeffounderror

NoClassDefFoundError for Guava's ImmutableMap with Dagger 2 for Android


I'm getting the next crash when I launch my Android app after adding the first Dagger module.

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/common/collect/ImmutableMap;
    at com.sofaking.moonworshipper.DaggerAppComponent.getMapOfClassOfAndProviderOfFactoryOf(DaggerAppComponent.java:35)
    at com.sofaking.moonworshipper.DaggerAppComponent.getDispatchingAndroidInjectorOfActivity(DaggerAppComponent.java:41)
    at com.sofaking.moonworshipper.DaggerAppComponent.injectApp(DaggerAppComponent.java:64)
    at com.sofaking.moonworshipper.DaggerAppComponent.inject(DaggerAppComponent.java:59)
    at com.sofaking.moonworshipper.DaggerAppComponent.inject(DaggerAppComponent.java:16)

The issue seems to be identical to this one on GitHub: https://github.com/google/dagger/issues/897

I understand that Dagger is trying to use Guava, although it shouldn't - But I'm not sure what to do to fix this. It's not a proguard issue.

I tried to include guava in my dependencies - which presented the next error while compiling:

Error: Program type already present: com.google.common.util.concurrent.internal.InternalFutures

which makes sense, as there are other libraries in my code which depend on guava.

implementation 'com.google.dagger:dagger:2.15'
kapt 'com.google.dagger:dagger-compiler:2.15'

compile 'com.google.dagger:dagger-android:2.15'
compile 'com.google.dagger:dagger-android-support:2.15'
kapt 'com.google.dagger:dagger-android-processor:2.15'

// tried adding this as well, didn't work
api 'com.google.guava:guava:27.0-android'

Edit: Here's tree of resolved dependecies: https://pastebin.com/RsPPjD6H


Solution

  • After seeing that Dagger depends on com.google.guava:guava:23.3-jre, I tried adding the next line in my Gradle build file:

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

    And it works!