android-studioandroid-roomgradle-kotlin-dslkotlin-symbol-processing

Unable to enable ksp for room database when using kotlin DSL build script


Problem: I want to use ksp with room. On adding ksp(libs.androidx.room.compiler) in the module level build.gradle.kts file, I get the following error:

Unable to load class 'org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinCompilationData'
org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinCompilationData

Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)

Re-download dependencies and sync project (requires network)
The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.

Stop Gradle build processes (requires restart)
Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.

In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.


I tried both the mentioned options, Re-download dependencies as well as Stop Gradle build processes. both do not work, and I get the same error.

Below are my files and detailed procedure that I have followed to add ksp.

I created a new project from android studio and used the Kotlin DSL (Recommended) as the build configuration language.

I want to use room database in my project. So, I have added the following in my module level build.gradle.kts file. [Except for the dependencies, and the ksp plugin on the top, the full file is generated by android studio. none of the lines have been modified.]

@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
    alias(libs.plugins.androidApplication)
    alias(libs.plugins.kotlinAndroid)
    id("com.google.gms.google-services")

    // added for ksp
    alias(libs.plugins.kspRoom)
}

android {
    namespace = "com.example.myappname"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.example.myappname"
        minSdk = 21
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    buildFeatures {
        viewBinding = true
    }
}

dependencies {

    implementation(libs.core.ktx)
    implementation(libs.appcompat)
    implementation(libs.material)
    implementation(libs.constraintlayout)
    implementation(libs.navigation.fragment.ktx)
    implementation(libs.navigation.ui.ktx)
    implementation(libs.firebase.auth)
    implementation(libs.firebase.ui.auth)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.test.ext.junit)
    androidTestImplementation(libs.espresso.core)
    implementation(libs.play.services.auth)
    implementation(libs.mqtt.client)

    // added for room
    implementation(libs.androidx.room.runtime)
    annotationProcessor(libs.androidx.room.compiler)
    implementation(libs.androidx.room.ktx)
    implementation(libs.androidx.room.paging)


    // below are the lines that throe error when uncommented.  
    //ksp(libs.androidx.room.compiler)

}

Here is the libs.version.toml file.

[versions]
agp = "8.2.0"
firebase-ui-auth = "8.0.2"
google-services = "4.4.0"
kotlin = "1.8.21"
core-ktx = "1.12.0"
junit = "4.13.2"
androidx-test-ext-junit = "1.1.5"
espresso-core = "3.5.1"
appcompat = "1.6.1"
material = "1.11.0"
constraintlayout = "2.1.4"
mqtt-client = "1.1.4"
navigation-fragment-ktx = "2.7.6"
navigation-ui-ktx = "2.7.6"
play-services-auth = "20.7.0"
room-runtime = "2.6.1"
firebase-auth = "22.3.0"
ksp = "1.8.10-1.0.9"

[libraries]
androidx-room-compiler = { module = "androidx.room:room-compiler", version.ref = "room-runtime" }
androidx-room-ktx = { module = "androidx.room:room-ktx", version.ref = "room-runtime" }
androidx-room-paging = { module = "androidx.room:room-paging", version.ref = "room-runtime" }
androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = "room-runtime" }
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "core-ktx" }
firebase-ui-auth = { module = "com.firebaseui:firebase-ui-auth", version.ref = "firebase-ui-auth" }
google-services = { module = "com.google.gms:google-services", version.ref = "google-services" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-ext-junit" }
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espresso-core" }
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
mqtt-client = { module = "com.ditchoom:mqtt-client", version.ref = "mqtt-client" }
navigation-fragment-ktx = { group = "androidx.navigation", name = "navigation-fragment-ktx", version.ref = "navigation-fragment-ktx" }
navigation-ui-ktx = { group = "androidx.navigation", name = "navigation-ui-ktx", version.ref = "navigation-ui-ktx" }
play-services-auth = { module = "com.google.android.gms:play-services-auth", version.ref = "play-services-auth" }
firebase-auth = { group = "com.google.firebase", name = "firebase-auth", version.ref = "firebase-auth" }

[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kspRoom = {id = "com.google.devtools.ksp", version.ref = "ksp"}

[bundles]

I have added the ksp plugin in the project level/top-level build.gradle.kts as follows:

buildscript {
    dependencies {
        classpath(libs.google.services)
    }
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
    alias(libs.plugins.androidApplication) apply false
    alias(libs.plugins.kotlinAndroid) apply false
    
    // for ksp
    
    // alias method by adding the id in toml file
    alias(libs.plugins.kspRoom) apply false

    // direct method of adding the id here itself
    id("com.google.devtools.ksp") version "1.8.10-1.0.9" apply false
}
true 

Using alias or direct id will not affect the build. Build will succeed if either of them are used.

The build succeeds for the above files if the ksp(libs.androidx.room.compiler) is not used in the module level. If the line is used, build fails with error above. [I haven't added any room db code or models to test of ksp has been successfully incorporated]

Also, here is my Gradle version from the Gradle wrapper: gradle-8.2-bin.zip

There is no documentation whatsoever, wherever by whoever on the internet.


Solution

  • Finally, found the solution. Works like a charm. The errors were due to incorrect versions.

    1. Change JDK version to 18 (Amazon Corretto 18) from Settings -> Build Tools -> Gradle -> Gradle JDK

    2. Change JavaVersion and Kotlin jvmTarget, both to 18 in module build.gradle.kts

      Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
      plugins {
          alias(libs.plugins.androidApplication)
          alias(libs.plugins.kotlinAndroid)
          id("com.google.gms.google-services")
          alias(libs.plugins.kspRoom)
      }
      
      // other sections of the build file
      
      compileOptions {
          sourceCompatibility = JavaVersion.VERSION_18
          targetCompatibility = JavaVersion.VERSION_18
      }
      kotlinOptions {
          jvmTarget = "18"
      }
      dependencies {
          // other dependencies 
          implementation(libs.androidx.room.runtime)
          annotationProcessor(libs.androidx.room.compiler)
          ksp(libs.androidx.room.compiler)
          implementation(libs.androidx.room.paging)
          implementation(libs.androidx.room.ktx)
      
          implementation(libs.androidx.paging.runtime.ktx)
          implementation(libs.androidx.lifecycle.livedata.ktx)
      
      }
      
      
    3. Upgrade Kotlin version to 1.9.21, ksp version to 1.9.10-1.0.13. The libs.versions.toml file:

      [versions]
      // other versions
      kotlin = "1.9.10"
      ksp = "1.9.10-1.0.13"
      
      [plugins]
      //other plugins
      kspRoom = {id = "com.google.devtools.ksp", version.ref = "ksp"}
      
      
    4. The project level buil.gradle.kts will look like this:

      buildscript {
          dependencies {
              classpath(libs.google.services)
          }
      }
      // Top-level build file where you can add configuration options common to all sub-projects/modules.
      @Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
      plugins {
          alias(libs.plugins.androidApplication) apply false
          alias(libs.plugins.kotlinAndroid) apply false
          alias(libs.plugins.kspRoom) apply false
      }
      true // Needed to make the Suppress annotation work for the plugins block