What is generateStubs
for Kotlin? Here is my configuration in build.gradle
.
I can not find it in public document here : http://kotlinlang.org/docs/reference/kapt.html
kapt {
generateStubs = true
}
If I'm using Java
and Kotlin(1.2)
in my project, it is still needed to add?
Using kapt with: generatestubs = true , in order to use libraries like dagger 2 or dbflow, makes accessing internal classes from unit tests impossible. Example project: Bitbucket (remove kapt option in app's gradle in order to run tests without errors)
The Kotlin Annotation Processing Tool (KAPT) works with Java's annotation processing infrastructure to make most Java language annotation processors work in Kotlin out of the box. To do this, KAPT compiles Kotlin code into Java stubs that retain information that Java annotation processors care about.
KSP makes creating lightweight compiler plugins easier KSP is designed to hide compiler changes, minimizing maintenance efforts for processors that use it. KSP is designed not to be tied to the JVM so that it can be adapted to other platforms more easily in the future. KSP is also designed to minimize build times.
Basically, they are metadata that you can attach in your code. Now, we introduce a new purpose. We can use annotations as instructions for the compiler. And we can create a consumer or a processor for the compiler to use for processing these annotations.
EDIT:
If I'm using Java and Kotlin(1.2) in my project, it is still needed to add
No, With the version 1.0.4 introduces a new experimental implementation of the annotation processing API. Now there is no need to configure this generateStubs
in build.gradle
.
Add the following to your build.gradle
if you want to enable it:
apply plugin: 'kotlin-kapt'
You will also have to remove the generateStubs
config from build.gradle
But as your question about the Details on generateStubs
I keep my old Post as it is.
Use :
Using kapt
with: generatestubs = true
, in order to use libraries like dagger 2 or dbflow,This will enable the compiler to generate stub classes required for interoperability between Java and Kotlin. Unless generateStubs = true
is enabled, "bootstrap
" (A custom annotation processor, which is passed to javac
, loads the annotation data and calls other annotation processors.) Java code is required to reference generated sources.pulled that from
Note : Generated codes are always in Java
not in Kotlin
.
When to Include:
Generating stubs requires relatively much work, because all declarations must be resolved, and sometimes knowing return types requires analysis of expression (bodies of functions or property initializers after the =
sign). So, using stubs in kapt
slows your build down somewhat. That’s why stubs are off by default, and to enable them you need to write the following in your build.gradle
file:
kapt {
generateStubs = true
}
How this works:
Stubs, compiler generated intermediate classes, allows "generated" sources to be referenced from Kotlin otherwise the compiler will not be able to reference the missing sources.
Generated source is created in "build/generated/source/kapt/main"
, as this is under "build
", normally excluded from IntelliJ's project sources, this source root will be marked in the build script itself.
sourceSets {
main.java.srcDirs += [file("$buildDir/generated/source/kapt/main")]
}
Example :
Dagger2-example with Kotlin (1.1.50) annotation processor support Gradle build
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