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'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With