0

In the doc there are three values for the aptMode.

Is there any detail information about these values ?

What is the meaning of "stubs" ?

Saeed Entezari
  • 3,685
  • 2
  • 19
  • 40
Liu Tom
  • 397
  • 2
  • 9

1 Answers1

2

See https://blog.jetbrains.com/kotlin/2015/06/better-annotation-processing-supporting-stubs-in-kapt/ (stubs are described in the second paragraph, but the first one provides context):

The initial version of kapt worked by intercepting communication between annotation processors (e.g. Dagger 2) and javac, and added already-compiled Kotlin classes on top of the Java classes that javac saw itself in the sources. The problem with this approach was that, since Kotlin classes had to be already compiled, there was no way for them to refer to any code generated by the processor (e.g. Dagger’s module classes). Thus we had to write Dagger application classes in Java.

As discussed in the previous blog post, the problem can be overcome by generating stubs of Kotlin classes before running javac and then running real compilation after javac has finished. Stubs contain only declarations and no bodies of methods. The Kotlin compiler used to create such stubs in memory anyways (they are used for Java interop, when Java code refers back to Kotlin), so all we had to do was serialize them to files on disk.

And also this answer.

But now stubs are generated by default, you can explicitly disable this generation by using aptMode=apt or only generate stubs by using aptMode=stubs. I think they are primarily for use internally by build systems (e.g. Gradle), as described in https://www.bountysource.com/issues/38443087-support-for-kapt-for-improved-kotlin-support:

There's 4 steps.

  1. kaptGenerateStubsKotlin: run kotlinc with plugin:org.jetbrains.kotlin.kapt3:aptMode=stubs
  2. kaptKotlin run kotlinc with plugin:org.jetbrains.kotlin.kapt3:aptMode=apt
  3. compileKotlin run kotlinc regularly
  4. compileJava run javac with -proc:none and pass the generated sources from step 2.

These steps are slightly different with each minor version of kotlin so this will be interesting.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487