Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is generateStubs configuration in Kotlin?

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?

like image 723
kimkevin Avatar asked Dec 01 '17 05:12

kimkevin


People also ask

What is kapt Generatestubs?

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)

What is the use of Kotlin kapt?

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.

What is KSP Android?

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.

What is Kotlin annotation processor?

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.


1 Answers

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

like image 152
Dipali Shah Avatar answered Nov 03 '22 23:11

Dipali Shah