我在Android应用程序中使用Dagger。 它在应用程序中工作,但是当我运行检测测试时,我得到一个NoClassDefFoundError。
我正在使用Gradle和Espresso。这是在没有progaurd的情况下发生的。
这很奇怪,因为" Module $$ ModuleAdapter"加载,但"模块$$ ModuleAdapter $ EndpointProvidesAdapter"不是。
我将APK从设备中拉回来并使用dexdump验证该类确实在APK中,"模块$$ ModuleAdapter $ EndpointProvidesAdapter"。
关于可能导致此问题的任何想法?
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)
答案 0 :(得分:5)
Double Espresso现已弃用,转而使用Espresso 2.0。它可能对你有用,因为Jake在枚举传递依赖关系方面做得很好,你可能需要排除这些依赖关系以使事情有效。
根据我的经验,使用带有Dagger的Espresso 2.0可能需要您从espresso依赖项中排除javax.inject:
androidTestCompile ('com.android.support.test.espresso:espresso-core:2.0') {
exclude group: 'javax.inject'
}
您可能需要为项目包含的所有espresso依赖项执行此操作。
答案 1 :(得分:3)
这似乎与我加入Espresso的方式有关,而不是Dagger问题......
androidTestCompile ('com.google.android.apps.common.testing:espresso:1.1' ){
exclude group: 'com.squareup.dagger'
}
切换到Jake Wharton"双浓咖啡"让问题消失了。
https://github.com/JakeWharton/double-espresso
我仍然不确定为什么会在Dagger生成的类上导致NoClassDefFoundError。