You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/topics/kapt.md
+71-96Lines changed: 71 additions & 96 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,15 +8,24 @@
8
8
9
9
Annotation processors (see [JSR 269](https://jcp.org/en/jsr/detail?id=269)) are supported in Kotlin with the _kapt_ compiler plugin.
10
10
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"}
15
23
16
24
## UseinGradle
17
25
18
-
Follow these steps:
19
-
1. Apply the `kotlin-kapt` Gradle plugin:
26
+
To use kapt inGradle, follow these steps:
27
+
28
+
1. Apply the `kapt` Gradle plugin in your build script file `build.gradle(.kts)`:
20
29
21
30
<tabs group="build-script">
22
31
<tab title="Kotlin" group-key="kotlin">
@@ -39,7 +48,7 @@ Follow these steps:
39
48
</tab>
40
49
</tabs>
41
50
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:
43
52
44
53
<tabsgroup="build-script">
45
54
<tabtitle="Kotlin"group-key="kotlin">
@@ -67,47 +76,14 @@ Follow these steps:
67
76
If your project contains Java classes, `kapt` will also take care of them.
68
77
69
78
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.
105
81
106
82
## Annotation processor arguments
107
83
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:
109
85
110
-
```groovy
86
+
```kotlin
111
87
kapt {
112
88
arguments {
113
89
arg("key", "value")
@@ -118,10 +94,11 @@ kapt {
118
94
## Gradle build cache support
119
95
120
96
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:
0 commit comments