Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger code giving NoClassDefFoundError in Android instrumentation tests, but works in the normal application

I am using Dagger in an Android application. It is working in the Application but when I run the instrumentation tests, I am getting a NoClassDefFoundError.

I am using Gradle and Espresso. This is happening WITHOUT progaurd.

This is strange since the "Module$$ModuleAdapter" getting loaded , but "Module$$ModuleAdapter$EndpointProvidesAdapter" is not.

I pulled the APK back off the device and used dexdump to verify that the class is indeed in the APK, "Module$$ModuleAdapter$EndpointProvidesAdapter".

Any ideas on what might be causing this?

 java.lang.NoClassDefFoundError: Module$$ModuleAdapter$EndpointProvidesAdapter
            at ...Module$$ModuleAdapter.getBindings(MslModule$$ModuleAdapter.java:33)
            at ...Module$$ModuleAdapter.getBindings(MslModule$$ModuleAdapter.java:13)
            at dagger.ObjectGraph$DaggerObjectGraph.makeGraph(ObjectGraph.java:185)
            at dagger.ObjectGraph$DaggerObjectGraph.access$000(ObjectGraph.java:138)
            at dagger.ObjectGraph.create(ObjectGraph.java:129)
            at ...Application.onCreate(...Application.java:21)
            at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4344)
            at android.app.ActivityThread.access$1500(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassNotFoundException: Didn't find class ...Module$$ModuleAdapter$MslEndpointProvidesAdapter" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/....test-1.apk", zip file "/data/app/...-2.apk"],nativeLibraryDirectories=[/data/app-lib/....test-1, /data/app-lib/...-2, /vendor/lib, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
            at ...Module$$ModuleAdapter.getBindings(MslModule$$ModuleAdapter.java:33)
            at ...Module$$ModuleAdapter.getBindings(MslModule$$ModuleAdapter.java:13)
            at dagger.ObjectGraph$DaggerObjectGraph.makeGraph(ObjectGraph.java:185)
            at dagger.ObjectGraph$DaggerObjectGraph.access$000(ObjectGraph.java:138)
            at dagger.ObjectGraph.create(ObjectGraph.java:129)
            at ...eApplication.onCreate(...Application.java:21)
            at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4344)
            at android.app.ActivityThread.access$1500(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
like image 451
yogurtearl Avatar asked Apr 30 '14 23:04

yogurtearl


2 Answers

Double Espresso is now deprecated in favor of Espresso 2.0. It's possible that it worked for you because Jake's done a good job of enumerating transitive dependencies that you may need to exclude in order to get things working.

In my experience, using Espresso 2.0 with Dagger may require you to exclude javax.inject from your espresso dependencies:

    androidTestCompile ('com.android.support.test.espresso:espresso-core:2.0') {
    exclude group: 'javax.inject'
}

You may need to do this for all of the espresso dependencies that your project includes.

like image 187
jdonmoyer Avatar answered Nov 06 '22 22:11

jdonmoyer


This seems to be more related to the way I was including Espresso than it is a Dagger issue...

androidTestCompile ('com.google.android.apps.common.testing:espresso:1.1' ){
        exclude group: 'com.squareup.dagger'
}

Switching to Jake Wharton's "double-espresso" made the problem go away.

https://github.com/JakeWharton/double-espresso

I am still not sure why that would cause a NoClassDefFoundError on that Dagger generated class.

like image 3
yogurtearl Avatar answered Nov 06 '22 23:11

yogurtearl