javaandroidkotlinbuild.gradlekapt

How can I use ksp in my gradle dependency


I'm working on an Android project using Kotlin and the Jetpack Compose framework. I recently tried to integrate KSP (Kotlin Symbol Processing) into my project to use annotation processors like Room. However, I'm encountering an issue where Gradle is unable to resolve the ksp function, resulting in the error:


Unresolved reference: ksp

Here's a snippet of my build.gradle.kts file:

    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
    id("com.google.devtools.ksp") version "1.9.0-1.0.12" apply false
}

//kapt {
//    correctErrorTypes = true
//    useBuildCache = true
//}

android {
    namespace = "com.app.WeatherApp"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.app.WeatherApp"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = true
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = "17"
    }

    buildFeatures {
        compose = true
        viewBinding = true
    }

    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.1"
    }

    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {
    // Core AndroidX libraries
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.activity.compose)

    // Compose UI libraries
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)

    // Testing libraries
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)

    // Retrofit with Gson converter
    implementation(libs.retrofit)
    implementation(libs.converter.gson)

    // OkHttp
    implementation(libs.okhttp)
    implementation(libs.logging.interceptor)

    // Lifecycle components
    implementation(libs.androidx.lifecycle.runtime.kts)
    implementation(libs.androidx.lifecycle.viewmodel.ktx)
    implementation(libs.androidx.lifecycle.livedata.ktx)

    // Glide
    implementation(libs.glide)
    implementation(libs.androidx.room.ktx)
    implementation(libs.androidx.room.runtime)
    ksp(libs.androidx.room.compiler)

    // Gson library
    implementation(libs.gson)

    // Other third-party libraries
    implementation(libs.weatherview)
    implementation(libs.blurview)
}

Solution

  • You have to change the implementation location.

    libs.versions.toml

    [plugins]
    google-devtools-ksp = { id = "com.google.devtools.ksp", version = "1.9.0-1.0.12" }
    

    build.gradle(Project)

    plugins{
        alias(libs.plugins.google.devtools.ksp) apply false
    }
    

    build.gradle(Module:app)

    plugins{
        id ("com.google.devtools.ksp")
    }
    ...
    

    Sync the Gradle.

    I think this will help you resolve the KSP error.