With 'kotlin-kapt' plugin, Android Studio doesnt provide specific errors about Dagger 2

13.3k views Asked by At

I use Kotlin with Dagger 2. The problem is, when I make mistake while implementing Dagger (e.g., miss @Inject for class constractor), IDE doesnt show specifically where mistake is. Insead compiler error is always the same:

Execution failed for task ':app:kaptDebugKotlin'. Internal compiler error. See log for more details

Class with a deliberate mistake (commented @Inject):

class LoginPresenter //@Inject
constructor(private val request: LoginRequest)

Project build.gradle file:

buildscript {
    ext.kotlin_version = '1.1.4-3'

    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Module build.gradle:

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

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.example.kravets_a.kotlinanddagger"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    ...

    compile 'com.google.dagger:dagger:2.10'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.10'
    kapt "com.google.dagger:dagger-compiler:2.10"

    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
repositories {
    mavenCentral()
}
2

There are 2 answers

0
Andrew On BEST ANSWER

Option 1

Use apply plugin: 'kotlin-kapt'. And when error occurs, you can see it in Android Studio => on the bottom right => at Gradle Console.

Thanks to David Medenjak comment.

Option 2

Substitute apply plugin: 'kotlin-kapt' with kapt { generateStubs = true }

Compilation fails with expected result:

[com.example.kravets_a.kotlinanddagger.logic.MainActivityComponent.inject(com.example.kotlinanddagger.MainActivity)] com.example.kotlinanddagger.logic.LoginPresenter cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.

Tutorials like this, this or that says that apply plugin: 'kotlin-kapt' can be used instead of kapt { generateStubs = true }. But in that case Android Studio does not provide specific compilation error.

1
Auras On

The solution is to add this to your module's build.gradle:

kapt {
    correctErrorTypes = true
}