

Here is the complete code.įunction names are not important. So, create a new Kotlin class and name it as “Book”.

We will create a table to store details of books. In this small project we are going to have only one table. If you want the primary key to auto generate set autoGenerate = true.Then, use the on the variable selected as the primary key of the table.

But, if we want different names we can provide them using Key.Columns will have same names as the data class’s variable names.And, if we need a different name, we could give that name as the value of the “ tableName” property.Database table will have the same name as the class name.So, we will create them as Kotlin data classes. Holding data is the only purpose of these classes.Moreover, to qualify a class as a room entity class, we need to annotate it with.We need to create entity classes for each database table.Entity classes, Dao interface and the database class. To use Room library, you need 3 code components. Implementation 'nstraintlayout:constraintlayout:2.0.4'ĪndroidTestImplementation ':junit:1.1.2'ĪndroidTestImplementation ':espresso-core:3.3.0' Implementation 'androidx.appcompat:appcompat:1.2.0' Implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version" Lifecycles only (without ViewModel or LiveData) Implementation ':kotlinx-coroutines-android:1.4.2' Implementation ':kotlinx-coroutines-core:1.4.2' Implementation ":kotlin-stdlib:$kotlin_version" Implementation "androidx.room:room-ktx:$room_version" optional - Kotlin Extensions and Coroutines support for Room Kapt "androidx.room:room-compiler:$room_version" Implementation "androidx.room:room-runtime:$room_version" TargetCompatibility JavaVersion.VERSION_1_8 SourceCompatibility JavaVersion.VERSION_1_8 ProguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
