I had a usecase so I have added retrofit library to use it locally in libs folder
You can see the package structure and error in the image below when trying to import Timber android libary.
In the app/build.gradle file, I have added dependency:
implementation 'com.jakewharton.timber:timber:4.7.1'
In the libs/retrofit/build.gradle file, there also I added dependency:
// I also tried using api instead of implementation
implementation 'com.jakewharton.timber:timber:4.7.1'
Now, I synced, did invalidate cache and restart, clean, rebuild project.
Then tried to use Timber.d("some log") call in one of the files in libs/retrofit directory.
It resulted in error:
Add library: 'Gradle com.jakewharton.timber:timber:4.7.1#aar' to Classpath
Note, the Timber.d("other log") works fine in one of the files in app directory, example app/MainActivity.kt
What I am doing wrong?
Also, to note Retrofit code works perfectly fine in app module. The issue is using a Timber logging library is not working in any of the modules in libs
In my opinion, the answer lies in the question asked in this thread itself.
What happened, is that when I went through this situation myself, I saw this thread in the Google Search results.
After adding the dependency, for example:-
implementation 'com.jakewharton.timber:timber:4.7.1'
and thereafter syncing, I saw that it was merely a page refresh issue.
That is I switched to another source file which was opened in another tab and came back to this in which the following code was issued (the place of the aforesaid error):-
Timber.tag(...).e(...)
Timber usage.
Timber is packaged as an AAR file, that means it can only be used in android modules of your project.
The solution would be to convert your kotlin module to an android module by applying the plugins below in your build.gradle file of that module
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
I tried to replace Koin with Hilt (bad idea) for DI and now I'm stuck with this error:
Hilt_App.java:21: error: cannot find symbol
return DaggerApp_HiltComponents_ApplicationC.builder()
^
symbol: variable DaggerApp_HiltComponents_ApplicationC
What is it? How to fix it?
I got the same error message.
My problem was that I had an old/deprecated gradle dependency.
Make sure to remove following dpenedency from your gradle file:
/* DELETE this dependency */
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
If you are working on a modular project, take care of dependencies!
For example, if you have a retrofit dependency in your data module, even your data module implemented in the app module, you must add retrofit dependency as well or make them transactive with the api to be accessible to the app module.
For those who had the same error but did not miss any dependencies. I had this problem, because I forgot to annotate one class with "#Inject constructor". After I did that, everything worked again.
as answered in the guestion comments. The problem was that when using Jetpack integrations as explained here for the ViewModels https://developer.android.com/training/dependency-injection/hilt-jetpack and you have to add those dependencies also in the main app module (not only in the modules where you actually use ViewModels).
for example , if you have the following in the feature module's build.gradle file:
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hilt_lifecycle"
kapt "androidx.hilt:hilt-compiler:1.0.0-alpha02"
make sure you add them to the app's build.gradle file as well
I've met the issue and work with these dependencies:
implementation "com.google.dagger:hilt-android:$hiltVersion"
kapt "com.google.dagger:hilt-android-compiler:$hiltVersion"
kapt "androidx.hilt:hilt-compiler:$hiltAndroidXVersion"
https://medium.com/gradeup/dependency-injection-dagger-hilt-vs-koin-ab2f7f85e6c6
Error while adding MoshiPack Library in Kotlin latest version 1.3.70 to gradle.build application
Moshi pack
implementation 'com.daveanthonythomas.moshipack:moshipack:1.0.1'
Error Message
Duplicate class kotlin.reflect.KClasses found in modules jetified-kotlin-reflect-1.1.1.jar (org.jetbrains.kotlin:kotlin-reflect:1.1.1) and jetified-kotlin-stdlib-1.3.70.jar (org.jetbrains.kotlin:kotlin-stdlib:1.3.70)
Any suggestions how to solve this issue or any other library I can use in Kotlin so I can use Message Pack.
Thanks in advance
Try to add this to your dependencies:
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
and make sure you specified your Android NDK location under File>Project Structure...>SDK Location
Starting Kotlin 1.3.70 some basic useful members on KClass included in Kotlin standard library (they were in a kotlin-reflect before).
See "Working with KClass" in https://blog.jetbrains.com/kotlin/2020/03/kotlin-1-3-70-released/
In your case MoshiPack adds a kotlin-reflect library that conflicts with standard library.
You should exclude transitive dependency to resolve the conflict.
If you use KotlinDSL, in build.gradle.kts:
implementation ("com.daveanthonythomas.moshipack:moshipack:1.0.1") {
exclude(group = "org.jetbrains.kotlin", module = "kotlin-reflect")
}
If you use Groovy, in build.gradle:
implementation ('com.daveanthonythomas.moshipack:moshipack:1.0.1') {
exclude group: 'org.jetbrains.kotlin', module: 'kotlin-reflect'
}
I tried this and it worked
implementation "org.jetbrains.kotlin:kotlin-reflect:1.4.10"
i think the only way to solve it , to go back to kotlin version 1.3.61 , so remove 1.3.70 and use 1.3.61
So I finally figured it out and here's how :-
So the main problem is you have injected 2 dependency of same class. Here he used 2 dependency for Kotlin which conflict in runtime to fix that you have to check which dependency is duplicated. (It is most case scenario. It can be with any other dependency i.e. Hilt)
Go to File > Project structure > Dependencies
Check which dependency are repeating. In this case it will have (androidx.core:core:1.8.0) and (androidx.core:core:+)
as you can see there are 2 dependency with same classes version 1.8.0 will have all the class which core:+ will have and this is causing a error.
Now delete (androidx.core:core:+) and hit Apply and sync project.
Now you should be good to go. here is structure after changing dependency.
Note:- This method will show all the android dependency which you might be not included but you will see all the dependency which any app has. Please remove the dependency who are you familiar with do not remove any dependency without any proper knowledge.
I have gone through various stackoverflow link some of the link are :
LInk 1
Link 2
but no one solve this below are the gradle file and my application :
// dependency injection
implementation 'com.google.dagger:dagger-android:2.14.1'
implementation 'com.google.dagger:dagger-android-support:2.14.1'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.14.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1'
And the MyApplication file is :
DaggerAppComponent.builder()
.application(this)
.build()
.inject(this);
AppLogger.init();
You have to build the project after setting up dagger and then start typing Dagger and Android Studio will show the class it built for dagger component
There is many methods to solve it,
Below methods are worldwide problem solver
Click on Invalidate/cache restart in android studio
Close android studio, Go > androidStudio installed folder and delete cache folder manualy and start again.
Rename dagger component and again clean/rebuild your project.
These 3 methods can solve your problem.
When I applied the plugin 'kotlin-kapt' in the gradle file, it caused the DaggerAppComponent to not be found. Disabled it and the DaggerAppComponent was found again and I am using Dagger 2.28.1.
So the issue could be linked to specific changes with the plugins.
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.
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
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.
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.
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() {
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
It seems, there is a bug with kapt, project cleaning should help.
./gradlew clean
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.
I received this error, when there was a compilation error in my Injected class. Please ensure that there aren't any compilation errors.
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.
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.
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
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
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 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.
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 {
Search at the compiled by kapt build files in
/home/alex/AndroidProvects/TopProject/app/build/tmp/kapt3/stubs/onlineDebug/app for "NonExistentClass" string
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)
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')