Android Library, Kotlin and Dagger2
android, android-library, dagger-2, kapt, kotlin
Solution
The problem was that Gradle was not finding the Dagger generated files by `kapt`, so I solved the problem by adding `src/main/kapt` to my sourceSets configuration on my Core module (lib):
build.gradle (Core module)
android {
...
sourceSets {
main.java.srcDirs += ['src/main/kotlin', 'src/main/kapt']
}
}
After that, the Core module started finding their Dagger 2 generated files.
Problem
I'm building an application that have two modules: the Core module, that is an Android Library (com.android.library) and the Application module (com.android.application). After I converted the Java files to Kotlin, the project is not compiling, giving me an error that the generated Dagger 2 files were not found (unresolved reference). But those files currently being generated under: ...core\build\generated\source\kapt\release{my\core\namespace}\DaggerBaseComponent.java What I'm missing? build.gradle (Core module) ``` apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-android-extensions' ... android { ... sourceSets { main.java.srcDirs += 'src/main/kotlin' } } dependencies { ... // Dagger. kapt "com.google.dagger:dagger-compiler:2.10" compile 'com.google.dagger:dagger:2.10' provided 'javax.annotation:jsr250-api:1.0' // Kotlin compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" } ``` build.gradle (Application module) ``` apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-android-extensions' ... android { sourceSets { main.java.srcDirs += 'src/main/kotlin' } } dependencies { ... // Dagger. kapt "com.google.dagger:dagger-compiler:2.10" compile 'com.google.dagger:dagger:2.10' provided 'javax.annotation:jsr250-api:1.0' // Kotlin compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" } ``` build.gradle (Project) ``` buildscript { ext.kotlin_version = '1.1.2-3' ... dependencies { classpath 'com.android.tools.build:gradle:2.3.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } ``` ApplicationContext.kt (of my Core Module) ``` class ApplicationContext : Application() { var baseComponent: BaseComponent? = null private set override fun onCreate() { super.onCreate() initializeInjector() } private fun initializeInjector() { // DaggerBaseComponent is and unresolved reference baseComponent = DaggerBaseComponent.builder() .appModule(AppModule(this)) .endpointModule(EndpointModule()) .build() } companion object { operator fun get(context: Context): ApplicationContext { return context.applicationContext as ApplicationContext } } } ```