Skip to content

Commit 84ccb59

Browse files
authored
update: update the kapt plugin docs (#4922)
* update: update the kapt plugin docs * fix: review
1 parent 7b6880e commit 84ccb59

File tree

1 file changed

+71
-96
lines changed

1 file changed

+71
-96
lines changed

docs/topics/kapt.md

Lines changed: 71 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@
88

99
Annotation processors (see [JSR 269](https://jcp.org/en/jsr/detail?id=269)) are supported in Kotlin with the _kapt_ compiler plugin.
1010

11-
In a nutshell, you can use libraries such as [Dagger](https://google.github.io/dagger/) or
12-
[Data Binding](https://developer.android.com/topic/libraries/data-binding/index.html) in your Kotlin projects.
13-
14-
Please read below about how to apply the *kapt* plugin to your Gradle/Maven build.
11+
In a nutshell, kapt helps you use libraries like [Dagger](https://google.github.io/dagger/)
12+
and [Data Binding](https://developer.android.com/topic/libraries/data-binding/index.html)
13+
in your Kotlin projects by enabling Java-based annotation processing.
14+
15+
> If you encounter any issues when using kapt with the K2 compiler,
16+
> report them to our [issue tracker](http://kotl.in/issue) and disable the K2 mode in your `gradle.properties` file:
17+
>
18+
> ```kotlin
19+
> kapt.use.k2=false
20+
> ```
21+
>
22+
{style="note"}
1523
1624
## Use in Gradle
1725
18-
Follow these steps:
19-
1. Apply the `kotlin-kapt` Gradle plugin:
26+
To use kapt in Gradle, follow these steps:
27+
28+
1. Apply the `kapt` Gradle plugin in your build script file `build.gradle(.kts)`:
2029
2130
<tabs group="build-script">
2231
<tab title="Kotlin" group-key="kotlin">
@@ -39,7 +48,7 @@ Follow these steps:
3948
</tab>
4049
</tabs>
4150

42-
2. Add the respective dependencies using the `kapt` configuration in your `dependencies` block:
51+
2. Add the respective dependencies using the `kapt` configuration in the `dependencies {}` block:
4352

4453
<tabs group="build-script">
4554
<tab title="Kotlin" group-key="kotlin">
@@ -67,47 +76,14 @@ Follow these steps:
6776
If your project contains Java classes, `kapt` will also take care of them.
6877

6978
If you use annotation processors for your `androidTest` or `test` sources, the respective `kapt` configurations are named
70-
`kaptAndroidTest` and `kaptTest`. Note that `kaptAndroidTest` and `kaptTest` extends `kapt`, so you can just provide the
71-
`kapt` dependency and it will be available both for production sources and tests.
72-
73-
## Try Kotlin K2 compiler
74-
75-
> Support for K2 in the kapt compiler plugin is [Experimental](components-stability.md). Opt-in is required (see details below),
76-
> and you should use it only for evaluation purposes.
77-
>
78-
{style="warning"}
79-
80-
From Kotlin 1.9.20, you can try using the kapt compiler plugin with the [K2 compiler](https://blog.jetbrains.com/kotlin/2021/10/the-road-to-the-k2-compiler/),
81-
which brings performance improvements and many other benefits. To use the K2 compiler in your Gradle project, add the following
82-
option to your `gradle.properties` file:
83-
84-
```kotlin
85-
kapt.use.k2=true
86-
```
87-
88-
If you use the Maven build system, update your `pom.xml` file:
89-
90-
```xml
91-
<configuration>
92-
...
93-
<args>
94-
<arg>-Xuse-k2-kapt</arg>
95-
</args>
96-
</configuration>
97-
```
98-
99-
> To enable the kapt plugin in your Maven project, see [](#use-in-maven).
100-
>
101-
{style="tip"}
102-
103-
If you encounter any issues when using kapt with the K2 compiler, please report them to our
104-
[issue tracker](http://kotl.in/issue).
79+
`kaptAndroidTest` and `kaptTest`. Note that `kaptAndroidTest` and `kaptTest` extend `kapt`, so you can provide the
80+
`kapt` dependency, and it will be available both for production sources and tests.
10581

10682
## Annotation processor arguments
10783

108-
Use `arguments {}` block to pass arguments to annotation processors:
84+
Use the `arguments {}` block in your build script file `build.gradle(.kts)` to pass arguments to annotation processors:
10985

110-
```groovy
86+
```kotlin
11187
kapt {
11288
arguments {
11389
arg("key", "value")
@@ -118,10 +94,11 @@ kapt {
11894
## Gradle build cache support
11995

12096
The kapt annotation processing tasks are [cached in Gradle](https://guides.gradle.org/using-build-cache/) by default.
121-
However, annotation processors run arbitrary code that may not necessarily transform the task inputs into the outputs,
122-
might access and modify the files that are not tracked by Gradle etc. If the annotation processors used in the build cannot
123-
be properly cached, it is possible to disable caching for kapt entirely by adding the following lines to the build script,
124-
in order to avoid false-positive cache hits for the kapt tasks:
97+
However, annotation processors can run arbitrary code, which may not reliably transform task inputs into outputs,
98+
or may access and modify files that Gradle doesn't track.
99+
If the annotation processors used in the build cannot be properly cached,
100+
you can disable caching for kapt entirely by specifying the `useBuildCache` property in the build script.
101+
This helps prevent false-positive cache hits for the kapt tasks:
125102

126103
```groovy
127104
kapt {
@@ -168,35 +145,39 @@ tasks.withType(org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask.class
168145

169146
### Caching for annotation processors' classloaders
170147

171-
> Caching for annotation processors' classloaders in kapt is [Experimental](components-stability.md).
172-
> It may be dropped or changed at any time. Use it only for evaluation purposes.
173-
> We would appreciate your feedback on it in [YouTrack](https://youtrack.jetbrains.com/issue/KT-28901).
174-
>
175-
{style="warning"}
148+
<primary-label ref="experimental-general"/>
176149

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

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

181154
```none
182-
# positive value will enable caching
183-
# use the same value as the number of modules that use kapt
155+
# gradle.properties
156+
#
157+
# Any positive value enables caching
158+
# Use the same value as the number of modules that use kapt
184159
kapt.classloaders.cache.size=5
185160
186-
# disable for caching to work
161+
# Disable for caching to work
187162
kapt.include.compile.classpath=false
188163
```
189164

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

192167
```none
193-
# specify annotation processors' full names to disable caching for them
168+
# Specify annotation processors' full names to disable caching for them
194169
kapt.classloaders.cache.disableForProcessors=[annotation processors full names]
195170
```
196171

172+
> If you encounter any issues with the feature,
173+
> we would appreciate your feedback in [YouTrack](https://youtrack.jetbrains.com/issue/KT-28901).
174+
>
175+
{style="note"}
176+
197177
### Measure performance of annotation processors
198178

199-
Get a performance statistics on the annotation processors execution using the `-Kapt-show-processor-timings` plugin option.
179+
To get performance statistics on the annotation processors execution,
180+
use the `-Kapt-show-processor-timings` plugin option.
200181
An example output:
201182

202183
```text
@@ -222,33 +203,37 @@ sample/src/main/
222203

223204
### Measure the number of files generated with annotation processors
224205

225-
The `kotlin-kapt` Gradle plugin can report statistics on the number of generated files for each annotation processor.
206+
The `kapt` Gradle plugin can report statistics on the number of generated files for each annotation processor.
207+
208+
This helps track whether any unused annotation processors are included in the build.
209+
You can use the generated report to find modules that trigger unnecessary annotation processors and update the modules to avoid that.
226210

227-
This is useful to track if there are unused annotation processors as a part of the build.
228-
You can use the generated report to find modules that trigger unnecessary annotation processors and update the modules to prevent that.
211+
To enable statistics reporting:
229212

230-
Enable the statistics in two steps:
231-
* Set the `showProcessorStats` flag to `true` in your `build.gradle(.kts)`:
213+
1. Set the `showProcessorStats` property value to `true` in your `build.gradle(.kts)`:
232214

233-
```kotlin
234-
kapt {
235-
showProcessorStats = true
236-
}
237-
```
215+
```kotlin
216+
// build.gradle.kts
217+
kapt {
218+
showProcessorStats = true
219+
}
220+
```
238221

239-
* Set the `kapt.verbose` Gradle property to `true` in your `gradle.properties`:
222+
2. Set the `kapt.verbose` Gradle property to `true` in your `gradle.properties`:
240223

241-
```none
242-
kapt.verbose=true
243-
```
224+
```none
225+
# gradle.properties
226+
kapt.verbose=true
227+
```
244228

245-
> You can also enable verbose output via the [command line option `verbose`](#use-in-cli).
229+
> You can also enable verbose output with the [command line option `verbose`](#use-in-cli).
246230
>
247-
> {style="note"}
231+
{style="note"}
248232

249-
The statistics will appear in the logs with the `info` level. You'll see the `Annotation processor stats:` line followed by
250-
statistics on the execution time of each annotation processor. After these lines, there will be the `Generated files report:` line
251-
followed by statistics on the number of generated files for each annotation processor. For example:
233+
The statistics appear in the logs with the `info` level.
234+
You can see the `Annotation processor stats:` line followed by statistics on the execution time of each annotation processor.
235+
After these lines, there is the `Generated files report:` line followed by statistics on the number of
236+
generated files for each annotation processor. For example:
252237

253238
```text
254239
[INFO] Annotation processor stats:
@@ -264,23 +249,24 @@ With compile avoidance enabled, Gradle can skip annotation processing when rebui
264249
processing is skipped when:
265250

266251
* The project's source files are unchanged.
267-
* The changes in dependencies are [ABI](https://en.wikipedia.org/wiki/Application_binary_interface) compatible.
252+
* The changes in dependencies are [ABI](https://en.wikipedia.org/wiki/Application_binary_interface)-compatible.
268253
For example, the only changes are in method bodies.
269254

270255
However, compile avoidance can't be used for annotation processors discovered in the compile classpath since _any changes_
271256
in them require running the annotation processing tasks.
272257

273258
To run kapt with compile avoidance:
274-
* Add the annotation processor dependencies to the `kapt*` configurations manually as described [above](#use-in-gradle).
275-
* Turn off the discovery of annotation processors in the compile classpath by adding this line to your `gradle.properties` file:
259+
* [Add the annotation processor dependencies to the `kapt*` configurations manually](#use-in-gradle).
260+
* Turn off the discovery of annotation processors in the compile classpath in the `gradle.properties` file:
276261

277-
```none
278-
kapt.include.compile.classpath=false
279-
```
262+
```none
263+
# gradle.properties
264+
kapt.include.compile.classpath=false
265+
```
280266

281267
## Incremental annotation processing
282268

283-
kapt supports incremental annotation processing that is enabled by default.
269+
kapt supports incremental annotation processing by default.
284270
Currently, annotation processing can be incremental only if all annotation processors being used are incremental.
285271

286272
To disable incremental annotation processing, add this line to your `gradle.properties` file:
@@ -385,17 +371,6 @@ For example:
385371
</configuration>
386372
```
387373

388-
To enable the kapt plugin with the K2 compiler, add the `-Xuse-k2-kapt` compiler option:
389-
390-
```xml
391-
<configuration>
392-
...
393-
<args>
394-
<arg>-Xuse-k2-kapt</arg>
395-
</args>
396-
</configuration>
397-
```
398-
399374
## Use in IntelliJ build system
400375

401376
kapt is not supported for IntelliJ IDEA's own build system. Launch the build from the "Maven Projects"
@@ -486,4 +461,4 @@ kapt {
486461
```
487462

488463
If you use Maven, you need to specify concrete plugin settings.
489-
See this [example of settings for the Lombok compiler plugin](lombok.md#using-with-kapt).
464+
See this [example of settings for the Lombok compiler plugin](lombok.md#using-with-kapt).

0 commit comments

Comments
 (0)