Professional Documents
Culture Documents
Getting Started With Room Database in Android - by Amit Raikwar - Medium
Getting Started With Room Database in Android - by Amit Raikwar - Medium
Search
Listen Share
Introduction:
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.
https://amitraikwar.medium.com/getting-started-with-room-database-in-android-fa1ca23ce21e 1/13
31/03/2024, 11:53 Getting Started with Room Database in Android | by Amit Raikwar | Medium
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
}
OR
For latest android and jetpack compose if its failing with some error we might have
to add ksp() {Kotlin symbol processing}.
https://amitraikwar.medium.com/getting-started-with-room-database-in-android-fa1ca23ce21e 2/13
31/03/2024, 11:53 Getting Started with Room Database in Android | by Amit Raikwar | Medium
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")
}
plugins {
id "com.google.devtools.ksp" version "1.8.10-1.0.9" apply false
}
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "users")
data class User(
@PrimaryKey(autoGenerate = true) val id: Long = 0,
val username: String,
val email: String
)
https://amitraikwar.medium.com/getting-started-with-room-database-in-android-fa1ca23ce21e 3/13
31/03/2024, 11:53 Getting Started with Room Database in Android | by Amit Raikwar | Medium
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
@Dao
interface UserDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertUser(user: User)
import androidx.room.Database
import androidx.room.RoomDatabase
import android.app.Application
import androidx.room.Room
companion object {
lateinit var database: AppDatabase
}
Please check the demo project for setting up the injectable room database object.
GitHub - raikwaramit/RoomDatabaseModule
Contribute to raikwaramit/RoomDatabaseModule development by
creating an account on GitHub.
github.com
https://amitraikwar.medium.com/getting-started-with-room-database-in-android-fa1ca23ce21e 5/13
31/03/2024, 11:53 Getting Started with Room Database in Android | by Amit Raikwar | Medium
Conclusion:
Implementing a Room Database in your Android app can significantly simplify your
data storage needs. With its intuitive setup and powerful features, you can
efficiently manage your app’s local data. By following this guide, you’ve learned how
to set up Room, define entities and DAOs, perform database operations, and handle
migrations.
This guide covered the basics of Room. As you become more comfortable with the
library, you can explore its advanced features, such as database relationships,
LiveData integration, and complex queries.
With Room, managing local data in your Android app has never been easier. Happy
coding!
Follow
Software engineer with a passion for exploration. Learning new things and sharing engaging articles. Join me
on a journey of learning, growth, and innovation.
https://amitraikwar.medium.com/getting-started-with-room-database-in-android-fa1ca23ce21e 6/13
31/03/2024, 11:53 Getting Started with Room Database in Android | by Amit Raikwar | Medium
Amit Raikwar
Amit Raikwar
Amit Raikwar
https://amitraikwar.medium.com/getting-started-with-room-database-in-android-fa1ca23ce21e 8/13
31/03/2024, 11:53 Getting Started with Room Database in Android | by Amit Raikwar | Medium
Amit Raikwar
https://amitraikwar.medium.com/getting-started-with-room-database-in-android-fa1ca23ce21e 9/13
31/03/2024, 11:53 Getting Started with Room Database in Android | by Amit Raikwar | Medium
Bharath prakash
52 1
Majidshahbaz
https://amitraikwar.medium.com/getting-started-with-room-database-in-android-fa1ca23ce21e 10/13
31/03/2024, 11:53 Getting Started with Room Database in Android | by Amit Raikwar | Medium
Lists
ChatGPT
21 stories · 548 saves
Staff Picks
609 stories · 872 saves
Chase
https://amitraikwar.medium.com/getting-started-with-room-database-in-android-fa1ca23ce21e 11/13
31/03/2024, 11:53 Getting Started with Room Database in Android | by Amit Raikwar | Medium
16 1
42
https://amitraikwar.medium.com/getting-started-with-room-database-in-android-fa1ca23ce21e 12/13
31/03/2024, 11:53 Getting Started with Room Database in Android | by Amit Raikwar | Medium
2.7K 14
Nimit Raja
50 2
https://amitraikwar.medium.com/getting-started-with-room-database-in-android-fa1ca23ce21e 13/13