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
I'm also maintaining the lib module.
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
}
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.
But my company wanted to generate the APK using stable version of Android Studio. I'm using Android Studio 2.3.3.
When compiling the Android Project, I encountered this error:
error: error.NonExistentClass
The error appears when
:app:kaptDebugKotlin
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.
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):
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
How to fix it (the workaround)
Just use plain apt
or annotationProcessor
for running Dagger compiler. As soon as I changed:
kapt libs.daggerCompiler
to
annotationProcessor libs.daggerCompiler
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.
I had a very similar situation with a NonExistentClass
error in a multi-module project using Dagger and turns out I forgot to add the kotlin library dependency. So, just adding it in the sub-module solved my problem:
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$rootProject.kotlinVersion"
tldr: Change kapt to annotationProcessor in build.gradle and you will see the real problem.
I got the same error, and it turned out that I just commented out a class that I was using in my AppComponent. Unfortunately the kapt tool didn't give me the proper error message. If you change the kapt to annotationProcessor at your library's compiler, and try to build, it won't succeed neither, but you will get a more detailed error message.
Just add this to build gradle file to avoid the issues related NonExistentClass
kapt {
correctErrorTypes true
}
https://kotlinlang.org/docs/reference/kapt.html#non-existent-type-correction
I've found if you're using
kapt {
generateStubs = true
}
changing to false will then present the actual error, you will probably have issues building the Dagger Graph once it's compilation issues have been corrected, but simply change back to true, and you should be good
In my case, I had @Nullable
annotation from support-annotations while I had removed it in order to migrate to AndroidX
.
When building, because the annotation was not imported correctly, it was detected as invalid.
What I did was to check the code and fix all imports.
I got this error when I had moved by mistake a test class into my main sourceset. Moving it back to the test sourceset got rid of the error.
It seems, there is a bug with kapt, project cleaning should help.
./gradlew clean
I had the same issue recently. As I sometimes commit through the Android Studio (3.4.c6) I use the "Optimize imports" option to remove unused imports. For some reason, it removed the import for the Parcelize annotation.
It appeared the errors after I upgraded the .gradle version.
Upgraded the version for mockito from 2.7.21 to 2.+ fixed the issue for me.
- androidTestCompile "org.mockito:mockito-android:2.7.21" // remove this
+ androidTestCompile "org.mockito:mockito-android:2.+" // add this
It seems kapt cannot find the class, or cannot determine which class to use. e.g.
import foo.* // class foo.Abc
import bar.* // annotation bar.Abc
@Abc
class Xyz { ... }
I received this error, when there was a compilation error in my Injected class. Please ensure that there aren't any compilation errors.
If you come across this problem after Android X migration and start loosing your mind here is one thing you can try.
My Case:
Our project had few modules in it, lets call one of them myModuleProject
.
After migration to Android X it was compiling and running fine if I run it from android studio, but when code moved to cloud and CI starts build, it was failing with ':myModuleProject:kaptDebugKotlin'
and with long list of errors such as
e: /home/circleci/code/myModuleProject/build/tmp/kapt3/stubs/debug/package_name_here/reporter/bindingadapter/SomeBindingAdapterKt.java:14: error: incompatible types: NonExistentClass cannot be converted to Annotation
@error.NonExistentClass()
After two days of nightmare I found that not only root project gradle.properties
but also module projects should include following!
android.databinding.enableV2=true
android.useAndroidX=true
android.enableJetifier=true
来源:https://stackoverflow.com/questions/45099688/error-error-nonexistentclass-kotlin-in-multi-module-dagger-project