bicicleteroNerd
bicicleteroNerd

Reputation: 379

Android Library, Kotlin and Dagger2

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
        }
    }
}

Upvotes: 3

Views: 1224

Answers (2)

iscariot
iscariot

Reputation: 434

Java module should have

plugins {
    id("java-library")
    id("kotlin")
    id("kotlin-kapt")
}

java {
    sourceSets {
        main.java.srcDirs += ['build/generated/source/kapt/main']
    }
}

Upvotes: 1

bicicleteroNerd
bicicleteroNerd

Reputation: 379

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.

Upvotes: 4

Related Questions