1

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!

Ashana.Jackol
  • 3,064
  • 28
  • 22
  • I know it might sound stupid, but have you tied to clean the project, maybe clean the cache and restart the Studio, etc. Also, what do you mean by "Suggestion". You implement, you build, it fails, but now the DaggerAppComponent is generated, you use it, import it and build again. But sometimes the code generation is messed up so it is good after some changes and problems to clean the build. – Yavor Mitev Jun 29 '22 at 09:15

3 Answers3

3

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'

Actually the latest kotlin version (1.7) is not fully support to dagger2 at the moment (I'm using latest Dagger 2.38.1), So the workaround is add this to your app level dependencies,

kapt "org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.4.2"

And build the project

Ashana.Jackol
  • 3,064
  • 28
  • 22
0

@HiltAndroidApp add at start of class and use latest version ext.hilt_version = '2.42'

-1
plugins {
    id 'kotlin-kapt'
}

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