Getting Started with Room Database in Android
<p>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.</p>
<p><img alt="" src="https://miro.medium.com/v2/resize:fit:662/1*eCptOwb2cImXY-ueplhNIg.png" style="height:385px; width:662px" /></p>
<p>Structure of using the Room data base in android MVVM project</p>
<h2>Section 1: Setting Up Room Database</h2>
<p><strong>Step 1: Add Dependencies</strong><br />
Open your app’s `<strong>build.gradle</strong>` module level file and add the necessary dependencies for Room and Kotlin Coroutines (for asynchronous operations):</p>
<pre>
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
}</pre>
<p>OR</p>
<p>For latest android and jetpack compose if its failing with some error we might have to add ksp() {Kotlin symbol processing}.</p>
<p>Add below dependencies and plugin in build.gradle(Module level).</p>
<pre>
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")
}</pre>
<p>Add below class path for the ksp in build.gradle(app level).</p>
<p><a href="https://amitraikwar.medium.com/getting-started-with-room-database-in-android-fa1ca23ce21e">Read More</a></p>