Unresolved reference DaggerApplicationComponent

2020-02-08 23:21发布

I'm trying to create my app component, but Dagger does not generate my app component. here is MyApplication class

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)
  }
}

and here is my AppComponent class

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

here is screenshot enter image description here

this is my project on github

here is error log

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

11条回答
来,给爷笑一个
2楼-- · 2020-02-08 23:40

In my case missing kapt compiler dependency. Please make sure to have below dependency too.

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

app 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'
}
查看更多
Animai°情兽
3楼-- · 2020-02-08 23:41

This helped me to fix this issue

Add this in the top of the build.gradle

apply plugin: 'kotlin-kapt'

Inside android tag add

kapt {
    generateStubs = true
}

And then replace

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

to

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

Now Rebuild the project by

Build -> Rebuild project
查看更多
放荡不羁爱自由
4楼-- · 2020-02-08 23:45

Please try enabling stubs generation, this might be the reason why the class is not visible at this stage of the build process. In your build.gradle file, top level:

kapt {
    generateStubs = true
}
查看更多
\"骚年 ilove
5楼-- · 2020-02-08 23:45

I've already downloaded your Github project. Thanks for sharing!

The answer for your issue is pretty simple:

Build -> Rebuild project

Dagger dependencies files will be recreated and app after would launched with any problem.

I checked already this with Android Studio 2.1.2 version. It works

查看更多
SAY GOODBYE
6楼-- · 2020-02-08 23:49

Please try adding this to your build.gradle

android {
    dexOptions {
        incremental false
    }

EDIT: apparently a year later this can happen if you don't apply kotlin-kapt plugin. Also make sure you use kaptinstead of annotationProcessor.

查看更多
狗以群分
7楼-- · 2020-02-08 23:52

you should remove

kapt {
generateStubs = true}

and add to the top of application gradle file

apply plugin: 'kotlin-kapt'

then Dagger will take care of the rest :)

查看更多
登录 后发表回答