Migration from Kapt to KSP

Saqib
ProAndroidDev
Published in
3 min readNov 13, 2023

--

Kapt is now in maintenance mode so why not migrate to KSP ?

Kapt enables the use of Java annotation processors with Kotlin code. These processors don’t have direct Kotlin support, the whole process is done creating Java stubs which then these processors process.

KSP is Kotlin alternative to Kapt . It has a better understanding of Kotlin language constructs and runs directly with Kotlin. It’s said 2x faster than Kapt .

It is recommended to useKSP and Kapt will be depreciated in future, its better to do migration as early as possible.

Steps to perform Migration from Kapt to KSP .

Check for Libraries for Migration

Wherever you are using Kapt , you need to check if KSP support for that library is available.

Many libraries have added support for KSP likeRoom, Moshi etc and others are adding e.g Hilt.

If you are using Hilt, Hilt support for KSP is still in alpha. You are required to use at least Kotlin 1.9.0 , Dagger 2.48 and KSP version 1.9.0–1.0.12 or above. You can see KSP releases here.

In my project I was using Kotlin 1.8.10 so I had to upgrade to at least Kotlin 1.9.0 , making sure the correct KotlinCompilerExtensionVersion is used against Kotlin 1.9.0 which I set to be 1.5.2 . You can see the mapping between Compose Compiler Version and Compatible Kotlin Version from the link.

Add KSP Plugin

In your project level build.gradle file add KSP plugin.

I am using Version Catalog to manage plugins and dependencies, If you want to migrate to Version Catalog, you can read my other story from the link below.

KSP plugin in Version Catalog.

[versions]
kspPlugin = "1.9.0-1.0.12"

[plugins]
ksp = { id = "com.google.devtools.ksp", version.ref="kspPlugin" }

Adding plugin to the build.gradle .

// root project level build.gradle
plugins {
alias(libs.plugins.ksp) apply false
}

// module level build.gradle
plugins {
alias(libs.plugins.ksp)
}

Replace Kapt with KSP

Migrate modules one by one. In each module’s build.gradle add plugin for KSP

// module level build.gradle
plugins {
alias(libs.plugins.ksp)
}

Then replace kapt with KSP as below.

// dagger hilt
implementation(libs.dagger.hilt)
ksp(libs.dagger.hilt.compiler)

Sync and build your project, if you have errors, do visit the corresponding library documentations.

Remove Kapt Configurations

In the end you would like to remove kapt plugin and related configurations.

E.g the following commented lines will no longer be required.

plugins {
// kotlin("kapt")
}

//// Allow references to generated code
//kapt {
// correctErrorTypes = true
//}

There might be cases where some of the libraries have not supported KSP as yet, but you can keep Kapt and KSP running together until you completely migrate to KSP.

That’s it for now, I hope it was helpful.

Remember to follow and 👏 if you liked it :)

— — — — — — — — — — —

GitHub | LinkedIn | Twitter

--

--

Senior Mobile Engineer (Android & iOS) , Berlin | Sharing my development experience