I\'m new with kotlin and Dagger. I have a little problem that I do not how to solve and I don\'t find a solution.
So this is what I have:
@Module
class A
You need to have the kapt processor in build.gradle:
kapt {
generateStubs = true
}
dependencies {
...
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.google.dagger:dagger:2.0.2'
kapt 'com.google.dagger:dagger-compiler:2.0.2'
...
}
This extension will generate the code for dagger.
Additionally, for newer gradle versions, you can also apply the plugin in your build.gradle:
apply plugin: 'kotlin-kapt'
dependencies {
...
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.google.dagger:dagger:2.0.2'
kapt 'com.google.dagger:dagger-compiler:2.0.2'
...
}
You can check this project for reference
If u have problem withe DaggerComponent
, You should add
apply plugin: 'kotlin-kapt'
kapt {
generateStubs = true
}
to build.gradle
it will generate kotlin
code for dagger 2
otherwise project will only build on Rebuild
This issue can be fixed with the bellow change which is different from original answer
Following will also work well to fix this issue
with plugins
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
and dependencies
implementation "com.google.dagger:dagger:$dagger_version"
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
kapt "com.google.dagger:dagger-android-processor:$dagger_version"
For reference check out this Gist
I don't know when this change happened, but on 1.1.2 of the Kotlin gradle plugin you replace this (in your module's build.gradle
):
kapt {
generateStubs = true
}
with this:
apply plugin: 'kotlin-kapt'
and then make sure to replace dependencies that use annotationProcessor
with kapt
.
For example, the old way would be to use:
annotationProcessor (
'some.library:one:1.0'
...
'some.library.n:n.0'
)
and now you use:
kapt (
'some.library:one:1.0'
...
'some.library.n:n.0'
)
generateStubs
does not work anymore. Feel free to make a build with the latest Kotlin and it would tell you in the Messages section of Android Studio that it is not necessary anymore. Here's an up-to-date list of dependencies for Dagger2 for Android and Kotlin
apply plugin: 'kotlin-kapt'
//...
// Other Gradle stuff
//...
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:1.1.4-3"
compile 'com.google.dagger:dagger-android:2.12'
kapt 'com.google.dagger:dagger-android-processor:2.12'
compileOnly 'com.google.dagger:dagger:2.12'
kapt 'com.google.dagger:dagger-compiler:2.12'
}