1

I've been facing this problem for 3 hours. I couldn't find any logical reason to what is happening. The problem is, Only when I use the generated Dagger classes, and after Gradle do the Build every reference to these classes became red and I get the error:

Error: Unresolved reference: DaggerAppComponent

But, If I comment out every line in the App class reference any of these generated Dagger classes. The build process completed successfully. I tried doing clean/ rebuild/ make project. but it doesn't work. Some information about my environment:
Build.gradle:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "xxxxx"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation "android.arch.lifecycle:extensions:1.1.0"
    implementation "android.arch.lifecycle:viewmodel:1.1.0"
    implementation "android.arch.lifecycle:livedata:1.1.0"
    implementation "android.arch.persistence.room:runtime:1.0.0"
    implementation "android.arch.lifecycle:runtime:1.1.0"

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:support-v4:26.1.0'


    kapt "android.arch.lifecycle:compiler:1.1.0"
    kapt "android.arch.persistence.room:compiler:1.0.0"

    kapt "com.google.dagger:dagger-compiler:2.8"
    compile "com.google.dagger:dagger:2.8"
    compile "com.google.dagger:dagger-android:2.8"

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

Kotlin version: ext.kotlin_version = '1.1.51'
Android Studio version: 3.0.1

dependencies section in Build.Gradle file for the project (not the app module):

dependencies {
    classpath 'com.android.tools.build:gradle:3.0.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

App class:

class App:Application() {
    private val component:AppComponent by lazy {
        DaggerAppComponent.builder()
                .build()
    }
    override fun onCreate() {
        super.onCreate()
        component.inject(this)
    }
}

If anyone can figure out what is the problem, it would be much appreciated.
P.S. I'm still learning Dagger2 and I'm new to Kotlin and Native development in general. I came from C# and Xamarin background.

Rickless
  • 1,377
  • 3
  • 17
  • 36

1 Answers1

3

You seem to be missing the application of the kapt Gradle plugin, as described in the kapt documentation. Basically, add this after your existing plugins:

apply plugin: 'kotlin-kapt'

(If this helps, this question should be marked as a duplicate of this one)

zsmb13
  • 85,752
  • 11
  • 221
  • 226
  • It worked, but why? I was having no issue with `Android Architecture component` and `Room`? Thanks a lot mate! – Rickless Mar 14 '18 at 17:31
  • You are right. The question is indeed duplicate. Should I delete it? or close it (by selecting your answer) and then report it as duplicate? – Rickless Mar 14 '18 at 17:33
  • 1
    Not sure why, sometimes annotation processors work without explicitly applying the plugin... That would probably deserve its own question. As for this one, I've closed it for now. – zsmb13 Mar 14 '18 at 17:35
  • 1
    Thanks again mate! – Rickless Mar 14 '18 at 17:35