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

时间:2018-08-14 20:18:09

标签: android kotlin dagger

当我从Java转移到Kotlin时,我陷入了这个问题。 Dagger不生成DaggerComponent类。我尝试了多种方法来解决此问题。这是我的代码:

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'

这是我的 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'

       ...

最后这是我的组件接口代码:

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

我认为我做得正确,我用正确的注释声明了所有必需的代码。我已经清理并重建了项目,但仍然无法正常工作。我做了失效缓存并重新启动等。它仍然不起作用。

在声明组件时我是否犯了一个错误?或者我的Android Studio有问题。我对此一无所知。这真的让我感到困惑,并使我感到压力。

2 个答案:

答案 0 :(得分:2)

添加@Singleton

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

答案 1 :(得分:0)

我只是通过将@Singleton添加到我的组件中,然后将其添加到我的应用程序gradle中来解决我的问题:

scp -i D:\Work\KeyPairs\Tech-O-Dex-API D:\Jenkins\workspace\Tech-O-Dex\target\Tech-O-Dex-0.1.0.jar ec2-user@ec2-<IP>.compute-1.amazonaws.com:~/

我不知道一个人能解决我的问题。但是我看到在gradle完成构建的消息中有一些警告,警告之一是kotlin编译器在数据绑定中存在问题。所以解决了这个问题,我也解决了我的问题。谢谢。