Android: Migrate from kapt to KSP
Kapt (the Kotlin Annotation Processing Tool) allows you to use Java annotation processors with Kotlin code, even if those processors don’t have specific support for Kotlin. This is done by generating Java stubs from your Kotlin files that the processors can then read. This stub generation is an expensive operation and has a significant impact on build speed.
KSP (Kotlin Symbol Processing) is a Kotlin-first alternative to kapt. KSP analyzes Kotlin code directly, which is up to 2x faster. It also has a better understanding of Kotlin’s language constructs.
Kapt is now in maintenance mode, and we recommend migrating from kapt to KSP wherever possible. In most cases, this migration only requires changes to your project’s build configuration.
Migration steps:
Step 1: Check the kapt libraries that you are using in your project
Step 2: Check the KSP library available. You can check the list of supported libraries in the documentation
Step 3: Add KSP plugin to your project
First, declare the KSP plugin in your top level build.gradle.kts
file. Make sure that you choose a KSP version aligned with your project's Kotlin version. You can find a list of releases on the KSP GitHub page.
// Top level build.gradle
plugins {
id("com.google.devtools.ksp") version "1.8.10-1.0.9" apply false
}
// Module level build.gradle
plugins {
id("com.google.devtools.ksp")
}
Step 4: Replace kapt library with KSP
dependencies {
kapt("androidx.room:room-compiler:2.5.0") // Remove this line
ksp("androidx.room:room-compiler:2.5.0") // Add this line
}
Step 5: Sync build and make sure build working
Step 6: Remove kapt plugin from gradle files
plugins {
id("org.jetbrains.kotlin.kapt") // Remove this line
}