Robert
Robert

Reputation: 1266

Unresolved reference to DaggerApplicationComponent

I can't handle error related to Dagger and it's generation of component.

import android.app.Application
import dagger.Component

@Component(modules = arrayOf(ApplicationModule::class))
interface ApplicationComponent{
    fun inject(app: Application)
}

The place where component is called looks like that

class MyAwesomeApplication : Application(){
    val component: ApplicationComponent by lazy {
        DaggerApplicationComponent.builder().appModule(ApplicationModule(this)).build()
    }

    override fun onCreate() {
        super.onCreate()
        component.inject(this)
    }
}

In gradle build file I have set up:

implementation "com.google.dagger:dagger:$dagger_version"
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"
kapt "com.google.dagger:dagger-android-processor:$dagger_version"

Also the stub generation is turned on and kotlin-kapt has been applied

Any ideas what is wrong there?

Upvotes: 1

Views: 3118

Answers (4)

Sriraksha
Sriraksha

Reputation: 559

For me its because of dagger version. By using the below version resolved the issue for me.

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

dagger_version = '2.17'
    implementation "com.google.dagger:dagger:$dagger_version"
    kapt "com.google.dagger:dagger-compiler:$dagger_version"

Upvotes: 0

barmacki
barmacki

Reputation: 41

For me it was a gradle dependency issue. Solved it by adding the following to the app.gradle script:

apply plugin: 'kotlin-kapt'
.
.
dependencies {
    .
    .
    kapt "com.google.dagger:dagger-compiler:$dagger2_version"
    kapt "com.google.dagger:dagger-android-processor:$dagger2_version"
}

Then I cleaned and rebuilt the project.

Upvotes: 0

Xi Wei
Xi Wei

Reputation: 1679

Your build.gradle should look something like this

apply plugin: 'kotlin-kapt'
…
dependencies {
…
    final dagger_version = '2.17'
    implementation "com.google.dagger:dagger:$dagger_version"
    kapt "com.google.dagger:dagger-compiler:$dagger_version"
    compileOnly 'javax.annotation:javax.annotation-api:1.3.2'
}

I think what you are missing is compileOnly 'javax.annotation:javax.annotation-api:1.3.2'. I have an article for detailed steps to setup Dagger.

Upvotes: 1

Jithish P N
Jithish P N

Reputation: 2160

app.gradle

apply plugin: 'kotlin-kapt'

android {
kapt {
  generateStubs = true
}
}

// Dagger 2
implementation "com.google.dagger:dagger:$dagger2_version"
kapt "com.google.dagger:dagger-compiler:$dagger2_version"

Build -> Rebuild project

Upvotes: 0

Related Questions