Android Kotlin : Error Unresolved reference: DaggerAppComponent
On my end, as of
Kotlin version 1.5.30
Dagger version 2.35.1
Dagger support version 2.28.3
There's an issue with dagger 2 (above versions) and kotlin 1.5.30. When you try to run the project using
implementation 'com.google.dagger:dagger:2.35.1'
implementation 'com.google.dagger:dagger-android:2.35.1'
implementation 'com.google.dagger:dagger-android-support:2.28.3'
kapt 'com.google.dagger:dagger-compiler:2.28.3'
kapt 'com.google.dagger:dagger-android-processor:2.28.3'
It will fail with error:
java.lang.reflect.InvocationTargetException
When i used Run with --stacktrace
i found out that the issue was:
IllegalStateException: Unsupported metadata version" in KaptWithoutKotlincTask with 1.5.0-M2
I found out that this is because Dagger uses the old version of kotlinx-metadata-jvm
library & doesn't support kotlin version 1.5 and above yet.
So as a fix, i added:
kapt 'org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.2.0'
I don't know if there's an official fix to this yet, but this might help someone.
N:B: i tried changing from kapt to annotationProcessor, and though the project compiled, i still couldn't generate DaggerAppComponent
I was getting the same error message, but my case was different than yours. The unresolved reference appeared only when generating signed APK. This is how I solved it:
app/build.gradle
kapt {
generateStubs = true
}
dependencies {
//...
implementation 'com.google.dagger:dagger:2.9'
kapt 'com.google.dagger:dagger-compiler:2.9'
}
I can now deploy my signed APK without errors
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
And in your dependencies:
implementation "com.google.dagger:dagger:2.x"
implementation "com.google.dagger:dagger-android:2.x"
implementation "com.google.dagger:dagger-android-support:2.x"
kapt "com.google.dagger:dagger-compiler:2.x"
kapt "com.google.dagger:dagger-android-processor:2.x"
At first, It gives an error after rebuild:
Unresolved reference: DaggerAppComponent
You should try to import (Alt + Enter) that component.