Search code examples
javadependency-injectionkotlindagger-2

Kotlin + Dagger 2: Dagger* files won't generate


I started using Kotlin and Dagger 2 together for the first time. I assumed everything is the same as in Java, but apparently, not quite. Dagger won't generate Dagger*files for me. Here is my code:

Components:

@PerActivity
@Subcomponent(modules = arrayOf(ApplicationModule::class))
interface ActivityComponent {
    fun inject(app: OneAccountApplication)
}

@Singleton
@Component(modules = arrayOf(ApplicationModule::class))
interface ApplicationComponent {

fun inject(syncService: SyncService)
@ApplicationContext fun context(): Context
fun application(): Application
fun ribotsService(): OneAccountService
fun preferencesHelper(): PreferencesHelper
fun databaseHelper(): DatabaseHelper
fun dataManager(): DataManager
}

@ConfigPersistent
@Component(dependencies = arrayOf(ApplicationComponent::class))
interface ConfigPersistentComponent {
    fun activityComponent(activityModule: ActivityModule): ActivityComponent
}

Modules:

@Module
class ActivityModule(private val mActivity: Activity) {

    @Provides
    internal fun provideActivity() = mActivity

    @Provides
    @ActivityContext
    internal fun providesContext() = mActivity
}

@Module
class ApplicationModule(val mApplication: Application) {

    @Provides @Singleton
    internal fun provideApplication() = mApplication

    @Provides
    @ApplicationContext
    internal fun provideContext() = mApplication

    @Provides
    @Singleton
    internal fun provideOneAccountService() = OneAccountService.Creator.newOneAccountService()
}

Scope Annotations:

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityContext

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ApplicationContext

@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class ConfigPersistent

@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class PerActivity

This is basically my whole DI system which I was using in my java code successfully. But with Kotlin, for some reason it does not work. I have also added: apply plugin: 'kotlin-kapt' to gradle.build like:

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

And in dependencies I have:

dependencies {

    final DAGGER_VERSION = '2.8'
    def daggerCompiler = "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
    annotationProcessor daggerCompiler
    testAnnotationProcessor daggerCompiler
    androidTestAnnotationProcessor daggerCompiler
    compile  "com.google.dagger:dagger:$DAGGER_VERSION"
}
kapt {
    generateStubs = true
}

Basically, this it https://github.com/ribot/android-boilerplate transformed into Kotlin.


Solution

  • Kotlin works with kapt instead of the annotationProcessor from the Android plugin.

    You need to include and use the following in your build.gradle file:

    kapt {
        generateStubs = true
    }
    dependencies {
        // ...
        kapt daggerCompiler
    }
    

    More detailed instructions can also be found here: Dagger and Kotlin