Search code examples
javaandroidkotlinandroid-roomkotlin-coroutines

How to implement corroutines/room correctly? Kotlin Android


have tried to implement coroutines/room in two projects and I get the same error. Please help me!!!

Execution failed for task ':app:kaptDebugKotlin'

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

This is my room database class:

@Database(entities = [Meal::class], version = 1, exportSchema = false)
@TypeConverters(MealTypeConvertor::class)
abstract class MealDataBase : RoomDatabase() {
    abstract fun mealDao(): MealDao

    companion object {
        @Volatile
        var INSTANCE: MealDataBase? = null

        @Synchronized
        fun getInstance(context: Context): MealDataBase {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(
                    context,
                    MealDataBase::class.java,
                    "meal.db"
                ).fallbackToDestructiveMigration()
                    .build()
            }
            return INSTANCE as MealDataBase
        }

    }
}

This my model data Class with the @Entity anotation:

@Entity(tableName = "mealInformation")
data class Meal(
val dateModified: Any?,
@PrimaryKey
val idMeal: String,
val strArea: String?,
val strCategory: String?,
val strCreativeCommonsConfirmed: Any?,
val strDrinkAlternate: Any?,
val strImageSource: Any?,
val strIngredient1: String?
)

My class is already noted with @Entity and my version of gradle is up to date. My dependencies:

dependencies {
def nav_version = "2.3.5"
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
apply plugin: 'kotlin-kapt'

//Navigation Component
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

//intuit
implementation 'com.intuit.sdp:sdp-android:1.0.6'
implementation 'com.intuit.ssp:ssp-android:1.0.6'

//gif
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.17'

//Retrofit
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

//images
implementation 'com.github.bumptech.glide:glide:4.13.2'

//videoModel mvvm
def lifecycle_version = "2.4.0-rc01"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1"

//room
def roomVersion = "2.4.2"
implementation("androidx.room:room-runtime:$roomVersion")
annotationProcessor("androidx.room:room-compiler:$roomVersion")
kapt("androidx.room:room-compiler:$roomVersion")
implementation("androidx.room:room-ktx:$roomVersion")

}

My class is already noted with @Entity and my version of gradle is up to date


Solution

  • The previous solution was only useful for a short time, the real problem was that I had to update gradle. In the classpath I added this:

    > buildscript {
        repositories {
            ...
            ...
        }
        dependencies {
            classpath "com.android.tools.build:gradle:7.0.4"
            classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10'
            classpath "com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin:1.7.20-Beta-1.0.6"
            ...
        }
    
     
    
        }
    

    Be sure that the ksp version match with your kotlin plugin version, you can also check the maven website here: Maven

    Then in the build.gradle add the ksp plugin, add the new ksp compileOption and change the kap annotatons to ksp:

        plugins {
        ...
        ...
        id 'com.google.devtools.ksp'
        }
        android{
        ...
        ksp {
            arg("room.schemaLocation", "$projectDir/schemas".toString())
        }
        ...
        
    }
    dependencies {
        ...
        //room
        def roomVersion = "2.4.3"
    
        implementation("androidx.room:room-runtime:$roomVersion")
        ksp "androidx.room:room-compiler:$roomVersion"
        implementation("androidx.room:room-ktx:$roomVersion")
        ...
    

    }