A
A
amimamoya2021-11-24 20:06:02
Kotlin
amimamoya, 2021-11-24 20:06:02

The code using room. What is the problem?

I recently learned about such a wonderful library as room. I decided to use it in my new project and nothing works for me, it gives an error. I've been looking for 10 differences between my code and other people's code for a long time and I can't figure out what the reason is. Here is the code:
ProductDao interface:

@Dao
interface ProductDao {
    @Insert
    fun addProduct(product: Product)

    @Update
    fun updateProduct()

    @Delete
    fun deleteProduct(name: String)

    @Query("SELECT * FROM Vegetables")
    fun getProduct()
}

Vegetables data class:

@Entity
data class Vegetables(val name: String,
                      val about: String,
                      var price: String
                      )

And finally the ProductDatabase:

@Database(entities = arrayOf(Vegetables::class), version = 1, exportSchema = false)
abstract class ProductDatabase : RoomDatabase() {
    abstract fun productDao(): ProductDao

    companion object {
        @Volatile
        private var INSTANCE: ProductDatabase? = null

        fun getDatabase(context: Context): ProductDatabase {
            if (INSTANCE != null) {
                return INSTANCE!!
            }
            synchronized(this) {
                INSTANCE = Room.databaseBuilder(context, ProductDatabase::class.java, "database")
                    .fallbackToDestructiveMigration().build() //Вот тут возникает ошибка
                return INSTANCE!!
            }
        }
    }
}

In the activity file, I just do this:

var db = ProductDatabase.getDatabase(this)
And I get this error: Caused by: java.lang.RuntimeException: cannot find implementation for com.example.shop.ProductDatabase. ProductDatabase_Impl does not exist

There are implementations in build.gradle (Module):

dependencies {
    implementation("[androidx.room](http://androidx.room):room-runtime:2.3.0")
    annotationProcessor("[androidx.room](http://androidx.room):room-compiler:2.3.0")
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2021-11-24
@amimamoya

For Kotlin, you need to use kapt instead of annotationProcessor

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question