kapt 编译器插件

kapt is in maintenance mode. We are keeping it up-to-date with recent Kotlin and Java releases but have no plans to implement new features. Please use the Kotlin Symbol Processing API (KSP) for annotation processing. See the list of libraries supported by KSP.

kapt 编译器插件 - 图1

Annotation processors (see JSR 269) are supported in Kotlin with the kapt compiler plugin.

In a nutshell, you can use libraries such as Dagger or Data Binding in your Kotlin projects.

Please read below about how to apply the kapt plugin to your Gradle/Maven build.

Using in Gradle

Follow these steps:

  1. Apply the kotlin-kapt Gradle plugin:

【Kotlin】

  1. plugins {
  2. kotlin("kapt") version "1.9.10"
  3. }

【Groovy】

  1. plugins {
  2. id "org.jetbrains.kotlin.kapt" version "1.9.10"
  3. }
  1. Add the respective dependencies using the kapt configuration in your dependencies block:

【Kotlin】

  1. dependencies {
  2. kapt("groupId:artifactId:version")
  3. }

【Groovy】

  1. dependencies {
  2. kapt 'groupId:artifactId:version'
  3. }
  1. If you previously used the Android support for annotation processors, replace usages of the annotationProcessor configuration with kapt. If your project contains Java classes, kapt will also take care of them.

    If you use annotation processors for your androidTest or test sources, the respective kapt configurations are named kaptAndroidTest and kaptTest. Note that kaptAndroidTest and kaptTest extends kapt, so you can just provide the kapt dependency and it will be available both for production sources and tests.

Annotation processor arguments

Use arguments {} block to pass arguments to annotation processors:

  1. kapt {
  2. arguments {
  3. arg("key", "value")
  4. }
  5. }

Gradle 构建缓存支持

The kapt annotation processing tasks are cached in Gradle by default. However, annotation processors run arbitrary code that may not necessarily transform the task inputs into the outputs, might access and modify the files that are not tracked by Gradle etc. If the annotation processors used in the build cannot be properly cached, it is possible to disable caching for kapt entirely by adding the following lines to the build script, in order to avoid false-positive cache hits for the kapt tasks:

  1. kapt {
  2. useBuildCache = false
  3. }

Improving the speed of builds that use kapt

Running kapt tasks in parallel

To improve the speed of builds that use kapt, you can enable the Gradle Worker API for kapt tasks. Using the Worker API lets Gradle run independent annotation processing tasks from a single project in parallel, which in some cases significantly decreases the execution time.

When you use the custom JDK home feature in the Kotlin Gradle plugin, kapt task workers use only process isolation mode. Note that the kapt.workers.isolation property is ignored.

If you want to provide additional JVM arguments for a kapt worker process, use the input kaptProcessJvmArgs of the KaptWithoutKotlincTask:

【Kotlin】

  1. tasks.withType<org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask>()
  2. .configureEach {
  3. kaptProcessJvmArgs.add("-Xmx512m")
  4. }

【Groovy】

  1. tasks.withType(org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask.class)
  2. .configureEach {
  3. kaptProcessJvmArgs.add('-Xmx512m')
  4. }

Caching for annotation processors’ classloaders

在 kapt 中缓存注解处理器的类加载器 is Experimental. It may be dropped or changed at any time. Use it only for evaluation purposes. We would appreciate your feedback on it in YouTrack.

kapt 编译器插件 - 图2

Caching for annotation processors’ classloaders helps kapt perform faster if you run many Gradle tasks consecutively.

To enable this feature, use the following properties in your gradle.properties file:

  1. # positive value will enable caching
  2. # use the same value as the number of modules that use kapt
  3. kapt.classloaders.cache.size=5
  4. # disable for caching to work
  5. kapt.include.compile.classpath=false

If you run into any problems with caching for annotation processors, disable caching for them:

  1. # specify annotation processors' full names to disable caching for them
  2. kapt.classloaders.cache.disableForProcessors=[annotation processors full names]

Measuring performance of annotation processors

Get a performance statistics on the annotation processors execution using the -Kapt-show-processor-timings plugin option. An example output:

  1. Kapt Annotation Processing performance report:
  2. com.example.processor.TestingProcessor: total: 133 ms, init: 36 ms, 2 round(s): 97 ms, 0 ms
  3. com.example.processor.AnotherProcessor: total: 100 ms, init: 6 ms, 1 round(s): 93 ms

You can dump this report into a file with the plugin option -Kapt-dump-processor-timings (org.jetbrains.kotlin.kapt3:dumpProcessorTimings). The following command will run kapt and dump the statistics to the ap-perf-report.file file:

  1. kotlinc -cp $MY_CLASSPATH \
  2. -Xplugin=kotlin-annotation-processing-SNAPSHOT.jar -P \
  3. plugin:org.jetbrains.kotlin.kapt3:aptMode=stubsAndApt,\
  4. plugin:org.jetbrains.kotlin.kapt3:apclasspath=processor/build/libs/processor.jar,\
  5. plugin:org.jetbrains.kotlin.kapt3:dumpProcessorTimings=ap-perf-report.file \
  6. -Xplugin=$JAVA_HOME/lib/tools.jar \
  7. -d cli-tests/out \
  8. -no-jdk -no-reflect -no-stdlib -verbose \
  9. sample/src/main/

Measuring the number of files generated with annotation processors

The kotlin-kapt Gradle plugin can report statistics on the number of generated files for each annotation processor.

This is useful to track if there are unused annotation processors as a part of the build. You can use the generated report to find modules that trigger unnecessary annotation processors and update the modules to prevent that.

Enable the statistics in two steps:

  • Set the showProcessorStats flag to true in your build.gradle(.kts):

    1. kapt {
    2. showProcessorStats = true
    3. }
  • Set the kapt.verbose Gradle property to true in your gradle.properties:

    1. kapt.verbose=true

You can also enable verbose output via the command line option verbose.

kapt 编译器插件 - 图3

The statistics will appear in the logs with the info level. You’ll see the Annotation processor stats: line followed by statistics on the execution time of each annotation processor. After these lines, there will be the Generated files report: line followed by statistics on the number of generated files for each annotation processor. For example:

  1. [INFO] Annotation processor stats:
  2. [INFO] org.mapstruct.ap.MappingProcessor: total: 290 ms, init: 1 ms, 3 round(s): 289 ms, 0 ms, 0 ms
  3. [INFO] Generated files report:
  4. [INFO] org.mapstruct.ap.MappingProcessor: total sources: 2, sources per round: 2, 0, 0

Compile avoidance for kapt

To improve the times of incremental builds with kapt, it can use the Gradle compile avoidance. With compile avoidance enabled, Gradle can skip annotation processing when rebuilding a project. Particularly, annotation processing is skipped when:

  • The project’s source files are unchanged.
  • The changes in dependencies are ABI compatible. For example, the only changes are in method bodies.

However, compile avoidance can’t be used for annotation processors discovered in the compile classpath since any changes in them require running the annotation processing tasks.

To run kapt with compile avoidance:

  • Add the annotation processor dependencies to the kapt* configurations manually as described above.
  • Turn off the discovery of annotation processors in the compile classpath by adding this line to your gradle.properties file:
  1. kapt.include.compile.classpath=false

Incremental annotation processing

kapt supports incremental annotation processing that is enabled by default. Currently, annotation processing can be incremental only if all annotation processors being used are incremental.

To disable incremental annotation processing, add this line to your gradle.properties file:

  1. kapt.incremental.apt=false

Note that incremental annotation processing requires incremental compilation to be enabled as well.

Java compiler options

kapt uses Java compiler to run annotation processors.
Here is how you can pass arbitrary options to javac:

  1. kapt {
  2. javacOptions {
  3. // Increase the max count of errors from annotation processors.
  4. // Default is 100.
  5. option("-Xmaxerrs", 500)
  6. }
  7. }

Non-existent type correction

Some annotation processors (such as AutoFactory) rely on precise types in declaration signatures. By default, kapt replaces every unknown type (including types for the generated classes) to NonExistentClass, but you can change this behavior. Add the option to the build.gradle(.kts) file to enable error type inferring in stubs:

  1. kapt {
  2. correctErrorTypes = true
  3. }

Using in Maven

Add an execution of the kapt goal from kotlin-maven-plugin before compile:

  1. <execution>
  2. <id>kapt</id>
  3. <goals>
  4. <goal>kapt</goal> <!-- You can skip the <goals> element
  5. if you enable extensions for the plugin -->
  6. </goals>
  7. <configuration>
  8. <sourceDirs>
  9. <sourceDir>src/main/kotlin</sourceDir>
  10. <sourceDir>src/main/java</sourceDir>
  11. </sourceDirs>
  12. <annotationProcessorPaths>
  13. <!-- Specify your annotation processors here -->
  14. <annotationProcessorPath>
  15. <groupId>com.google.dagger</groupId>
  16. <artifactId>dagger-compiler</artifactId>
  17. <version>2.9</version>
  18. </annotationProcessorPath>
  19. </annotationProcessorPaths>
  20. </configuration>
  21. </execution>

To configure the level of annotation processing, set one of the following as the aptMode in the <configuration> block:

  • stubs – only generate stubs needed for annotation processing.
  • apt – only run annotation processing.
  • stubsAndApt – (default) generate stubs and run annotation processing.

For example:

  1. <configuration>
  2. ...
  3. <aptMode>stubs</aptMode>
  4. </configuration>

Using in IntelliJ build system

kapt is not supported for IntelliJ IDEA’s own build system. Launch the build from the “Maven Projects” toolbar whenever you want to re-run the annotation processing.

Using in CLI

kapt compiler plugin is available in the binary distribution of the Kotlin compiler.

You can attach the plugin by providing the path to its JAR file using the Xplugin kotlinc option:

  1. -Xplugin=$KOTLIN_HOME/lib/kotlin-annotation-processing.jar

Here is a list of the available options:

  • sources (required): An output path for the generated files.
  • classes (required): An output path for the generated class files and resources.
  • stubs (required): An output path for the stub files. In other words, some temporary directory.
  • incrementalData: An output path for the binary stubs.
  • apclasspath (repeatable): A path to the annotation processor JAR. Pass as many apclasspath options as many JARs you have.
  • apoptions: A base64-encoded list of the annotation processor options. See AP/javac options encoding for more information.
  • javacArguments: A base64-encoded list of the options passed to javac. See AP/javac options encoding for more information.
  • processors: A comma-specified list of annotation processor qualified class names. If specified, kapt does not try to find annotation processors in apclasspath.
  • verbose: Enable verbose output.
  • aptMode (required)
    • stubs – only generate stubs needed for annotation processing.
    • apt – only run annotation processing.
    • stubsAndApt – generate stubs and run annotation processing.
  • correctErrorTypes: See below. Disabled by default.

The plugin option format is: -P plugin:<plugin id>:<key>=<value>. Options can be repeated.

An example:

  1. -P plugin:org.jetbrains.kotlin.kapt3:sources=build/kapt/sources
  2. -P plugin:org.jetbrains.kotlin.kapt3:classes=build/kapt/classes
  3. -P plugin:org.jetbrains.kotlin.kapt3:stubs=build/kapt/stubs
  4. -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=lib/ap.jar
  5. -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=lib/anotherAp.jar
  6. -P plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true

Generating Kotlin sources

kapt can generate Kotlin sources. Just write the generated Kotlin source files to the directory specified by processingEnv.options["kapt.kotlin.generated"], and these files will be compiled together with the main sources.

Note that kapt does not support multiple rounds for the generated Kotlin files.

AP/Javac options encoding

apoptions and javacArguments CLI options accept an encoded map of options.
Here is how you can encode options by yourself:

  1. fun encodeList(options: Map<String, String>): String {
  2. val os = ByteArrayOutputStream()
  3. val oos = ObjectOutputStream(os)
  4. oos.writeInt(options.size)
  5. for ((key, value) in options.entries) {
  6. oos.writeUTF(key)
  7. oos.writeUTF(value)
  8. }
  9. oos.flush()
  10. return Base64.getEncoder().encodeToString(os.toByteArray())
  11. }

Keeping Java compiler’s annotation processors

By default, kapt runs all annotation processors and disables annotation processing by javac. However, you may need some of javac’s annotation processors working (for example, Lombok).

In the Gradle build file, use the option keepJavacAnnotationProcessors:

  1. kapt {
  2. keepJavacAnnotationProcessors = true
  3. }

If you use Maven, you need to specify concrete plugin settings. See this example of settings for the Lombok compiler plugin.