Reader small image

You're reading from  Modern Android 13 Development Cookbook

Product typeBook
Published inJul 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781803235578
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Madona S. Wambua
Madona S. Wambua
author image
Madona S. Wambua

Madona S. Wambua is a Google Developer Expert in the Android category, an Android Engineer programming in Kotlin, and the founder and Chief Technology Officer of Jibu Labs. She is also a Women Tech Makers Ambassador and Co-Chair of AnitaB. She has over ten years of experience in the field and has worked on consumer-facing applications and building software development kits for developers. She has also worked on the famous Google Glass during her tenure at a startup and got an opportunity to work on interactive AR videos to transform lives through machine learning and computer vision. Madona is also a Keynote Speaker and the host of Tech Talks with Madona, a podcast geared towards supporting women and allies in tech.
Read more about Madona S. Wambua

Right arrow

Using the Room Database and Testing

Android applications can benefit significantly from storing data locally. The Room persistence library harnesses the power of SQLite. In particular, Room offers excellent benefits for Android developers. Furthermore, Room offers offline support, and the data is stored locally. In this chapter, we will learn how to implement Room, a Jetpack Library.

In this chapter, we’ll be covering the following recipes:

  • Implementing Room in your applications
  • Implementing Dependency Injection in Room
  • Supporting multiple entities
  • Migrating existing SQL database to Room
  • Testing your local database

It is also important to mention there are a couple more libraries are used with Room – for example, RxJava and Paging integration. In this chapter, we will not focus on them but instead on how you can utilize Room to build modern Android apps.

Technical requirements

Implementing Room in your applications

Room is an object-relational mapping library used in Android data persistence and is the recommended data persistence in Modern Android Development. In addition, it is effortless to use, understand and maintain, and harnesses the powers of SQLiteDatabase, it also helps reduce boilerplate code, an issue many developers experience when using SQLite. Writing tests is also very straightforward and easy to understand.

The most notable advantage of Room is that it is easy to integrate with other architecture components and gives developers runtime compile checks – that is, Room will complain if you make an error or change your schema without migrating, which is practical and helps reduce crashes.

How to do it…

Let’s go ahead and create a new empty compose project and call it RoomExample. In our example project, we will create a form intake from users; this is where users can save their first and last names, date of birth...

Implementing Dependency Injection in Room

As with other recipes, Dependency Injection is vital, and in this recipe, we will walk through how we can inject our DatabaseModule and provide the Room database where it is needed.

Getting ready

You will need to have prior knowledge of how Hilt works to be able to follow this recipe step by step.

How to do it…

Open the RoomExample project and add Hilt, which is what we will use for Dependency Injection. In Chapter 3, Handling the UI State in Jetpack Compose and Using Hilt, we covered Hilt, so we will not discuss it here but just show you how you can use it with Room:

  1. Open your project and add the necessary Hilt dependency. See Chapter 3, Handling the UI State in Jetpack Compose and Using Hilt, if you need help setting up Hilt or visit https://dagger.dev/hilt/.
  2. Next, let’s go ahead and add our @HiltAndroidApp class, and in the Manifest folder, add the name of our HiltAndroidApp, in our case, UserInformation...

Supporting multiple entities in Room

In this recipe, you will learn how to handle multiple entities in Room. This is useful whenever you have a big project that needs a different data input. An excellent example that we can work with is a budgeting app.

To support multiple entities in Room, you need to define multiple classes that represent your database tables. Each class should have its own annotations and fields that correspond to columns in a table. For instance, a budgeting app might need different types of models, such as the following:

  • BudgetData
  • ExpenseData
  • ExpenseItem

Hence, having multiple entities is sometimes necessary, and knowing how to handle that comes in handy.

Getting ready

To follow along with this recipe, you must have completed the previous recipe.

How to do it …

You can use any project of your choosing to implement the topics discussed in this recipe. In addition, you can use this example in your pre-existing project...

Migrating an existing SQL database to room

As we mentioned earlier, Room does harness the power of SQLite, and because many applications still use legacy, you might find applications still using SQL and be wondering how you can migrate to Room and utilize the latest Room features.

In this recipe, we will cover the migration of an existing SQL database to Room with step-by-step examples. Furthermore, Room offers an abstraction layer to help with SQLite migrations – that is, by offering the Migration class to developers.

How to do it…

Because we did not create a new SQLite database example, since that is not necessary, we will try to emulate a scenario with a dummy sample SQLite database and showcase how you can migrate your existing SQLite database to Room:

  1. Since we will be adding Room in an existing SQLite project, you will need to ensure you add the required dependencies. To set this up, refer to the Implementing Room in your applications recipe.
  2. ...

Testing your local database

So far, we have ensured that we write tests whenever necessary for our projects. We will now need to go ahead and write tests for our RoomExample project, since this is crucial, and you might be required to do so in a real-world scenario. Hence, in this recipe, we will look at a step-by-step guide on writing CRUD tests for our database.

Getting ready

You will need to open the RoomExample project to get started with this recipe.

How to do it…

Let’s go ahead and first add all the needed Room testing dependencies, and then start writing our tests. For the Hilt test setup, refer to the Technical requirements section, where you can find all the required code:

  1. You will need to add the following to your build.gradle:
    androidTestImplementation "com.google.truth:truth:1.1.3"
    androidTestImplementation "android.arch.core:core-testing:1.1.1"
  2. After you have added the required dependencies inside the Android test...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Modern Android 13 Development Cookbook
Published in: Jul 2023Publisher: PacktISBN-13: 9781803235578
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Madona S. Wambua

Madona S. Wambua is a Google Developer Expert in the Android category, an Android Engineer programming in Kotlin, and the founder and Chief Technology Officer of Jibu Labs. She is also a Women Tech Makers Ambassador and Co-Chair of AnitaB. She has over ten years of experience in the field and has worked on consumer-facing applications and building software development kits for developers. She has also worked on the famous Google Glass during her tenure at a startup and got an opportunity to work on interactive AR videos to transform lives through machine learning and computer vision. Madona is also a Keynote Speaker and the host of Tech Talks with Madona, a podcast geared towards supporting women and allies in tech.
Read more about Madona S. Wambua