ERROR : error.NonExistentClass Kotlin In multi module Dagger project
Asked Answered
R

23

85

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.

Reuben answered 14/7, 2017 at 9:41 Comment(2)
Can you post your project's and module's gradle files?Larimore
can you also post the output from ./gradlew build --info --stacktraceParaplegia
C
50

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.

Centralization answered 26/7, 2017 at 14:49 Comment(1)
You can also run ./gradlew :app:compileDebugJavaWithJavac --stacktrace to find out the source of the error.Eustache
C
103

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

Curie answered 12/4, 2019 at 7:51 Comment(5)
this helped me to find real issue. before adding this, the error was misleadingZygoma
this should be the accepted answer, as it takes you to the real issue.Durrace
thanks, you're a lifesaver. my problems also arised regarding the android studio forcing to use androidx when ViewBindingPeart
Shouldn't it be correctErrorTypes = true ?Cerargyrite
On a kt gradle file it was failing to build for me without the =Cerargyrite
C
50

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.

Centralization answered 26/7, 2017 at 14:49 Comment(1)
You can also run ./gradlew :app:compileDebugJavaWithJavac --stacktrace to find out the source of the error.Eustache
I
8

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"
Initiatory answered 27/8, 2018 at 11:18 Comment(1)
Had nearly the same - in my case I forgot to add kotlin-android plugin to module's build.gradle.Gallous
P
6

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.

Pollie answered 11/12, 2017 at 0:50 Comment(1)
Using annotationProcessor showed the problem to me. Thanks!Lessard
G
5

After removing the outdated library

implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha03'

I got this error:

incompatible types: NonExistentClass cannot be converted to Annotation 

Looking at https://dagger.dev/hilt/view-model.html I changed in a ViewModel:

class MainViewModel @ViewModelInject constructor(
    ...
) : ViewModel() {

to

@HiltViewModel
class MainViewModel @Inject constructor(
    ...
) : ViewModel() {
Gibeonite answered 24/5, 2021 at 15:23 Comment(2)
i am getting unresolved reference on @Inject , does i miss something ?Outrun
@Kuldeepmourya, maybe you should rebuild the project. Or check providers maybe.Gibeonite
E
4

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.

Eidetic answered 25/5, 2019 at 10:53 Comment(2)
This was actually my problem. Thanks!Spotless
Replacing all imports from org.jetbrains.annotations.Nullable to androidx.annotation.Nullable fixed the issue for meKinfolk
S
2

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

Stillness answered 7/10, 2017 at 9:17 Comment(0)
I
1

It seems, there is a bug with kapt, project cleaning should help.

./gradlew clean
Irish answered 14/7, 2017 at 12:51 Comment(3)
Plus Invalid Cache and Restart did it for me.Nyasaland
as stated on youtrack.jetbrains issue, we've to delete the ./gradle/caches: youtrack.jetbrains.com/issue/…Peart
This worked great for me when nothing else did!Klystron
L
1

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.

Lovesick answered 12/7, 2019 at 10:11 Comment(0)
H
1

I received this error, when there was a compilation error in my Injected class. Please ensure that there aren't any compilation errors.

Haldas answered 20/8, 2019 at 9:50 Comment(0)
P
1

For all those arriving like me on this topic with a similar error.
Check your annotation imports.
For me the problem was I had the @MicronautTest annotation same as another test just the wrong one. Somehow intellij seems to think the import is fine while it really is not.

I had
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
yet needed the kotest one.
import io.micronaut.test.extensions.kotest.annotation.MicronautTest

The kapt error is, while technically correct, quite uninformative. So just check the imports and if they are all correct.

Paleface answered 16/6, 2021 at 13:10 Comment(0)
H
1
ERROR : error.NonExistentClass

This means there is a problem with providing dependencies( and not the dependency itself!).

Sometimes annotation processor(in this case Dagger) is unable to build dependency graph on the first build iteration due to a provider absence.

For instance: You are passing GlideApp as a parameter in your provider while GlideApp class is not generated yet! So, watch out for your providers for NonExistentClass errors.

Haemostasis answered 13/10, 2021 at 7:42 Comment(0)
M
1

Its a compilation sync issue.

Do

./gradlew clean build

or

invalidate cache and restart

Melisandra answered 16/6, 2023 at 6:34 Comment(0)
M
1

If anyone still having issue. Check this link out

     androidComponents {
    onVariants(selector().all(), { variant ->
        afterEvaluate {
            // This is a workaround for https://issuetracker.google.com/301244513 which depends on internal
            // implementations of the android gradle plugin and the ksp gradle plugin which might change in the future
            // in an unpredictable way.
            project.tasks.getByName("ksp" + variant.name.capitalize() + "Kotlin") {
                def buildConfigTask = (com.android.build.gradle.tasks.GenerateBuildConfig) project.tasks.getByName("generate" + variant.name.capitalize() + "BuildConfig")

                ((org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompileTool) it).setSource(
                        buildConfigTask.sourceOutputDir
                )
            }
        }
    })
}

Add this at the end of app level build.gradle file.

Mayweed answered 22/11, 2023 at 10:54 Comment(0)
S
0

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.

Searchlight answered 20/12, 2018 at 9:20 Comment(0)
P
0

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
Piccadilly answered 3/5, 2019 at 16:47 Comment(0)
P
0

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
Palaeogene answered 4/5, 2019 at 10:38 Comment(5)
Where would I put that in a module, exactly?Favouritism
in module's gradle.properties filePalaeogene
Mmm, there's no gradle.properties file in my modules, only the top-level one... Any idea why that is?Favouritism
I am not sure why don't you have gradle.properties file but you can add it, it is just a test file with .properties extensionPalaeogene
*text file .propertiesPalaeogene
E
0

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 { ... }
Estriol answered 14/6, 2019 at 8:50 Comment(0)
K
0

I had a project with Dagger injecting something into Presenters At one moment I got a persistent "NonExistentClass.java:3: error: error.NonExistentClass must be INTERFACE" error

The root cause was trivial: a rogue copy of the valid @Inject annotated file with unsatisfied dependencies somehow slipped into the project. How can we find it? The project in Android Studio looks OK.

  1. Look at the error message, it looks like:

    /home/alex/AndroidProvects/TopProject/app/build/tmp/kapt3/stubs/onlineDebug/error/NonExistentClass.java:3: error: error.NonExistentClass must be INTERFACE public final class NonExistentClass {

  2. Search at the compiled by kapt build files in /home/alex/AndroidProvects/TopProject/app/build/tmp/kapt3/stubs/onlineDebug/app for "NonExistentClass" string

  3. You will find the exact file with the exact unsatisfied Dagger dependency (in my case it was a rogue orphan file in the place where it shouldn't exist)

Kiakiah answered 26/7, 2020 at 15:56 Comment(0)
S
0

I had similar issues with dagger. Adding the following helped solve it :

// dagger
implementation dep('com.google.dagger:dagger')
implementation dep('com.google.dagger:dagger-android-support')
implementation dep('com.spotify.dagger.android:dagger')
kapt dep('com.google.dagger:dagger-android-processor')
kapt dep('com.google.dagger:dagger-compiler')
Shoon answered 20/5, 2021 at 21:14 Comment(0)
N
0

The problem for me was forgetting to import the annotation.

Adding @EnableBatchProcessing without any import incurred that problem :)

Natter answered 6/12, 2023 at 9:20 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewBystreet
A
0

I encountered this error when I had a class with the same name on another module. This other class was not created using Dagger/Hilt.

Americanist answered 7/3, 2024 at 9:59 Comment(0)
S
0

here it started, after I removed these implementations

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Stallings answered 7/3, 2024 at 23:45 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.