Introduction to Android App Development With Kotlin: Room Database (Part 7)
Learn more about Kotling for Android app development and the Google Room database.
Join the DZone community and get the full member experience.
Join For FreeThis is a continuation of tutorial series on Android development with Kotlin. The previous tutorial can be found here.
With the basic structure of our MVVM app in place, we can now look at implementing the persistence layer, i.e. the database. Lucky for us, Google came out with its own persistence library built on top of SQLite; it’s called Room. Previously, there were many independent libraries which were a cause of major headaches for Android developers who maintained multiple projects.
Today, we’ll implement Room into our app and store the items users type in.
Hello, Room
What exactly is Room?
“The Room persistence library provides an abstraction layer over SQLite to allow for more robust database access while harnessing the full power of SQLite.”
And why do we need it?
“The library helps you create a cache of your app's data on a device that's running your app. This cache, which serves as your app's single source of truth, allows users to view a consistent copy of key information within your app, regardless of whether users have an internet connection.”
Let’s do it!
Step 1. Import
In build.gradle (Module: app), under dependencies, add:
// Room
implementation "androidx.room:room-runtime:2.1.0-alpha06"
kapt "androidx.room:room-compiler:2.1.0-alpha06"
Then, enable the kapt
plugin in the same file:
apply plugin: 'kotlin-kapt’
Step 2. Some Theory
Our implementation of the Room database will consist of three major components:
- Database — Contains the database holder and serves as the main access point for the underlying connection to your app's persisted, relational data.
- Entity — Represents a table within the database.
- DAO — Contains the methods used for accessing the database.
Step 3. Create Database
Create a new folder named "data" inside which we’ll store all our data related classes. Create a new Kotlin class named AppDatabase
:
@Database(entities = arrayOf(Movie::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun movieDao(): MovieDao
}
Notice that it is an abstract class and it’s annotated @Database
. It lists entities that will be stored in our database. Entities are equivalent to tables, therefore, listing the Movie
entity tells Room to create a table to store our movies.
Version parameter can be incremented when changes are made to the database in the future updates.
Our code will not compile as it is because we’re still missing Movie
class and MovieDao
class.
Step 4. Create Entity
Create a new Kotlin class named Movie
.
@Entity
data class Movie (
@PrimaryKey(autoGenerate = true) var id: Int? = null,
@ColumnInfo var name: String = ""
)
Notice the annotations. @PrimaryKey defines a special type of column for our table, if you worked with SQLite or MySQL in the past you know that it serves as an index. @ColumnInfo is very simply responsible for defining a column. You can add as many columns as you like to your table but for simplicity’s sake we’ll stick to just one for now.
So far so good.
Step 5. Create DAO
This step will allow us to get our app compiling again, and I promise this is the last file I am asking you to create in this tutorial. So, create a new interface and name it MovieDao
:
@Dao
interface MovieDao {
@Query("SELECT * FROM movie")
fun getAll(): List<Movie>
@Insert
fun insert(item: Movie)
}
Build and run your project to ensure everything is fine. It should compile without any hiccups.
As you can see, the DAO (Data Access Object) as the acronym indicates where we access our database. The above code allows us to fetch the complete list of movies and insert new movies into the database.
Conclusion
We have started our adventure with Room and have set up all the components required for its operation. In the next tutorial, we will instantiate this database and put it to good use.
The complete working code can be found in my GitHub account.
Opinions expressed by DZone contributors are their own.
Comments