Android Kotlin : Error Unresolved reference: DaggerAppComponent

空扰寡人 提交于 2019-12-20 17:28:33

问题


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.


回答1:


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"



回答2:


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




回答3:


At first, It gives an error after rebuild:

Unresolved reference: DaggerAppComponent

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




回答4:


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

@Inject
     internal lateinit


来源:https://stackoverflow.com/questions/42843996/android-kotlin-error-unresolved-reference-daggerappcomponent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!