# Use annotation processors in Kotlin projects
* Use [kapt](kapt.html) if: * You have a Maven project. * You have a Gradle project, but the required Java annotation processor doesn't support KSP yet. [See the list of supported libraries](ksp-overview.html#supported-libraries).
* Use [KSP](ksp-overview.html) if: * You have a Gradle project, and the required Java annotation processor supports KSP. * You want to create your own annotation processors.
Annotation processors analyze your source code at compile time to generate boilerplate code, validate usage, or produce other artifacts.
Kotlin supports two ways to work with annotation processors:
* [The kapt compiler plugin](#use-kapt-with-java-annotation-processors) works by generating stub files from Kotlin source code and then running the Java annotation processors on those stubs. This extra stub-generation step makes the build time slower and means kapt can't understand Kotlin-specific constructs, such as [extension functions](extensions.html) or [null safety](null-safety.html). kapt supports both Maven and Gradle. It's recommended for all Maven projects and for Gradle projects with processor libraries that haven't yet adopted KSP, such as [MapStruct](https://mapstruct.org/).
* [The KSP framework](#use-ksp-in-gradle-projects) reads Kotlin source code directly through a Kotlin-first API, without generating stubs. It understands Kotlin-specific features natively and runs builds faster than kapt. Currently, KSP has official support only for Gradle. It's recommended for writing your own processors and working with KSP-compatible libraries like [Dagger](https://dagger.dev/).
## Use kapt with Java annotation processors
[kapt](kapt.html) lets you use existing Java annotation processors in Kotlin projects without any changes to the processors
themselves.
The example below shows how to use the [MapStruct](https://mapstruct.org/) annotation processor, which generates type-safe
mapper implementations between Java beans at compile time.
1. In your build file, apply the `kapt` plugin and add MapStruct to the `dependencies` section:
Maven:
```XML
2.4.21
17
1.6.3
org.mapstruct
mapstruct
${mapstruct.version}
org.jetbrains.kotlin
kotlin-maven-plugin
${kotlin.version}
true
org.mapstruct
mapstruct-processor
${mapstruct.version}
```
* The `true` option configures kapt [automatically](maven-configure-project.html#automatic-configuration). It registers the `src/main/kotlin` and `src/test/kotlin` source roots and adds the `kapt` and `compile` executions bound to the correct lifecycle phases, so the `kapt` goal runs before compilation.
* The `maven.compiler.release` property configures both the Java and Kotlin compilers, so the Java sources generated by kapt are compiled against the same JVM target.
Gradle Kotlin:
```KOTLIN
plugins {
kotlin("kapt") version "2.4.21"
}
dependencies {
implementation("org.mapstruct:mapstruct:1.6.3")
kapt("org.mapstruct:mapstruct-processor:1.6.3")
}
```
Gradle Groovy:
```GROOVY
plugins {
id "org.jetbrains.kotlin.kapt" version "2.4.21"
}
dependencies {
implementation "org.mapstruct:mapstruct:1.6.3"
kapt "org.mapstruct:mapstruct-processor:1.6.3"
}
```
2. Define your data classes and a mapper interface:
```KOTLIN
import org.mapstruct.Mapper
import org.mapstruct.factory.Mappers
data class UserDto(val id: Long, val firstName: String, val lastName: String)
data class UserEntity(val id: Long, val firstName: String, val lastName: String)
@Mapper
interface UserMapper {
fun toDto(entity: UserEntity): UserDto
fun toEntity(dto: UserDto): UserEntity
companion object : UserMapper by Mappers.getMapper(UserMapper::class.java)
}
```
3. Build the project. MapStruct generates the `UserMapperImpl` class in the generated sources' directory.
Use the `UserMapper` companion object to call the generated implementation:
```KOTLIN
fun main() {
val entity = UserEntity(id = 1L, firstName = "John", lastName = "Doe")
val dto = UserMapper.toDto(entity)
println(dto)
// UserDto(id=1, firstName=John, lastName=Doe)
}
```
## Use KSP in Gradle projects
With [KSP](ksp-overview.html), you can use existing annotation processors in Gradle projects and create your own
processors that generate code based on annotations in your source code.
### Use KSP with Java annotation processors
For Gradle projects, use KSP with the compatible annotation processors. KSP is faster than kapt and
can understand Kotlin-specific features natively. See the list of [libraries that already support KSP](ksp-overview.html#supported-libraries).
The example below shows how to use [Dagger](https://dagger.dev/), a compile-time dependency injection framework that
generates the wiring code for your dependency graph.
1. In your `build.gradle(.kts)` file, apply the KSP plugin and add Dagger to the `dependencies` block:
Kotlin:
```KOTLIN
// build.gradle.kts
plugins {
kotlin("jvm") version "2.4.21"
id("com.google.devtools.ksp") version "2.3.12"
}
dependencies {
implementation("com.google.dagger:dagger:2.59.2")
ksp("com.google.dagger:dagger-compiler:2.59.2")
}
```
Groovy:
```GROOVY
// build.gradle
plugins {
id 'org.jetbrains.kotlin.jvm' version '2.4.21'
id 'com.google.devtools.ksp' version '2.3.12'
}
dependencies {
implementation 'com.google.dagger:dagger:2.59.2'
ksp 'com.google.dagger:dagger-compiler:2.59.2'
}
```
Tip:
To find the latest version of KSP, check the GitHub [Releases](https://github.com/google/ksp/releases) page.
2. Annotate your Kotlin classes with Dagger annotations:
```KOTLIN
import javax.inject.Inject
import javax.inject.Singleton
import dagger.Component
import dagger.Module
import dagger.Provides
@Singleton
class UserRepository @Inject constructor() {
fun getUser(): String = "John Doe"
}
@Module
class AppModule {
@Provides
@Singleton
fun provideUserRepository(): UserRepository = UserRepository()
}
@Singleton
@Component(modules = [AppModule::class])
interface AppComponent {
fun userRepository(): UserRepository
}
```
3. Build the project. Dagger generates implementation classes, such as `DaggerAppComponent` in the
`build/generated/ksp` directory. Use the generated class in your code:
```KOTLIN
fun main() {
val appComponent = DaggerAppComponent.create()
val userRepository = appComponent.userRepository()
println("User: ${userRepository.getUser()}")
// User: John Doe
}
```
For more information on Dagger support for KSP, see its [documentation](https://dagger.dev/dev-guide/ksp.html).
### Create your own annotation processor
You can use the KSP API to write your own annotation processors that generate code at compile time.
A new processor requires three modules:
* An `annotation` module that declares the custom annotation.
* A `processor` module that implements the `SymbolProcessor` and `SymbolProcessorProvider` factories. `SymbolProcessor` contains the main logic, while `SymbolProcessorProvider` creates the processor and registers the provider in the `META-INF/services/` path.
* An `app` module that applies the KSP plugin depends on the processor, and uses the annotation.
For complete step-by-step instructions, see the [KSP quickstart](ksp-quickstart.html#create-your-own-processor).
## What's next
* [Learn more about kapt configuration](kapt.html)
* [Get started with KSP](ksp-quickstart.html)
* [See how to migrate from kapt to KSP](ksp-kapt-migration.html)