Error while using suspend with DAO methods

Viewed 731

I am getting a compilation error that I have described below. I have tried different solution provided on similar question like mine but neither of them worked for me.

Please help me with this.

The errors I am getting:-

(1)

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);

(2)

error: Not sure how to handle insert method's return type.
public abstract java.lang.Object addTransaction(@org.jetbrains.annotations.NotNull()

(3)

error: Not sure how to handle delete method's return type. Currently the supported 
return types are void, int or Int.
public abstract java.lang.Object deleteTransaction(@org.jetbrains.annotations.NotNull()

(4)

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);

(5)

Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
   > java.lang.reflect.InvocationTargetException (no error message)

This is a compilation error that I am getting while building:-

enter image description here

The DAO class:-

package com.tallileo.myapplication.TransactionDatabase

import androidx.lifecycle.LiveData
import androidx.room.*

 @Dao
 interface TransactionDao {
   @Insert(onConflict = OnConflictStrategy.IGNORE)
   suspend fun addTransaction(transaction:Transaction)

   @Delete
   suspend fun deleteTransaction(transaction:Transaction)

   @Query("SELECT * FROM transactions ORDER BY id DESC")
   fun getAllTransaction():LiveData<List<Transaction>>
}

Entity Class

package com.tallileo.myapplication.TransactionDatabase

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

@Entity(tableName = "transactions")
data class Transaction(
   val transactionType:String,
   val amountType:String,
   val name:String,
   val amount:String,
   val accountTo:String,
   val accountFrom:String,
   val categoryType:String,
   val currency:String,
   var additionalNote:String=""
 ) {
   @PrimaryKey(autoGenerate = true)
   var id:Int=0
 }

Database Class

package com.tallileo.myapplication.TransactionDatabase

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

@Database(entities = [Transaction::class], version = 1, exportSchema = true)
abstract class TransactionDatabase : RoomDatabase() {

    abstract fun transactDao():TransactionDao

    companion object {
        // Singleton prevents multiple instances of database opening at the
        // same time.
        @Volatile
        private var INSTANCE: TransactionDatabase? = null

        fun getDatabase(context: Context): TransactionDatabase {
            // if the INSTANCE is not null, then return it,
            // if it is, then create the database
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    TransactionDatabase::class.java,
                    "TransactionDatabase"
                ).build()
                INSTANCE = instance
                // return instance
                instance
            }
        }
    }
}

My build gradle

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

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.tallileo.myapplication"
        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
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {
    def roomVersion = "2.3.0"

    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'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    //Lottie Animation
    implementation 'com.airbnb.android:lottie:3.4.0'

    //room components
    implementation("androidx.room:room-runtime:$roomVersion")
    kapt("androidx.room:room-compiler:$roomVersion")
    implementation 'androidx.room:room-common:2.3.0'
    implementation("androidx.room:room-ktx:$roomVersion")
}

My kotlin version is 1.6.0. Also when I remove suspend from all the methods in the DAO, then there is no compilation error.

2 Answers

All of your room versions should be the same. You have expressly called out common differently than the rest. Why not let common just come in transitivley?

Had the same problem as well, updating my room_version from '2.3.0' to '2.4.0-beta02' fixed it!

Related