KSP vs KAPT
- Kotlin course
- Expert
- Lesson 10/47
- 5 min
Learn the difference between KSP and KAPT.
KSP vs KAPT
KSP and KAPT both generate code at compile time, but they take different paths. KSP reads Kotlin symbols directly; KAPT first converts Kotlin to a Java stub.
KSP is up to 2x faster; it supports Kotlin-specific constructs (suspend functions, inline classes) better. KAPT is older and more common; legacy libraries like Dagger still require KAPT. When starting a new project, prefer KSP; if a dependency forces KAPT, plan a migration. Both generate code at compile time; neither has runtime cost.
Migration plan
Migrating from KAPT to KSP is often simple: in the Gradle file, replace kapt with ksp and update dependencies.
Example: instead of kapt("com.example:processor:1.0") write ksp("com.example:processor:1.0"). With Kotlin 2.x the kapt plugin still works, but KSP is preferred. If a dependency does not offer KSP, keep KAPT temporarily; once the library adds KSP support, remove it fully. This keeps the project working.
Practice Write the comparison table
Define data class Tool(val name: String, val factor: Int). In main, build listOf(Tool("KSP", 1), Tool("KAPT", 3)). For each tool, write println("${'$'}{it.name}: ${'$'}{it.factor}x").
Starter code
// add the Tool data class
fun main() {
// create two Tool instances and print each
}Sample solution
data class Tool(val name: String, val factor: Int)
fun main() {
val tools = listOf(Tool("KSP", 1), Tool("KAPT", 3))
tools.forEach { println("${it.name}: ${it.factor}x") }
}Expected output
KSP: 1x KAPT: 3x
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which one is faster?
- KAPT, because it uses Java
- KSP, because it reads Kotlin directly Correct answer
- It depends on the library
- Both are equally fast
Explanation: KSP is faster because it reads Kotlin symbols directly.
2 Write KSP speed
val ksp = 1
val kapt = ____
println("KSP ${ksp}x, KAPT ${kapt}x")
val ksp = 1
val kapt = 3
println("KSP ${ksp}x, KAPT ${kapt}x")
Explanation: KAPT is typically up to 3x slower.
3 Match KSP/KAPT
| Term | Meaning |
|---|---|
KSP |
Reads Kotlin directly |
KAPT |
Converts to Java stub |
ksp(...) |
KSP dependency |
kapt(...) |
KAPT dependency |