简体   繁体   中英

Kotlin - Dagger 2 - Component is not generated by Dagger 2 in Android

I'm stuck at this problem when I move from java to Kotlin. Dagger is not generating the DaggerComponent class. I've tried many ways to fix this. This is my Code :

app.gradle :

...

apply plugin: 'kotlin-kapt'

...

dependencies {
    ...

    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-compiler:$dagger_version"
    kapt "com.google.dagger:dagger-android-processor:$dagger_version"
    provided 'javax.annotation:jsr250-api:1.0'

   ...

    compileOnly 'org.glassfish:javax.annotation:10.0-b28'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}

apply plugin: 'com.google.gms.google-services'

this is my project.gradle :

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.1.51'
    ext.support_version = '27.1.1'
    ext.glide_version = '4.7.1'
    ext.glide_slider_version = '1.3.2'
    ext.dagger_version = '2.17'

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.0.1'

       ...

And finally this is my component interface code :

@Component(modules = arrayOf(AppModule::class, DatabaseModule::class))
interface DIComponent {
    fun inject(app:App)
}

I think that i did it correctly, I declare all the required code with the correct annotations. I've did clean and rebuild the project but it still doesn't work. i did the invalidate caches and restart etc. it's still doesn't work.

Did I make a mistake when declaring the component? Or is there something wrong with my Android Studio. I don't have any idea about this. It's really confusing me and made me stressful.

add @Singleton

@Singleton
@Component(modules = arrayOf(AppModule::class, DatabaseModule::class))
interface DIComponent {
    fun inject(app:App)
}

I just fixed my problem by adding @Singleton to my component and then add this to my app gradle :

compile 'com.android.databinding:compiler:3.1.4'

I dont know with one solved my problem. But i saw that in the message when the gradle is finished building there are some warning and one of the warning is that there is a problem with the kotlin compiler in data binding. So fixed that and i fixed my problem too. Thanks .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM