Blog Infos
Author
Published
Topics
Published
Topics

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 HiltHilt 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)
}

OUR VIDEO RECOMMENDATION

, ,

Conquer your Compose Navigation with KSP & KotlinPoet in a Multi-Module Project

Our team encountered a navigation challenge while integrating Jetpack Compose into our multi-module project. Navigation became complex and difficult to manage due to multiple Android components like Activities, Fragments, and Composables.
Watch Video

Conquer your Compose Navigation with KSP & KotlinPoet in a Multi-Module Project

Navid Eghbali
Software Engineer Mobile
PAYBACK GmbH

Conquer your Compose Navigation with KSP & KotlinPoet in a Multi-Module Project

Navid Eghbali
Software Engineer Mo ...
PAYBACK GmbH

Conquer your Compose Navigation with KSP & KotlinPoet in a Multi-Module Project

Navid Eghbali
Software Engineer Mobile
PAYBACK GmbH

Jobs

No results found.

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

This article was previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Kotlin Symbol Processing (KSP) is a new API from Google for creating lightweight Kotlin…
READ MORE
blog
In this article, we are going to create a KSP-based annotation processor that generates…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu