Migrate from kapt to KSP

Kapt (the Kotlin Annotation Processing Tool) lets you 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 language constructs.

You can run kapt and KSP alongside each other in your project while you're migrating, and the migration can be done module by module, library by library.

Here's an overview of the migration steps:

  1. Check the libraries you use for KSP support
  2. Add the KSP plugin to your project
  3. Replace annotation processors with KSP
  4. Remove the kapt plugin

Check the libraries you use for KSP support

To get started, check if the libraries you're using with kapt already have KSP support. This is the case for many popular libraries (including Dagger, Glide, Room, and Moshi), and others are adding support.

You can check the list of supported libraries in the documentation, or refer to the documentation and issue tracker of the libraries you're using.

Add the 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.

Kotlin

plugins {
    id("com.google.devtools.ksp") version "2.0.21-1.0.27" apply false
}

Groovy

plugins {
    id 'com.google.devtools.ksp' version '2.0.21-1.0.27' apply false
}

Then, enable KSP in your module-level build.gradle.kts file:

Kotlin

plugins {
    id("com.google.devtools.ksp")
}

Groovy

plugins {
    id 'com.google.devtools.ksp'
}

Replace annotation processors with KSP

With KSP enabled, you can start replacing usages of kapt with KSP. For a vast majority of libraries, this just requires changing kapt to ksp at the dependency declaration, as they ship their annotation processor and KSP processor in the same artifact.

Kotlin

dependencies {
    kapt("androidx.room:room-compiler:2.5.0")
    ksp("androidx.room:room-compiler:2.5.0")
}

Groovy

dependencies {
    kapt 'androidx.room:room-compiler:2.5.0'
    ksp 'androidx.room:room-compiler:2.5.0'
}

After moving to KSP, sync and build your project to see if it still works correctly.

Some common issues to look out for:

  • Some libraries don't support the exact same set of features with kapt and KSP. If your code breaks after migrating, check the library's documentation.
  • KSP has more accurate Kotlin type information than kapt (for example, about nullability), which means that KSP processors can be more precise about type requirements. This might require some fixes in your source code as well, in addition to updating your build files.
  • If you were previously passing in arguments to the annotation processor, you'll likely need to pass in those arguments to KSP now. Note that the format of the arguments might differ between kapt and KSP. See the KSP documentation and consult the documentation of the library you're using to learn more.

Remove the kapt plugin

When you have no dependencies included with kapt in your module anymore, remove the kapt plugin.

If it was declared in a plugins block:

Kotlin

plugins {
    id("org.jetbrains.kotlin.kapt")
}

Groovy

plugins {
    id 'org.jetbrains.kotlin.kapt'
}

If it was using the apply plugin syntax using Groovy:

apply plugin: 'kotlin-kapt'

You should also remove any leftover configuration related to kapt, such as:

Kotlin


kapt {
    correctErrorTypes = true
    useBuildCache = true
}

Groovy


kapt {
    correctErrorTypes true
    useBuildCache true
}

Additional resources