Android Room using Kotlin

Photo by Carlos Muza on Unsplash

Room is a part of the Android Architecture components which provides an abstraction layer over SQlite which allows for a more robust database acces while still providing the full power of SQlite.

Today we are going to discuss the basics of using the Room library and how it can enhance your workflow as a developer.

Room Basics

The Room library consists of 3 major components:

Entity:

The Entity represents a table within the database and has to be annotated with @Entity. Each Entity consist of a minimum of one field has to define a primary key.

DAO (Database Access Object):

In Room you use data access objects to access and manage your data. The DAO is the main component of Room and includes methodes that offer access to your apps database it has to be annotated with @Dao. DAOs are used instead of query builders and let you seperate different components of your database e.g. current data and statistics, which allows you to easily test your database.

Database:

Serves as the database holder an is the main accesspoint to your relational data. It has to be annotated with @Database and extents the RoomDatabase. It also contains and returns the Dao (Database Access Object).

Adding the needed dependencies

First, we need to add the needed dependencies that will enable us to use the Room library. We can do so with some simple lines in the build.gradle (Module:app) file

We also need to create a variable for the room version in our build.gradle (Project) file

Entity

After importing the dependency we can start defining the Entity which represents a table in the database. In this example we just have a simple todo list item with a title and a content field.

Now you should start to relise that Room is based on annotations. Now let’s look on how you can autogenerate a Primarykey and change the Tablename.

DAO

Next, we can start building our DAO which will contain our data queries.

Here we just define basic SQL database functionality like inserting and deleting entries. You can see that the @Query annotation is used to annotate functions which are using queries. You can also use parameters in your queries using :parametername as you can see in the findByTitle function.

You can also make your queries observable using LiveData as you can see in this example.

Here, you are returning a LiveData Object holding a list of TodoEntries which you can observer in your activity.

Database

After that we can start writing the database which contains all your DAOs as abstract methods.

Here we define the version of the database and the entity and DAO that we are using.

You can also use more than one entity and define a custome build and invoke methode as you can see in this example.

Accessing the Database

After defining the database we can get an instance in our activity using the Room.databaseBuilder() methode.

We don’t need the Room.databaseBuilder() to get an instance of the second database example defined above. We just need to call the invoke() methode and pass the activity context.

Now we can start using the database instance to access our DAO object.

Database operations cannot run on the UI thread so we need to create another one. In this example we are using Kotlin coroutines to launch a thread for the database operations.

Testing your database

Now we can start testing our database to make sure that the read and write functionality is functioning correctly.

Here we insert an object into the database and read it and check if the two objects are the same.

Conclusion

Now you should know how the Room library works, how you can insert, update, get and delete entities in the database. You have also learned how to test your database using JUnit.

If you have found this useful, please consider recommending and sharing it with other fellow developers.

If you have any questions or critics, you can reach me in the comment section.

If you want to get continuous updates about my blog make sure to follow me on Medium and join my newsletter.

Originally published at gabrieltanner.org.

Android Developer | Web Developer | Part-time Blogger | Catch me at https://gabrieltanner.org/ | Buy me a coffee buymeacoff.ee/gabrieltanner

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store