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

标签 android kotlin android-room

这个问题在这里已经有了答案:





Trying to create room database with android, but keep getting dependency error

(3 个回答)


9 个月前关闭。




我正在关注 android developers tutorial实现本地房间数据库。
我目前正在处理 设置 因此改变了我的依赖。
但是,一旦我同步了应用程序 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.
如何将其指向 ksp() 方法?
以前我对 kapt() 有同样的问题,但通过添加 id kotlin-kapt 解决了它但我还没有找到 ksp() 所需的依赖项。
我的应用程序 build.gradle 看起来像这样。相应部分位于代码 fragment 的下部,并标有 //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"

}

最佳答案

首先,您需要添加 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
}
}
二、需要开启ksp插件
在您的应用程序 build.gradle
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.devtools.ksp' version "$ksp_version"
}

关于android - 尝试在 Android 库中使用 Room,但找不到方法 ksp()(Kotlin 符号处理),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67535271/

相关文章:

android - 使用 Room 时如何手动调用 CREATE TABLE?

java - 使用 Volley 预填充房间数据库

android - 使用 Kotlin 信号处理 (KSP) 时提供注释处理器参数

java - 打印源文件的当前内容(在 Android 上使用 Java 或 Kotlin)

kotlin - 错误 : cannot find symbol import com. gourav.news.databinding.ActivityDetailBindingImpl;

kotlin - Java8 高阶函数和 Kotlin Lambda 互操作性

ANDROID SQLite SELECT x AS _id

java - 在 Android 上针对多种屏幕分辨率时使用的最佳做法

android - webview 没有在 android 的 fragment 内加载 url

java - 在 Android Studio 中是否可以使用热键通过分配来替换字段的所有出现?