byte-sized adventures.

Android Room Database with Kotlin

28 Jul 2023

Implementing the Library and Binding to build.gradle (:app)

android {
    ... //Usual stuff

    buildFeatures{ viewBinding true }
}

dependencies {

    def room_version = "2.5.2" // Verison can be vary depends on the time
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"

    ... //Usual Stuff
}

@Entity(tableName = "my_library")
data class Book(
    val title   : String, 
    val summary : String,
    val year    : Int
){  //4 the sake of simplicity 
    //we only have 3 variables.
    @PrimaryKey(autoGenerate = true)
    val id = 0
}

Code defines a data class Book that represents an entity to be stored in an SQLite database using the Room Persistence Library. The Book class has 4 properties: title, summary, page, and id. The id property is marked as the primary key and will be automatically generated by the database when new entities are inserted. The table in the database where Book entities will be stored is named “my_library.”