Introduction to Room Database
DataStore is good for small things but with more complex and larger storage needs, you need a more powerful database. Room is a modern, object-oriented way to work with databases in your Android apps.
Room is built on top of SQLite (Android's built-in database), but it makes everything much simpler and less error-prone. Instead of writing complicated database code, you work with easy-to-understand building blocks.
Main Parts of Room Database
| Part | What It Does | Real-World Analogy |
|---|---|---|
| Entity | Defines what kind of data you want to store (like a Note or User) | Entities are also known as tables in a database |
| DAO | Lists all the ways you can work with your data (add, find, delete, update) | Here is where you list your SQL queries. |
| Database | Brings all your entities and DAOs together in one place | This is what contains all the data |
| Repository | Acts as a middleman between your app and the database | Connects the ViewModel to the database. |
| ViewModel | Manages the data for your app's screens and keeps them up to date | Updates the UI when the data changes. |
How Room Database Works
Room helps your app store and organize information in a way that's safe, fast, and easy to use. Each part has a special job, and together they make sure your data is always where you need it, when you need it.
- Entities are like blueprints for your data. They define what information you want to keep.
- DAOs are the instructions for how to work with your data—like adding, finding, or deleting notes.
- The Database is the main hub that brings everything together.
- The Repository helps keep your code clean and organized by handling all the data operations.
- The ViewModel makes sure your app's screens always show the latest data, even if the device rotates or the app is paused.
Building Example Database
The rest of the lessons will build an example database that will allow use to create, edit, and delete notes. Below the project file
RoomDatabaseDemo/
├── app/
│ ├── src/
│ │ └── main/
│ │ ├── AndroidManifest.xml
│ │ └── java/
│ │ └── com/example/roomdatabasedemo/
│ │ ├── NoteDatabase.kt
│ │ ├── MainActivity.kt
│ │ ├── Note.kt
│ │ ├── NoteDao.kt
│ │ ├── NoteRepository.kt
│ │ ├── NoteViewModel.kt
│ │ ├── NoteScreen.kt
│ │ └── ui/
│ │ └── theme/
│ │ ├── Color.kt
│ │ ├── Theme.kt
│ │ └── Type.kt
│ ├── build.gradle.kts
│ └── ...
├── gradle/
│ └── libs.versions.toml
├── build.gradle.kts
├── settings.gradle.kts
└── ...
The files we will mostly focus, when done upgrading the gradle and .toml files, will be the following:
- NoteDatabase.kt
- Note.kt
- NoteDao.kt
- NoteRepository.kt
- NoteViewModel.kt
- NoteScreen.kt
Dependencies
To use Room and Jetpack Compose, you need to add the right dependencies. Here is the code that was added.
gradle/libs.versions.toml (added)
room = "2.6.1"
[libraries]
androidx-room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "room" }
androidx-room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "room" }
androidx-room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
app/build.gradle.kts (added)
plugins {
id("kotlin-kapt") // For Room annotation processing
}
dependencies {
implementation(libs.androidx.room.runtime)
implementation(libs.androidx.room.ktx)
kapt(libs.androidx.room.compiler)
implementation(libs.androidx.material3)
}
For the rest of the lesson we will walk through buidling each part of this application going backwards starting with buiding the Entity and DAO. I have found that this method works best for understand how all the pieces fit together.