Room error: Type of the parameter must be a class annotated with @Entity or a collection/array of it. even after adding androidx.room:room-ktx

Viewed 556

According to what i understood its due to "suspend" keyword. But I have already added implementation("androidx.room:room-ktx:$roomVersion") dependency

Gradle file is:

 plugins {
        id 'com.android.application'
        id 'kotlin-android'
        id 'kotlin-kapt'
    }
    
    android {
        compileSdk 31

    defaultConfig {
        applicationId "com.kaushik.roomdemo"
        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'
        }
    }
    buildFeatures{
        viewBinding true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    def lifecycle_version = "2.4.0"
    def roomVersion = "2.3.0"

    // ViewModel
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")

    // LiveData
    implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version")

    // Annotation processor
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

    // Room
    implementation("androidx.room:room-runtime:$roomVersion")
    kapt("androidx.room:room-compiler:$roomVersion")

    // Kotlin Extensions and Coroutines support for Room
    implementation("androidx.room:room-ktx:$roomVersion")

    // Coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0-RC'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0-RC'

    // Activity KTX for viewModels()
    implementation "androidx.activity:activity-ktx:1.4.0"
}

Entity is:

@Entity(tableName = "subscriber_table")
data class Subscriber(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name="subscriber_id")
    val id:Int,
    @ColumnInfo(name="subscriber_name")
    val name:String,
    @ColumnInfo(name="subscriber_email")
    val email:String
)

Dao is :

@Dao
interface SubscriberDao {
    @Query("SELECT * from subscriber_table")
    fun getAllSubscriber(): LiveData<List<Subscriber>>

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun addSubscriber(subscriber: Subscriber):Long

    @Delete
    suspend fun deleteSubscriber(subscriber: Subscriber)
}

DataBase is:

@Database(entities = [Subscriber::class], version = 1)
abstract class SubscriberDataBase:RoomDatabase() {

    abstract fun subscriberDao(): SubscriberDao

    companion object{
        @Volatile
        private var INSTANCE: SubscriberDataBase? = null
        fun getInstance(context: Context):SubscriberDataBase{
            synchronized(this){
                var instance:SubscriberDataBase? = INSTANCE
                if(instance == null) {
                    instance = Room.databaseBuilder(
                        context.applicationContext,
                        SubscriberDataBase::class.java,
                        "Subscriber_DataBase"
                    ).build()
                }
                return instance
            }
        }
    }
}

now every time I tried to build the code it throws the following error: D:\CODE IT\Android Developmeent\RoomDemo\app\build\tmp\kapt3\stubs\debug\com\kaushik\roomdemo\db\SubscriberDao.java:17: error: Type of the parameter must be a class annotated with @Entity or a collection/array of it. kotlin.coroutines.Continuation<? super java.lang.Long> continuation);

I have already added implementation("androidx.room:room-ktx:$roomVersion") in dependency file. Still not working. Anyone, please help!!!!!!!!!!!!!!!!!!!!!!!!!!

4 Answers

Your Kotlin version must be the same as Kotlin version that Room library uses.

For example for Kotlin version 1.6.10 and Room version 2.2.3, I got the same error. After upgrading Room to version 2.4.1,The error was solved.

When Kotlin releases a new version, code generation might have changed so it could affect room library.

Your code looks ok, you probably haven't defined your entity in your RoomDatabase() class.

@Database(
    entities = [Subscriber::class],
    version = 1,
    exportSchema = false
)
abstract class AppDatabase : RoomDatabase() {
   //...
}

try this..maybe it will help you

@Dao
interface SubscriberDao {
    @Query("SELECT * from subscriber_table")
    fun getAllSubscriber(): LiveData<List<Subscriber>>

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun addSubscriber(subscriber: Subscriber):Long

    @Delete
    fun deleteSubscriber(subscriber: Subscriber)
}

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

android {
    compileSdk 31

defaultConfig {
    applicationId "com.kaushik.roomdemo"
    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'
    }
}
buildFeatures{
    viewBinding true
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8'
}

}

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

def lifecycle_version = "2.4.0"
def roomVersion = "2.3.0" //update this to '2.4.2'

// ViewModel
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")

// LiveData
implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version")

// Annotation processor
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

// Room
implementation("androidx.room:room-runtime:$roomVersion")
kapt("androidx.room:room-compiler:$roomVersion")

// Kotlin Extensions and Coroutines support for Room
implementation("androidx.room:room-ktx:$roomVersion")

// Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0-RC'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0-RC'

// Activity KTX for viewModels()
implementation "androidx.activity:activity-ktx:1.4.0"

}

Related