尝试在 Android 库中使用 Room,但找不到方法 ksp()(Kotlin 符号处理)

Mag*_*nns 22 android kotlin android-room

我正在按照android 开发人员教程来实现本地房间数据库。

我目前正在进行安装,因此更改了我的依赖项。但是一旦我同步应用程序 build.gradle 它就会抛出错误。

A problem occurred evaluating project ':app'.
> Could not find method ksp() for arguments [androidx.room:room-compiler:2.3.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Run Code Online (Sandbox Code Playgroud)

如何将其指向 ksp() 方法?

以前我对 kapt() 遇到了同样的问题,但通过添加解决了它id kotlin-kapt,但我还没有找到 ksp() 所需的依赖项。

我的应用程序 build.gradle 看起来像这样。相应的部分位于代码片段的下部并标记为//rooom <---------

    plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
    //Room
    id 'kotlin-kapt'

}



android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.testapplication"
        minSdkVersion 24
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled 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'
    }
}



dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.cardview:cardview:1.0.0' //for the popup window

    //Room <------------------------------
    def room_version = "2.3.0"

    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.legacy:legacy-support-v4:1.0.0"
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'

    // To use Kotlin annotation processing tool (kapt)
    kapt "androidx.room:room-compiler:$room_version"
    // To use Kotlin Symbolic Processing (KSP)
    ksp "androidx.room:room-compiler:$room_version"

    // optional - Kotlin Extensions and Coroutines support for Room
    implementation "androidx.room:room-ktx:$room_version"

    // optional - Test helpers
    testImplementation "androidx.room:room-testing:$room_version"

}
Run Code Online (Sandbox Code Playgroud)

Isl*_*sem 30

首先,你需要添加 ksp 插件 repo gradlePluginPortal 在你的项目 build.gradle 中

buildscript {
ext.ksp_version = "1.5.31-1.0.0"
ext.kotlin_version = "1.5.31"
repositories {
    maven{ url 'https://dl.bintray.com/kotlin/kotlin-eap'}
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    mavenCentral()
    jcenter() // Warning: this repository is going to shut down soon
    gradlePluginPortal()
    google()

}
dependencies {
    classpath "com.android.tools.build:gradle:7.0.2"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    //other dependencies here
}
Run Code Online (Sandbox Code Playgroud)

}

其次,您需要在应用程序 build.gradle 中启用 ksp 插件

plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.devtools.ksp' version "$ksp_version"
}
Run Code Online (Sandbox Code Playgroud)

  • 我在尝试使用 KSP 时被困了好几个小时。这个答案为我解决了这个问题。特别是行 `id 'com.google.devtools.ksp' version "$ksp_version"`。Google 自己的文档不会告诉您包含 ksp 版本。 (9认同)

小智 11

首先,您只需选择其中之一:

  // To use Kotlin annotation processing tool (kapt)
kapt "androidx.room:room-compiler:$room_version"
// To use Kotlin Symbolic Processing (KSP)
ksp "androidx.room:room-compiler:$room_version"
Run Code Online (Sandbox Code Playgroud)

选择它后,添加插件。即 id 'kotlin-kapt'


Mik*_*keT 2

我相信您需要遵循快速入门指南,例如依赖项是以下行:-

implementation("com.google.devtools.ksp:symbol-processing-api:1.5.0-1.0.0-alpha10")
Run Code Online (Sandbox Code Playgroud)

请参阅https://github.com/google/ksp/blob/main/docs/quickstart.md

您可能还希望考虑查看: