20

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?

kimkevin
  • 2,202
  • 1
  • 26
  • 48

1 Answers1

27

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

Community
  • 1
  • 1
Dipali Shah
  • 3,742
  • 32
  • 47
  • `bootstrap Java code is required to reference generated sources` Can you please tell what this means? What is "bootstrap" – azizbekian Dec 06 '17 at 05:59
  • I have a question. Why we can not find it on public Kotlin website? I didn't set `generatestubs = true` this in my build.gradle. But I'm using dagger2 well. – kimkevin Dec 06 '17 at 09:11
  • 1
    @kimkevin,A public kotlin website is having overview of new feature (not atleast in depth as other blogs does)a link to _when to include_ is having more detail on annotation processing with kotlin. **And yes you can use dagge2 in your project with default stub but that forces to write Dagger Application class in Java.** – Dipali Shah Dec 06 '17 at 09:20
  • 1
    @kimkevin , from this _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 source_ I've tried to show the initial version limitations. – Dipali Shah Dec 06 '17 at 09:23
  • @Dipalis. I got it. I was curious of the posts which have `generateStubs = true` but I can not find any information in public Kotlin website. If I'm setting `generateStubs = true` in my project, do you know where generated stubs are? – kimkevin Dec 06 '17 at 09:31
  • I thought stubs are classes which have only method definition such as Interface. – kimkevin Dec 06 '17 at 09:49
  • 1
    @kimkevin, Hope my answer clears that. `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.` – Dipali Shah Dec 06 '17 at 09:54
  • @Dipalis. I see. It's the last question! the latest version of Kotlin always created stubs? I checked classes from `generated/source/kapt` folder with `generateStubs = true` and without it. but generated classes are the same. – kimkevin Dec 06 '17 at 09:59
  • 1
    You are referencing a two-year-old blog post. This behaviour has changed since then, making your answer very misleading. Also, don't forget to reference all sources. For instance, you used definitions from [here](https://medium.com/@workingkills/pushing-the-limits-of-kotlin-annotation-processing-8611027b6711) without giving any credit to the author. – Be_Negative Dec 07 '17 at 22:24
  • 1
    Stubs are generated automatically at a presence of an annotation processor(kapt dependency in your build script) since kapt3. You don't need to include `generateStubs = true` as part of your configuration given that `kotlin-kapt` plugin is used since kotlin 1.1 [This answers](https://stackoverflow.com/a/41220138/4879701) gives you a perspective on a difference in behavior.... I had this written out as my answer - I recommend adding this information to your answer, as it will help avoid confusion when people are reading this. Does not have to be verbatim. – Be_Negative Dec 07 '17 at 23:16
  • 1
    @Be_Negative, Updated answer to clarify the confusion. – Dipali Shah Dec 09 '17 at 05:02
  • 1
    @Dipalis. Thank you. "until and unless you run into problem with the default KAPT annotation processing implementation." this is not true, that is why I removed this from your answer. Overall, this is a good summary, well done. – Be_Negative Dec 10 '17 at 19:19