Local data storage is crucial for many Android applications, allowing them to store and retrieve data efficiently. In this guide, we will explore Room, a powerful library that simplifies database management in Android apps. We’ll cover everything from setting up Room to performing database operations and handling migrations.

Structure of using the Room data base in android MVVM project
Section 1: Setting Up Room Database
Step 1: Add Dependencies
Open your app’s `build.gradle` module level file and add the necessary dependencies for Room and Kotlin Coroutines (for asynchronous operations):
gradle
dependencies {
def roomVersion = "2.4.0" // Check for the latest version
implementation "androidx.room:room-runtime:$roomVersion"
kapt "androidx.room:room-compiler:$roomVersion"
implementation "androidx.room:room-ktx:$roomVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2" // Add Coroutine dependency
}
OR
For latest android and jetpack compose if its failing with some error we might have to add ksp() {Kotlin symbol processing}.
Add below dependencies and plugin in build.gradle(Module level).
plugins {
.
.
id "com.google.devtools.ksp"
}
.
.
.
dependencies{
// Room dependency
val room_version = "2.5.2"
implementation("androidx.room:room-ktx:$room_version")
// To use Kotlin annotation processing tool (kapt)
ksp("androidx.room:room-compiler:$room_version")
}
Add below class path for the ksp in build.gradle(app level).