Convert KAPT to KSP - Room and Hilt Examples

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

ยท

2 min read

Convert KAPT to KSP - Room and Hilt Examples

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 {
        /*...*/        
        ksp {
            arg("room.schemaLocation", "$projectDir/schemas")
        }
        /*...*/
    }
    /*...*/
}
/*...*/

4. Upgrade to Minimum Compose Compiler Version

Since the Kotlin version has been upgraded to version 1.9.10, it requires upgrading to the minimum compose compiler version of 1.5.3.

/*...*/
android {
    /*...*/
    composeOptions {
        kotlinCompilerExtensionVersion '1.5.3'
    }
    /*...*/
}
/*...*/

5. Replace annotation processor with KSP

Replace KAPT

/*...*/
dependencies {
    /* Room Example*/
    kapt "androidx.room:room-compiler:2.5.2"
    /* Hilt Example*/
    kapt "com.google.dagger:hilt-android-compiler:2.48"
    kaptAndroidTest "com.google.dagger:hilt-android-compiler:$hilt_version"
}
/*...*/

with KSP

/*...*/
dependencies {
    /* Room Example*/
    ksp "androidx.room:room-compiler:2.5.2"
    /* Hilt Example*/
    ksp "com.google.dagger:hilt-android-compiler:2.48"
    kspAndroidTest "com.google.dagger:hilt-android-compiler:$hilt_version"
}
/*...*/

Source Code

GitHub Repository: AndroidNews

Did you find this article valuable?

Support Vincent Tsen by becoming a sponsor. Any amount is appreciated!

ย