简体   繁体   English

Kotlin - Dagger 2 - 组件不是由 Android 中的 Dagger 2 生成的

[英]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.当我从 Java 迁移到 Kotlin 时,我被这个问题困住了。 Dagger is not generating the DaggerComponent class. Dagger 不生成 DaggerComponent 类。 I've tried many ways to fix this.我已经尝试了很多方法来解决这个问题。 This is my Code :这是我的代码:

app.gradle : 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 :这是我的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.或者我的 Android Studio 有什么问题。 I don't have any idea about this.我对此没有任何想法。 It's really confusing me and made me stressful.这真的让我感到困惑,让我感到压力。

add @Singleton添加@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 :我只是通过将 @Singleton 添加到我的组件中来解决我的问题,然后将其添加到我的应用程序 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.但是我在 gradle 完成构建时的消息中看到有一些警告,其中一个警告是 kotlin 编译器在数据绑定中存在问题。 So fixed that and i fixed my problem too.所以解决了这个问题,我也解决了我的问题。 Thanks .谢谢 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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