Home > Software engineering >  Cannot find DaggerAppComponent inside Application class
Cannot find DaggerAppComponent inside Application class

Time:06-29

I'm using kotlin latest version and trying to implement dagger2, but after rebuilding the project the suggestions would never give me "DaggerAppComponent" to build.

my dependencies

implementation 'com.google.dagger:dagger:2.38.1'
implementation 'com.google.dagger:dagger-android:2.38.1'
implementation 'com.google.dagger:dagger-android-support:2.35.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.38.1'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.38.1' 

And I already added

plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'

}

here is my AppComponent class,

    @Component (
    modules = [
        AndroidSupportInjectionModule::class,
    ])
interface AppComponent: AndroidInjector<BaseApplication>{
    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(application: Application): Builder
        fun build(): AppComponent

    }
}

And here is my Application class, already added to manifests as well,

class BaseApplication: DaggerApplication() {

    override fun applicationInjector(): AndroidInjector<out DaggerApplication>? {
        return null
    }
}

I already tried adding kapt instead of using annotationProcessor but won't make any difference. Can somebody help me, I'm almost spent an entire day on this error!

CodePudding user response:

plugins {
    id 'kotlin-kapt'
}

dependencies {
    implementation 'com.google.dagger:dagger:2.41'
    kapt 'com.google.dagger:dagger-compiler:2.41'
}

CodePudding user response:

Ok finally I found some solution for this, I'm posting this because I think this will help someone in any way. I'm using the latest kotlin version 1.7.0 and the latest android studio. First I added the latest dependency for dagger2 which is 2.38.1 but it keeps giving me this error Execution failed for task ':app:kaptDebugKotlin' So finally I downgrade my dagger dependency to 2.35.1 and added these lines to the gradle.properties

kapt.use.worker.api=false
kapt.incremental.apt=false

then close the android studio and delete the 'build' folder inside 'app' folder. Restart android studio and works like a charm.Hope this will help you guys whose having same wired problem I had. Finally this is how my dependencies looks like,

implementation 'com.google.dagger:dagger:2.35.1'
implementation 'com.google.dagger:dagger-android:2.35.1'
kapt 'com.google.dagger:dagger-compiler:2.35.1'
kapt 'com.google.dagger:dagger-android-processor:2.35.1'
  • Related