【问题标题】:Error: Unresolved reference: DaggerAppComponent [duplicate]错误:未解决的参考:DaggerAppComponent [重复]
【发布时间】:2018-03-14 17:20:02
【问题描述】:

我已经面临这个问题 3 个小时了。我找不到任何合乎逻辑的理由来解释正在发生的事情。问题是,只有当我使用生成的 Dagger 类,并且在 Gradle 执行 Build 之后,对这些类的每个引用都变红了,我得到了错误:

错误:未解析的引用:DaggerAppComponent

但是,如果我注释掉 App 类中的每一行,请引用这些生成的 Dagger 类中的任何一个。构建过程成功完成。我试着做干净/重建/制作项目。但它不起作用。 关于我的环境的一些信息:
Build.gradle:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "xxxxx"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation "android.arch.lifecycle:extensions:1.1.0"
    implementation "android.arch.lifecycle:viewmodel:1.1.0"
    implementation "android.arch.lifecycle:livedata:1.1.0"
    implementation "android.arch.persistence.room:runtime:1.0.0"
    implementation "android.arch.lifecycle:runtime:1.1.0"

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:support-v4:26.1.0'


    kapt "android.arch.lifecycle:compiler:1.1.0"
    kapt "android.arch.persistence.room:compiler:1.0.0"

    kapt "com.google.dagger:dagger-compiler:2.8"
    compile "com.google.dagger:dagger:2.8"
    compile "com.google.dagger:dagger-android:2.8"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

Kotlin 版本:ext.kotlin_version = '1.1.51'
Android Studio 版本:3.0.1

Build.Gradle 文件中项目(不​​是应用模块)的依赖项部分:

dependencies {
    classpath 'com.android.tools.build:gradle:3.0.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

App类:

class App:Application() {
    private val component:AppComponent by lazy {
        DaggerAppComponent.builder()
                .build()
    }
    override fun onCreate() {
        super.onCreate()
        component.inject(this)
    }
}

如果有人能找出问题所在,将不胜感激。
附言我仍在学习Dagger2,而且我对KotlinNative 的开发总体上是新手。我来自C#Xamarin 背景。

【问题讨论】:

    标签: android kotlin dagger-2


    【解决方案1】:

    您似乎缺少 kapt Gradle 插件的应用程序,如 kapt documentation 中所述。基本上,在你现有的插件之后添加这个:

    apply plugin: 'kotlin-kapt'
    

    (如果有帮助,此问题应标记为与this one 重复)

    【讨论】:

    • 成功了,但是为什么呢?我对Android Architecture componentRoom 没有意见?非常感谢朋友!
    • 你是对的。这个问题确实是重复的。我应该删除它吗?或关闭它(通过选择您的答案),然后将其报告为重复?
    • 不知道为什么,有时注释处理器在没有明确应用插件的情况下工作......这可能值得自己提出问题。至于这个,我暂时关闭了。
    • 再次感谢伙计!
    猜你喜欢
    • 2017-08-08
    • 2021-01-29
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多