Choosing the ideal Annotation Processing Tool: Kapt vs KSP

Mohamed Gamal
2 min readOct 26, 2023

--

  • Kapt and KSP are tools in Android for dealing with special instructions called annotations. They help make code work better. Here’s the main difference:

Kapt (Kotlin Annotation Processing Tool):

  • Helps Kotlin and Java talk to each other.
  • Makes Java tools understand Kotlin code.
  • Slower because it does extra work.

KSP (Kotlin Symbol Processing):

  • Made just for Kotlin.
  • Understands Kotlin really well.
  • Faster (up to 2x faster) and better with Kotlin code.

To add KSP plugin to your project:

First, declare the KSP plugin in your top level build.gradle.kts file:

plugins {
id ‘com.google.devtools.ksp’ version ‘1.8.10-1.0.9' apply false
}

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

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

Then, replace annotation processors kapt with KSP like this:

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

And, of course remove any configuration related with kapt plugins:

plugins {
id ‘org.jetbrains.kotlin.kapt’
}

apply plugin: ‘kotlin-kapt’


kapt {
correctErrorTypes true
useBuildCache true
}

Which One to Use?

  • For new Kotlin projects, KSP is great!
  • For older projects with lots of Java, Kapt is easier.

Conclusion:

So, Kapt is like a translator it translates Kotlin code into Java for processors, and KSP is like a native speaker of Kotlin it understands Kotlin directly. Pick the one that fits your project! 🚀👨‍💻

Also, you can check the libraries you use for KSP support: https://kotlinlang.org/docs/ksp-overview.html#supported-libraries

👨‍💻You can find me here.

--

--

Mohamed Gamal
Mohamed Gamal

Written by Mohamed Gamal

Native Android Developer | kotlin Developer

No responses yet