KAPT to Kotlin KSP: Unleashing the Power of Kotlin Symbolic Processing

Amit Patoliya
3 min readJan 12, 2024

--

Kotlin Symbolic Processing

Why KSP(Kotlin Symbolic Processing)?

KAPT (Kotlin Annotation Processing Tool) serves as a Java annotation processor, translating Kotlin code into a Java stub for processor interpretation — a two-step process that can be time-intensive.

Enter KSP, the Kotlin Symbolic Processor, designed to analyze Kotlin code directly. This direct approach makes KSP twice as fast as KAPT, offering a more efficient solution for code processing.

Migrating to KSP is a straightforward process:

  1. Identify libraries that support KSP for compatibility.
  2. Integrate the KSP plugin into your project.
  3. Make the switch from kapt to ksp in your codebase.
  4. After updating all libraries, safely remove the Kapt plugin for a clean transition.

When you’re updating or moving your project to use new tools (kapt and KSP), you can use them at the same time in your project. This makes it easier to switch over gradually, step by step, by updating one module or library at a time.

Let’s see an example with Dagger Hilt and the room database.

Prerequisite:

Before you add the Ksp plugin make sure the application uses the kotlin plugin version 1.9.0 and above.

You can check the list of supported libraries in the documentation.

Dagger 2.48 (or above)
Kotlin 1.9.0 (or above)
KSP 1.9.0-1.0.12 (or above)

Step 1: Incorporate the KSP plugin, alongside the Dagger Hilt plugin (version 2.48, compatible with KSP), into your root Gradle file.

Root gradle file

Step 2: Extend the integration by adding the KSP plugin specifically to your app-level Gradle.

App-level Gradle

Step 3: Update your Gradle dependencies, Replace Kapt for KSP.

App-level gradle file

Once the setup is done, synchronize your project to harness a 2X faster build speed, enhancing your overall development experience.

Conclusion:
Kotlin Symbolic Processing (KSP) is a superior choice for annotation processing in Kotlin projects. It provides 2x build performance, enhanced accuracy, comprehensive support for Kotlin features, and improved developer experience making it the de-facto choice for Kotlin developers. Migrating from KAPT to KSP is a straightforward process that can significantly improve your development workflow and code generation quality.

--

--

Amit Patoliya

Mobile Technology Leader | Android Specialist | Technical Team Leader | Catalyst for Innovation & Excellence