38

I have installed Kotlin plugin today into an existing project with Dagger 2. Before Kotlin was installed I had no issues with Dagger. However, now the compiler complains :

Error:(5, 32) Unresolved reference: DaggerAppComponent
Error:Execution failed for task ':app:compileDebugKotlinAfterJava'.
> Compilation error. See log for more details
Error:(12, 21) Unresolved reference: DaggerAppComponent

Project gradle:

ext.kotlin_version = '1.1.1'

    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"

Module gradle:

kapt {
    generateStubs = true
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    testCompile 'junit:junit:4.12'

    compile 'com.google.dagger:dagger:2.7'
    kapt 'com.google.dagger:dagger-compiler:2.7'

    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"


}

The DaggerAppComponent file IS auto generated, so I'm confused as to why there is an un resolved reference error thrown.

2
  • you solve you problem? what was wrong? Apr 27, 2017 at 18:22
  • 2
    Open Gradle Console then click in to Run with --stacktrace to show more log ( more details about the error)
    – Sally
    Jun 22, 2017 at 15:12

7 Answers 7

75
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"
0
20

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

1
  • 'kapt.generateStubs' is not used by the 'kotlin-kapt' plugin
    – LunaVulpo
    Jun 18, 2018 at 10:26
8

At first, It gives an error after rebuild:

Unresolved reference: DaggerAppComponent

You should try to import (Alt + Enter) that component.

1
  • 6
    Don't downvote this; after doing the other steps, this is what I was missing lol. Aug 24, 2019 at 0:31
1

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

1

I spent a lot of time to fix this problem. In my case applied incorrect imports:

import com.google.android.datatransport.runtime.dagger.Module
import com.google.android.datatransport.runtime.dagger.Provides

I needed:

import dagger.Module
import dagger.Provides

Pay attantion

0

for me it was Showing Unresolved reference "DaggerAppComponent". after spending so many time i figured,this usually happens as a result of Android Studio failed to generate DaggerAppComponent automatically.

So, we need to clean and rebuild the project , then import (ALT+Enter )

If it is still not working try File=>Invalidate Caches/restart.

may be this would help someone some day !

-3

Do not use private for inject dagger in Kotlin, use it like this for Kotlin

@Inject
     internal lateinit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.