I'm trying to use Room on my Android project that I'm using with the Gradle Version Catalogs feature. Room is officialy suported since version 2.3.0-beta02, but I'm having a problem to make them work together.
What I have now configured is something like this:
libs.versions.toml
[versions]
agp = "8.1.2"
// ...
androidx-room = "2.3.0-beta02"
com-google-devtools-ksp = "1.8.10-1.0.9"
[libraries]
# Implementations
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "core-ktx" }
// ...
androidx-room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "androidx.room" }
androidx-room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "androidx.room" }
androidx-room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "androidx.room" }
[plugins]
com-android-application = { id = "com.android.application", version.ref = "agp" }
// ...
com-google-devtools-ksp = { id = "com.google.devtools.ksp", version.ref = "com-google-devtools-ksp" }
[bundles]
build.gradle.kts (:app)
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
alias(libs.plugins.com.android.application)
// ..
alias(libs.plugins.com.google.devtools.ksp)
}
android {
// ...
defaultConfig {
// ...
minSdk = 24
// noinspection EditedTargetSdkVersion
targetSdk = 34
versionCode = 1
versionName = "1.0.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.androidx.room.ktx)
implementation(libs.androidx.room.runtime)
ksp(libs.androidx.room.compiler)
// Test
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.espresso.core)
// Test Android
testImplementation(libs.junit)
testImplementation(libs.io.insert.koin.test)
}
build.gradle.kts (Top-level build file)
@Suppress("DSL_SCOPE_VIOLATION")
plugins {
alias(libs.plugins.com.android.application) apply false
/// ...
alias(libs.plugins.org.jetbrains.kotlin.plugin.parcelize) apply false
}
true // Needed to make the Suppress annotation work for the plugins block
The error I'm getting is this one:
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':app'.
> class org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation_Decorated cannot be cast to class org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinCompilationData (org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation_Decorated is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @6404c62a; org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinCompilationData is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @3df7681e)
(...)
* Exception is:
org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':app'.
(...)
org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:49)
Caused by: java.lang.ClassCastException: class org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation_Decorated cannot be cast to class org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinCompilationData (org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation_Decorated is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @6404c62a; org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinCompilationData is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @3df7681e)
(...)
class org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation_Decorated cannot be cast to class org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinCompilationData (org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation_Decorated is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @6404c62a; org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinCompilationData is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @3df7681e)
class org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation_Decorated cannot be cast to class org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinCompilationData (org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation_Decorated is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @6404c62a; org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinCompilationData is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @3df7681e)
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.
Try this
libs.versions.toml
[versions]
ksp = "2.0.21-1.0.27"
room = "2.6.1"
[libraries]
room-runtime = { module = "androidx.room:room-runtime", version.ref = "room" }
room-compiler = { module = "androidx.room:room-compiler", version.ref = "room"}
room-ktx = { module = "androidx.room:room-ktx", version.ref = "room" }
[plugins]
google-devtools-ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
build.gradle.kts (:app)
plugins {
alias(libs.plugins.google.devtools.ksp)
}
dependencies {
implementation(libs.room.runtime)
implementation(libs.room.ktx)
ksp(libs.room.compiler)
}
build.gradle.kts (Top-level build file)
plugins {
alias(libs.plugins.google.devtools.ksp) apply false
}
Now, sync your Gradle files and rebuild the project. Your Kotlin KSP with Room should work correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With