未解决的引用 DaggerApplicationComponent

49

我正在尝试创建我的应用程序组件,但是Dagger没有生成我的应用程序组件。这是MyApplication类。

class MyApplication : Application() {

companion object {
    @JvmStatic lateinit var graph: ApplicationComponent
}
@Inject
lateinit var locationManager : LocationManager

override fun onCreate() {
    super.onCreate()
    graph = DaggerApplicationComponent.builder().appModule(AppModule(this)).build()
    graph.inject(this)
  }
}

这是我的AppComponent

@Singleton
@Component(modules = arrayOf(AppModule::class))
interface ApplicationComponent {
    fun inject(application: MyApplication)
}

这里有一个截图 在此输入图片描述

这是我在 github 上的项目

这是错误日志

Error:(7, 48) Unresolved reference: DaggerApplicationComponent
Error:(28, 17) Unresolved reference: DaggerApplicationComponent
Error:Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details
Information:BUILD FAILED
Information:Total time: 21.184 secs
Error:e: .../MyApplication.kt: (7, 48): Unresolved reference: DaggerApplicationComponent
e: Unresolved reference: DaggerApplicationComponent
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

Information:4 errors
Information:0 warnings
Information:See complete output in console

当您尝试构建应用程序时,它会显示什么信息? - AndroidEx
我已经更新了我的问题。 - user5543258
@user5543258,你解决了你的问题吗?我也有同样的问题。 - AlexKost
2
这里什么都不起作用,你找到解决方案了吗? - LMaker
16个回答

52

我的解决方案是添加

apply plugin: 'kotlin-kapt'

并且移除

kapt {
    generateStubs = true
}

2
这拯救了我的生命。 - Jelil Adesina
我被卡了一整天,可惜没有搜索。这真的帮了我很多。 - Ankit Dubey

49

这帮助我解决了这个问题

将此添加到build.gradle的顶部

apply plugin: 'kotlin-kapt'

android 标签内添加

kapt {
    generateStubs = true
}

然后替换

annotationProcessor 'com.google.dagger:dagger-compiler:2.11'

kapt 'com.google.dagger:dagger-compiler:2.11'

现在通过 Rebuild 重新构建项目。

Build -> Rebuild project

1
我认为你应该删除 generateStubs = true - newbie

29
请尝试启用存根生成,这可能是构建过程此阶段该类不可见的原因。在您的build.gradle文件中,顶层:
kapt {
    generateStubs = true
}

1
Java 的版本怎么样? - Anonymous

7

我已经下载了你的Github项目,感谢你的分享!

针对你的问题,答案非常简单:

Build -> Rebuild project

Dagger 依赖文件将会被重新创建,之后启动应用程序不会有任何问题。

我已经使用 Android Studio 2.1.2 版本进行了检查,它可以正常工作。


5
你应该删除

kapt {
generateStubs = true}

并将其添加到应用gradle文件的顶部

apply plugin: 'kotlin-kapt'

然后Dagger会处理剩下的部分 :)

4

在build.gradle文件的顶部添加以下内容:

apply plugin: 'kotlin-kapt'

将以下内容添加到依赖中:
kapt 'com.google.dagger:dagger-compiler:2.15'
kapt 'com.google.dagger:dagger-android-processor:2.15'

接下来重新构建你的项目。我希望它能够正常工作。 谢谢。


3
在我的情况下,缺少了kapt编译器的依赖。请确保还有以下依赖关系。
kapt 'com.google.dagger:dagger-compiler:2.15'

应用程序 build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-kapt'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.usb.utility"
        minSdkVersion 14
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.dagger:dagger-android:2.15'
    compile 'com.google.dagger:dagger-android-support:2.15' // if you use the support libraries
    kapt 'com.google.dagger:dagger-android-processor:2.15'
    kapt 'com.google.dagger:dagger-compiler:2.15'
    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'
}

2

对于Java用户来说,解决方案很简单,只需要重新构建你的项目。


这是完美的解决方案。 - Mubashar Javed

2
答案很简单:
构建 -> 重新构建项目
这对我有效。

2
在新版的hilt中,你需要使用SingletonComponent::class代替ApplicationComponent::class。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接