kapt 的 aptMode 是做什么用的?

what is aptMode of kapt used for?

doc 中,aptMode 有三个值。

是否有关于这些值的任何详细信息?

"stubs"是什么意思?

参见 https://blog.jetbrains.com/kotlin/2015/06/better-annotation-processing-supporting-stubs-in-kapt/(存根在第二段中进行了描述,但第一段提供了上下文):

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.

还有

但现在默认生成存根,您可以使用 aptMode=apt 明确禁用此生成,仅 使用 aptMode=stubs 生成存根。我认为它们主要供构建系统内部使用(例如 Gradle),如 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.