DEV Community

Cover image for Convert KAPT to KSP - Room and Hilt Examples
Vincent Tsen
Vincent Tsen

Posted on • Originally published at vtsen.hashnode.dev

Convert KAPT to KSP - Room and Hilt Examples

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

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
    /*...*/
}
/*...*/
Enter fullscreen mode Exit fullscreen mode

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
    /*...*/
}
/*...*/
Enter fullscreen mode Exit fullscreen mode

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

 /*...*/
plugins {
    /*...*/
    id 'kotlin-kapt'
    /*...*/
}
/*...*/
Enter fullscreen mode Exit fullscreen mode

with KSP plugin.

/*...*/
plugins {
    /*...*/
    id 'com.google.devtools.ksp'
    /*...*/
}
/*...*/
Enter fullscreen mode Exit fullscreen mode

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")
            }
        }
        /*...*/
    }
    /*...*/
}
/*...*/
Enter fullscreen mode Exit fullscreen mode

You convert it to KPT configuration as in the following.

/*...*/
android {
    /*...*/
    defaultConfig {
        /*...*/     
        ksp {
            arg("room.schemaLocation", "$projectDir/schemas")
        }
        /*...*/
    }
    /*...*/
}
/*...*/
Enter fullscreen mode Exit fullscreen mode

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'
    }
    /*...*/
}
/*...*/
Enter fullscreen mode Exit fullscreen mode

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"
}
/*...*/
Enter fullscreen mode Exit fullscreen mode

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"
}
/*...*/
Enter fullscreen mode Exit fullscreen mode

Source Code

GitHub Repository: AndroidNews


Originally published at https://vtsen.hashnode.dev.

Top comments (0)