5

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
  • 1
    You can find the error in the gradle log. In Android Studio, in the bottom right, click at Gradle Console. There it will show the errors. Commented Sep 11, 2017 at 15:46
  • @DavidMedenjak's answer is, apparently, the best there is for now. It appears that AndroidStudio doesn't handle kapt errors yet. Cross your fingers. Commented Sep 11, 2017 at 21:33

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

kapt {
    correctErrorTypes = true
}

1 Comment

i've tried this recently, as per the official recommendation on kapt, but it's still failing. i.e. NonExistentClass is still being added

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.