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()
}
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'
withkapt { generateStubs = true }
Compilation fails with expected result:
Tutorials like this, this or that says that
apply plugin: 'kotlin-kapt'
can be used instead ofkapt { generateStubs = true }
. But in that case Android Studio does not provide specific compilation error.