kapt vs KSP

Runtime, build and Gradle Mid-level Kotlin 2.0+ API 24+ Reviewed By Best Latest Apps

Pick the fix

A build spends a long time in kaptGenerateStubsDebugKotlin. Room and Hilt are the only processors. What should you do?

Options
Hint 1 of 1

Do both libraries support KSP?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Switch Room and Hilt to KSP and remove the kapt plugin

Correct. Both support KSP, so the stub step disappears.

Why not the others?
  • B Add more Gradle memory. Helps a little; doesn't remove the work.
  • C Turn off incremental compilation. Slower.
  • D Move to Java. Not a real option.

30-second answer

Both run annotation processors such as Room, Dagger/Hilt and Moshi. kapt generates Java stubs from Kotlin so Java annotation processors can run, which is slow. KSP (Kotlin Symbol Processing) reads Kotlin symbols directly and is often about twice as fast. Room, Hilt and most popular libraries support KSP, so new projects should use it and drop kapt where possible.

Practise saying it · 25 s. No recording, just a timer

Explain kapt vs KSP.

Say your answer out loud, then check which of these points you covered:

  • Both run processors
  • kapt makes Java stubs
  • KSP reads Kotlin directly
  • Migrate to KSP

Deep dive

KSP2 is the current implementation and works with the K2 compiler.

Removing the last kapt processor lets you remove the kapt plugin and its stub step.

KSP processors can't change existing code; they only generate new files.

Compiler plugins (Compose, serialization) are different from annotation processors.

Using KSP for Room
plugins {
    id("com.google.devtools.ksp")
}

dependencies {
    implementation(libs.androidx.room.runtime)
    ksp(libs.androidx.room.compiler)
}

Common mistakes

  • Keeping kapt for one library when a KSP version exists.
  • Confusing compiler plugins with annotation processors.
  • Mixing kapt and KSP for the same processor.

Senior take & follow-ups

Explain the stub step and the migration path.

Follow-up questions

Think of your answer first, then open the hint.

  1. Why is kapt slow?

    Show hint

    It generates Java stubs for all Kotlin sources first.

  2. Is the Compose compiler a KSP processor?

    Show hint

    No, it's a Kotlin compiler plugin.

Suggest a fix if something here is wrong or out of date.