简体   繁体   English

在Android库项目中使用Dagger2在运行时抛出`NoClassDefFoundError`

[英]Use Dagger2 in android library project throws `NoClassDefFoundError` at runtime

I'm using dagger2 in one of my android library. 我在我的一个Android库中使用了dagger2。 After finish the library project, I use gradle uploadArchive to upload an aar to a maven repo. 完成库项目后,我使用gradle uploadArchivegradle uploadArchive上传到Maven存储库。 then use the compile group:artifect:version@aar syntax in an app project to consume the library project. 然后在应用程序项目中使用compile group:artifect:version@aar语法来使用库项目。 I have no problem building the app, but at runtime, it throws NoClassDefFoundError for the class MyModule_ProvideAppFactory generated my Dagger2. 我构建应用程序没有问题,但是在运行时,它为MyModule_ProvideAppFactory类生成的Dagger2抛出NoClassDefFoundError I figure this class is generated for the method @Provides public Application provideApp() . 我认为此类是为@Provides public Application provideApp()方法生成的。 Yet the generated DaggerMyComponent can be found at runtime, since the exception happens inside DaggerMyComponent class. 但是生成的DaggerMyComponent可以在运行时找到,因为该异常发生在DaggerMyComponent类内部。 I even included the following dependencies in the app project 我什至在应用程序项目中包括以下依赖项

compile 'com.google.dagger:dagger:2.0'
compile 'javax.annotation:jsr250-api:1.0'

Yet the problem still existed. 但是问题仍然存在。 I'm pretty sure not because of the 65K method limits problem because we have enabled multidex in the app project. 我很确定不是因为65K method limits问题,因为我们在应用程序项目中启用了multidex。 I'm not sure what I've been missing, or can Dagger2 be used this way? 我不确定自己所缺少的内容,还是可以使用Dagger2?

Turned out I was missing another dependency which are required by the classes generated by Dagger2. 原来,我错过了Dagger2生成的类所需的另一个依赖项。 Add compile 'javax.inject:javax.inject:1' 添加compile 'javax.inject:javax.inject:1'

to the gradle file and it works fine. 到gradle文件,它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM