Dagger2 - FragmentSubcomponentBuilder没有实现AndroidInjector.Factory

时间:2017-11-03 12:51:39

标签: android dependency-injection kotlin dagger-2 dagger

我使用dagger 2并试图将我的Android项目从Java翻译成Kotlin。成功构建后,我遇到了运行时错误:

MyFragmentSubcomponentBuilder未实现AndroidInjector.Factory

使用Java时没有出现此错误。而且我注意到在我的模块ActivityBuilder中,所有方法在构建后都未使用(它们必须在生成的子组件类中使用)。

@Module
abstract class ActivityBuilder {

    // ...

    @ContributesAndroidInjector(modules = arrayOf(MainActivityModule::class,MyFragmentProvider::class))
    internal abstract fun bindMainActivity(): MainActivity  // this method highlighted with gray color in AndroidStudio as unused 
}

如何设置项目以正确使用dagger 2和kotlin?

我的顶级gradle.build:

 buildscript {
        ext.kotlin_version = '1.1.51'
        repositories {
            jcenter()
            maven { url 'https://maven.google.com' }
            mavenCentral()
            google()
            maven {
                url 'https://github.com/uPhyca/stetho-realm/raw/master/maven-repo'
            }
            maven {
                url "https://jitpack.io"
            }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.0.0'
            classpath 'com.google.gms:google-services:3.1.0'
            classpath "io.realm:realm-gradle-plugin:3.0.0"
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }

    // ...

app build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion
    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0.0"
        multiDexEnabled true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    dataBinding {
        enabled = true
    }
    kapt {
        generateStubs = true
    }
}

// ...

kapt {
    generateStubs = true
}

dependencies {

    // ...

    kapt 'com.android.databinding:compiler:3.0.0'

    //Dagger 2 dependencies
    compile 'com.google.dagger:dagger:2.11'
    compile 'com.google.dagger:dagger-android:2.11'
    compile 'com.google.dagger:dagger-android-support:2.11'
    kapt 'com.google.dagger:dagger-compiler:2.11'
    kapt 'com.google.dagger:dagger-android-processor:2.11'

    // ...
}

正如你所看到的,我没有使用annotationProcessor作为dagger 2(只有kapt),而且我还添加了generateStubs = true。但是我仍然在运行时遇到错误。

1 个答案:

答案 0 :(得分:2)

AndroidSupportInjectionModule放入ApplicationComponent

中的数组模块中

例如

@Singleton
@Component(
        modules = arrayOf(
                AndroidSupportInjectionModule::class,
                ActivityBuilder::class,
                AppModule::class
        )
)
interface AppComponent : AndroidInjector<App> {

    @Component.Builder
    abstract class Builder : AndroidInjector.Builder<App>()

}