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

java.lang.NoClassDefFoundError: Failed resolution of: dagger/internal/Preconditions #403

Closed
luispereira opened this issue Jul 5, 2016 · 11 comments

Comments

@luispereira
Copy link

luispereira commented Jul 5, 2016

I'm using dagger on a library project and everything foes well on the sample app with the lib project. Altough, when I try to import the generated aar for the library the follow error happens in runtime when I run the application and the method pointing to the dagger injection on the library is called:

  java.lang.NoClassDefFoundError: Failed resolution of: Ldagger/internal/Preconditions;
   at com.test.dagger.DaggerTestComponent$Builder.testModule(DaggerTestComponent.java:186)
   at com.test.dagger.Injector.initializeApplicationComponent(Injector.java:31)
   at com.test.app.Test.<init>(Test.java:48)
   at com.test.app.Test.setup(Test.java:71)
   at com.test.SampleApp.onCreate(SampleApp.java:24)
   at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1014)
   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4751)
   at android.app.ActivityThread.-wrap1(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5461)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassNotFoundException: Didn't find class "dagger.internal.Preconditions" on path: DexPathList[[zip file "/data/app/com.test.testtest-1/base.apk"],nativeLibraryDirectories=[/data/app/com.test.testtest-1/lib/arm, /vendor/lib, /system/lib]]
   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
   at com.test.dagger.DaggerTestComponent$Builder.testModule(DaggerTestComponent.java:186) 
   at com.test.dagger.Injector.initializeApplicationComponent(Injector.java:31) 
   at com.test.app.Test.<init>(Test.java:48) 
   at com.test.app.Test.setup(Test.java:71) 
   at com.test.testtest.SampleApp.onCreate(SampleApp.java:24) 
   at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1014) 
   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4751) 
   at android.app.ActivityThread.-wrap1(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5461) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Suppressed: java.lang.ClassNotFoundException: dagger.internal.Preconditions
   at java.lang.Class.classForName(Native Method)
   at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
   at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
      ... 16 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

I'm only using the following dependencies on the app:

dependencies {
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.android.support:design:23.0.0'
    compile project(':Test_1.0.0-debug')   //this is the library (aar imported to the module) containing the dagger 2 annotations and modules
}

Also I'm Using release 2.4. of dagger

And I believe it is not a multidex issue :/

@ronshapiro
Copy link

Can you post all versions of dagger that you use in your codebase? both dagger and dagger-compiler?

@luispereira
Copy link
Author

Sure, @ronshapiro, I use both

apt "com.google.dagger:dagger-compiler:2.4"
compile "com.google.dagger:dagger:2.4"

@ronshapiro
Copy link

This is almost definitely a gradle/android toolchain issue, not a dagger one. A probable workaround is to add compile "com.google.dagger:dagger:2.4" to the main project's build.gradle

@luispereira
Copy link
Author

Thanks @ronshapiro, now that you mention I remembered that aars do not import dependencies and therefore dagger is not imported, sorry about this.

@andronicus-kim
Copy link

@luispereira please let me know how you resolved this issue. I got the same error but i can find a work around. i will really appreciate.

@luispereira
Copy link
Author

luispereira commented Apr 6, 2018

Hi @andronicus-kim, the problem here is that .aar files do not import used dependencies. This means that every dependency you use inside your .aar file you have to import it on your main project.

So in your main project, I had to do something like this:

dependencies {
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.android.support:design:23.0.0'
    apt "com.google.dagger:dagger-compiler:2.4" //apt is deprecated now is kapt or annotationProcessor
    compile "com.google.dagger:dagger:2.4"
    compile project(':Test_1.0.0-debug')  //your aar or your project
}

@andronicus-kim
Copy link

@luispereira Thanks a lot for your response, by main project you mean importing my dependencies to project level build.gradle ?
kindly share some gist on this if you have one, or some sample code

@andronicus-kim
Copy link

Hi @luispereira for more clarity, i encountered this error in my app not a library, i'm not sure if that will make a difference

@luispereira
Copy link
Author

@andronicus-kim, the example I shared was the import of the aar. on your project, if you use dagger dependency on your .aar project you need to use also on the project that you import the .aar

or simply do compile ('your aar'){transitive=true}

@andronicus-kim
Copy link

@luispereira Thanks a lot.

@masterwok
Copy link

This doesn't make any sense to me. Why does the consumer of a dependency need to also import the dependencies of the dependency?

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

4 participants