Dagger code giving NoClassDefFoundError in tests on the Android hardware platform, but works in a regular application

I am using a dagger in an android application. It works in the application, but when I run the test cases, I get a NoClassDefFoundError.

I use gradle and espresso. This happens WITHOUT progaurd.

This is strange because the Module $$ ModuleAdapter module is loading, but the "Module $$ ModuleAdapter $ EndpointProvidesAdapter" is not.

I pulled the APK back from the device and used dexdump to make sure the class is indeed in the APK, "Module $$ ModuleAdapter $ EndpointProvidesAdapter".

Any ideas on what could 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) 
+6
source share
2 answers

Double espresso is now deprecated in favor of Espresso 2.0. This may have worked for you because Jake did a good job of listing transitive dependencies that you might have to exclude in order to make things work.

In my experience, using Espresso 2.0 with a 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 the espresso dependencies your project includes.

+5
source

This seems to be more related to the way I turned on espresso than the dagger problem ...

 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'm still not sure why this would raise a NoClassDefFoundError for this dagger-created class.

+3
source

All Articles