错误:error.NonExistentClass Kotlin 在多模块 Dagger 项目中 [英] ERROR : error.NonExistentClass Kotlin In multi module Dagger project
问题描述
我使用 Dagger 2 和 Kotlin 进行 Android 开发.我的项目也是多模块项目.我的settings.gradle文件是这样的:
I'm using Dagger 2 and Kotlin for Android development. My project is also a multi-module project. My settings.gradle file is like this:
include :app
include :lib
我也在维护 lib 模块.
I'm also maintaining the lib module.
在 Dagger 文件中(例如在组件中),我尝试从其他模块中获取项目.例如:
In the Dagger Files (for example in the component), I try to get the item from other module. For example:
@Component
interface AppComponent{
fun getPresenter() : Presenter
}
Presenter 对象在 lib 模块中定义.我在 linux 环境中工作,我使用的是 Android Studio 3 preview canary 5.代码运行良好,我能够生成 APK.
The Presenter object is defined in lib module. I was working in linux environment and I'm using Android Studio 3 preview canary 5. The code is working well and I am able to generate APK.
但我公司想使用稳定版的 Android Studio 生成 APK.我使用的是 Android Studio 2.3.3.
But my company wanted to generate the APK using stable version of Android Studio. I'm using Android Studio 2.3.3.
编译Android项目时,遇到这个错误:
When compiling the Android Project, I encountered this error:
error: error.NonExistentClass
错误出现在
:app:kaptDebugKotlin
是因为找不到dagger类导致的,该类是在其他项目中定义的.可能的解决方法是什么?抱歉我的英语不好.
is performed and caused by the dagger class cannot found, the class is defined in the other project. What can be the possible workaround for this? Sorry for my bad English.
推荐答案
根本原因
基本上,在使用 kapt
时,没有什么可以解决这个问题.引用 此链接 来解决另一个使用 pre- 处理器(OrmaDatabase):
The Root Cause
Basically, there's not much that can be done to fix this when using kapt
. To quote this link that tackles the same problem in another library that uses pre-processors (OrmaDatabase):
因为 Kotlin 在 Java Annotation Processing 运行之前制作它的存根,Kotlin 对 OrmaDatabase 一无所知,而且它的名称存根中的声明将是 error.NonExistentClass.这打破了注释处理工具.这是一种kapt限制
Because Kotlin makes its stubs before Java Annotation Processing runs, Kotlin knows just nothing about OrmaDatabase, and the name of the declaration in stubs will be error.NonExistentClass. This breaks the Annotation Processing tool. It's a kind of kapt limitation
如何解决(解决方法)
只需使用普通的 apt
或 annotationProcessor
即可运行 Dagger 编译器.一旦我改变了:
How to fix it (the workaround)
Just use plain apt
or annotationProcessor
for running Dagger compiler. As soon as I changed:
kapt libs.daggerCompiler
到
annotationProcessor libs.daggerCompiler
在我的模块级 build.gradle
文件中,我能够得到错误.修复错误后,您必须将该行恢复为 kapt
,否则将不会生成 dagger 类,因为它们是在 Kotlin 中定义的.
in my module level build.gradle
file, I was able to get the errors. After you've fixed the errors, you gotta revert the line back to kapt
because otherwise dagger classes wouldn't be generated since they're defined in Kotlin.
这篇关于错误:error.NonExistentClass Kotlin 在多模块 Dagger 项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!