Search code examples
androidkotlinandroid-roomkotlin-coroutinesdao

Dao errors in every project with Room


This app is taken from an android course from their site. Once I re-download the same exact code the errors are not present. The code is the same but one has errors. [Errors are shown in the image][1] [1]: https://i.sstatic.net/zkk8e.png

Dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import kotlinx.coroutines.flow.Flow

/**
 * Database access object to access the Inventory database
 */
@Dao
interface ItemDao {

    @Query("SELECT * from item ORDER BY name ASC")
    fun getItems(): Flow<List<Item>>

    @Query("SELECT * from item WHERE id = :id")
    fun getItem(id: Int): Flow<Item>

    // Specify the conflict strategy as IGNORE, when the user tries to add an
    // existing Item into the database Room ignores the conflict.
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insert(item: Item)

    @Update
    suspend fun update(item: Item)

    @Delete
    suspend fun delete(item: Item)
}

Entity

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import java.text.NumberFormat

/**
 * Entity data class represents a single row in the database.
 */
@Entity
data class Item(
    @PrimaryKey(autoGenerate = true)
    val id: Int = 0,
    @ColumnInfo(name = "name")
    val itemName: String,
    @ColumnInfo(name = "price")
    val itemPrice: Double,
    @ColumnInfo(name = "quantity")
    val quantityInStock: Int,
)

Most android applications generate errors like these without even touching the code. Why is that?


Solution

  • Use KSP instead of KAPT

    KAPT and KSP are both tools that allow you to use annotation processors with Kotlin code. Annotation processors are used to add additional functionality to your code at compile time. For example, you can use an annotation processor to generate code, check for errors, or add annotations to your code.


    KAPT is the older of the two tools and is based on the Java annotation processing API. This means that KAPT can only be used with annotation processors that are written for Java. KSP is a newer tool that is based on the Kotlin compiler API. This means that KSP can be used with annotation processors that are written for Kotlin or Java.

    KSP has several advantages over KAPT, including:

    Faster performance: KSP analyzes Kotlin code directly, which is up to 2x faster than KAPT. Better Kotlin support: KSP has a better understanding of Kotlin's language constructs, which can lead to more accurate results. More flexibility: KSP is more flexible than KAPT and can be used to do a wider range of tasks. If you are using Kotlin, I recommend using KSP instead of KAPT. KSP is the newer, faster, and more flexible tool.

    Here is a table that summarizes the key differences between KAPT and KSP:

    Feature KAPT KSP
    Language support Java Kotlin
    Performance Slower Faster
    Flexibility Less flexible More flexible
    Documentation More mature Less mature

    To apply KSP:

    Add this line to your app build gradle file's plugins block.

    plugins {
        id 'com.google.devtools.ksp' version '1.8.10-1.0.9'
    }
    

    *the KSP version should match your kotlin version.

    And, change the way of applying your room compiler dependency.

    dependencies {
        // kapt 'androidx.room:room-compiler:2.5.0' // remove this line.
        ksp 'androidx.room:room-compiler:2.5.0'     // and add this.
    }