陷入未解决的参考:Android Gradle 中的 ksp 错误

问题描述 投票:0回答:1

我已经开始构建一个简单的 Android 应用程序。但我遇到了 未解决的参考:ksp 错误。我正在管理 libs.versions.toml 中的所有插件版本。以下是 gradle 文件和 libs 文件。谁能帮我看看我的代码有什么问题吗?

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" } 

设置.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(项目)

// 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(模块: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)
}

帮我解决这个问题

android kotlin gradle
1个回答
0
投票

您已经在模块中应用了 KSP 插件,但需要先将其添加到顶级 build.gradle.kts 中才能应用:

alias(libs.plugins.kotlin.ksp) apply false

也就是说,当尝试使用你的 gradle 设置时,我还必须调整以下内容才能使其运行:

  1. 从 settings.gradle.kts 中删除以下内容。 Gradle 将自动搜索该位置的版本目录:

    versionCatalogs {
        create("libs") {
            from(files("gradle/libs.versions.toml"))
        }
    }
    
  2. 在版本目录版本中,必须将其删除为字符串,而不是数字:

    minSdk = "24"
    compileSdk = "34"
    targetSdk = "34"
    
  3. 从模块级别 build.gradle.kts 中删除 Safe Args 插件:

    id("androidx.navigation.safeargs.kotlin") // Add Safe Args plugin if using 
    

    不需要它,因为您一开始就没有使用导航库。如果您想稍后添加,请参阅此处如何正确设置:https://developer.android.com/guide/navigation/use-graph/safe-args#kts

© www.soinside.com 2019 - 2024. All rights reserved.