I have started building a simple android app. But I am stuck with Unresolved reference: ksp error. I am managing all plugin versions in libs.versions.toml. Below are gradle files and libs file. Can anyone help me what's wrong with my code?
libs.versions.toml
[versions]
agp = "8.1.0"
kotlin = "1.9.0"
core-ktx = "1.10.1"
appcompat = "1.6.1"
material = "1.9.0"
constraintlayout = "2.1.4"
coroutines = "1.7.3"
lifecycle = "2.6.1"
room = "2.5.2"
recyclerview = "1.3.1"
datastore-preferences = "1.0.0"
# Testing libraries
junit = "4.13.2"
androidx-junit = "1.1.5"
espresso-core = "3.5.1"
# SDK versions
minSdk = 24
compileSdk = 34
targetSdk = 34
ksp = "1.9.0-1.0.12" # Add KSP version
[libraries]
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "core-ktx" }
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" }
coroutines-android = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-android", version.ref = "coroutines" }
lifecycle-viewmodel-ktx = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-ktx", version.ref = "lifecycle" }
lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycle" }
room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "room" }
room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "room" }
room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" }
recyclerview = { group = "androidx.recyclerview", name = "recyclerview", version.ref = "recyclerview" }
datastore-preferences = { group = "androidx.datastore", name = "datastore-preferences", version.ref = "datastore-preferences" }
# Testing libraries
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-junit" }
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espresso-core" }
[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
androidLibrary = { id = "com.android.library", version.ref = "agp" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
Settings.gradle.kts
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
versionCatalogs {
create("libs") {
from(files("gradle/libs.versions.toml"))
}
}
}
rootProject.name = "Naturo"
include(":app")
build.gradle.kts (Project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
alias(libs.plugins.androidApplication) apply false // Apply Android Application plugin
alias(libs.plugins.androidLibrary) apply false // Apply Android Library plugin
alias(libs.plugins.kotlinAndroid) apply false // Apply Kotlin Android plugin
}
build.gradle.kts (Module :app)
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("androidx.navigation.safeargs.kotlin") // Add Safe Args plugin if using Navigation
id("com.google.devtools.ksp") // Use KSP for Room annotation processing
}
android {
namespace = "com.minimalapps.naturo"
compileSdk = libs.versions.compileSdk.get().toInt()
defaultConfig {
applicationId = "com.minimalapps.naturo"
minSdk = libs.versions.minSdk.get().toInt()
targetSdk = libs.versions.targetSdk.get().toInt()
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = true
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.recyclerview)
implementation(libs.lifecycle.viewmodel.ktx)
implementation(libs.lifecycle.runtime.ktx)
implementation(libs.coroutines.android)
implementation(libs.room.runtime)
implementation(libs.room.ktx)
ksp(libs.room.compiler) // Use ksp instead of kapt
implementation(libs.datastore.preferences)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.espresso.core)
}
Help me fix this
You already apply the KSP plugin in your module, but you need to add it to your top-level build.gradle.kts first before it can be applied:
alias(libs.plugins.kotlin.ksp) apply false
As always you need to perform a gradle sync after changing the gradle setup before you can use it.
That said, when trying to use your gradle setup I also had to adjust the following to get it running:
Remove the following from your settings.gradle.kts. Gradle will automatically search for the version catalog at that location:
versionCatalogs {
create("libs") {
from(files("gradle/libs.versions.toml"))
}
}
In the version catalog versions must be delcare as String, not as a number:
minSdk = "24"
compileSdk = "34"
targetSdk = "34"
Remove the Safe Args plugin from the module level build.gradle.kts:
id("androidx.navigation.safeargs.kotlin") // Add Safe Args plugin if using
It isn't needed since you do not use the navigation library in the first place. If you want to add it at a later time please see here how to properly set it up: https://developer.android.com/guide/navigation/use-graph/safe-args#kts