无法在 RoomDB Dao 中使用挂起功能

N H*_*LAH 9 android kotlin android-studio android-room kotlin-coroutines

RoomDB 和 Kotlin 协程发生了什么?我一次又一次地尝试在 Room Dao 中使用挂起功能,但每次都会显示错误。我什至遵循 android codelabs 示例。但它又显示错误。如果我在 Dao 中编写暂停,应用程序甚至不会构建。但如果我删除 suspend 关键字,它就会成功构建。

它显示以下错误:

error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
kotlin.coroutines.Continuation<? super kotlin.Unit> continuation);
Run Code Online (Sandbox Code Playgroud)

我的实体:

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "vocabulary")
class Vocabulary(
    @PrimaryKey(autoGenerate = true)
    val vocabularyId: Long,
    val word: String,
    val meaning: String,
    val definition: String,
    val example: String,
    val partsOfSpeech: String,
    val synonyms: String,
    val antonyms: String,
    val phonetics: String,
    val folderId: Long,
    val isLearned: Int
)
Run Code Online (Sandbox Code Playgroud)

我的道:

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy


@Dao
interface VocabuilderDao {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insertVocab(vocabulary: Vocabulary)
}
Run Code Online (Sandbox Code Playgroud)

我的数据库类:

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase


@Database(entities = [Vocabulary::class], version = 1, exportSchema = false)
public abstract class VocabuilderRoomDb : RoomDatabase(){

    abstract fun vocabuilderDao(): VocabuilderDao

    companion object{

        @Volatile
        private var INSTANCE: VocabuilderRoomDb? = null

        fun getRoomDatabase(context: Context): VocabuilderRoomDb{
            return INSTANCE ?: synchronized(this){
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    VocabuilderRoomDb::class.java,
                    "vocabuilder_roomdb"
                ).build()
                INSTANCE = instance
                instance
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的 build.gradle (项目级别):

    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    buildscript {
        ext.kotlin_version = '1.6.0'
    
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            classpath "com.android.tools.build:gradle:7.0.2"
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0"
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    ext {
        activityVersion = '1.2.3'
        appCompatVersion = '1.3.0'
        constraintLayoutVersion = '2.0.4'
        coreTestingVersion = '2.1.0'
        coroutines = '1.5.0'
        lifecycleVersion = '2.3.1'
        materialVersion = '1.3.0'
        roomVersion = '2.3.0'
        // testing
        junitVersion = '4.13.2'
        espressoVersion = '3.1.0'
        androidxJunitVersion = '1.1.2'
    }
    
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }

My build.gradle(module):

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'

}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.domesoft.vocabuilder"
        minSdk 21
        targetSdk 31
        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
    }

    packagingOptions {
        exclude 'META-INF/atomicfu.kotlin_module'
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
    implementation "androidx.activity:activity-ktx:$rootProject.activityVersion"

    // Dependencies for working with Architecture components
    // You'll probably have to update the version numbers in build.gradle (Project)

    // Room components
    implementation "androidx.room:room-ktx:$rootProject.roomVersion"
    kapt "androidx.room:room-compiler:$rootProject.roomVersion"
    androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

    // Lifecycle components
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$rootProject.lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-common-java8:$rootProject.lifecycleVersion"

    // Kotlin components
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines"

    // UI
    implementation "androidx.constraintlayout:constraintlayout:$rootProject.constraintLayoutVersion"
    implementation "com.google.android.material:material:$rootProject.materialVersion"

    // Testing
    testImplementation "junit:junit:$rootProject.junitVersion"
    androidTestImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
    androidTestImplementation ("androidx.test.espresso:espresso-core:$rootProject.espressoVersion", {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    androidTestImplementation "androidx.test.ext:junit:$rootProject.androidxJunitVersion"
}
Run Code Online (Sandbox Code Playgroud)

Kha*_*din 10

如果您使用 kotlin 版本 (1.7.0),则应使用 room 最新的 alpha 版本 (2.5.0-alpha02)

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0"

implementation "androidx.room:room-runtime:2.5.0-alpha02"
implementation "androidx.room:room-ktx:2.5.0-alpha02"
kapt "androidx.room:room-compiler:2.5.0-alpha02"
Run Code Online (Sandbox Code Playgroud)

如果你想在稳定版本(2.4.2)中使用 room 应该与 kotlin 版本(1.6.20)一起使用

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.20"

implementation "androidx.room:room-runtime:2.4.2"
implementation "androidx.room:room-ktx:2.4.2"
kapt "androidx.room:room-compiler:2.4.2"
Run Code Online (Sandbox Code Playgroud)

我都尝试过并且它们都有效。这是参考:问题跟踪器


Ste*_*one 1

将Room版本更新为2.4.0-rc01.
就我而言,它解决了问题,并且适用于 Kotlin 1.6.0。