Convert KAPT to KSP — Room and Hilt Examples

Step-by-Step Guide: Migrating from KAPT to KSP for Room Database and Hilt Dependency Injection

Vincent Tsen

--

KAPT stands for Kotlin Annotation Processing Tool and KSP stands for Kotlin Symbol Processing. Both are annotation-processing tools that are used for code generation.

KAPT is the old way which is Java-based and KSP is the new way which is Kotlin-based, and it builds (generates codes) a lot faster than the KAPT.

The 2 most common Android libraries that use them are Room Database and Hilt Dependency Injection. Let’s explore the steps to convert these libraries from KAPT to KPT…

1. Update Kotlin version to minimum version “1.9.10”

In your project build.gradle file (Groovy example):

/*...*/
plugins {
/*...*/
id 'org.jetbrains.kotlin.android' version '1.9.10' apply false
/*...*/
}
/*...*/

2. Add KSP Plugin to your build.gradle file

In your project build.gradle file (Groovy example):

/*...*/
plugins {
/*...*/
id 'com.google.devtools.ksp' version '1.9.10-1.0.13' apply false
/*...*/
}
/*...*/

In your app build.gradle file (Groovy example), replace KAPT

 /*...*/
plugins {
/*...*/
id 'kotlin-kapt'
/*...*/
}
/*...*/

with KSP plugin.

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

3. Replace KAPT configuraiton with KSP configuration

For example, you have the following kapt configuration in your app build.gradle to specify the room schema location.

/*...*/
android {
/*...*/
defaultConfig {
/*...*/
kapt {
arguments {
arg("room.schemaLocation", "$projectDir/schemas")
}
}
/*...*/
}
/*...*/
}
/*...*/

You convert it to KPT configuration as in the following.

/*...*/
android {
/*...*/
defaultConfig {
/*...*/…

--

--