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 DataStore to Store Data and Testing

Modern Android Development practices help Android developers create better applications. DataStore is a data storage solution provided by the Android Jetpack library. It allows developers to store key-value pairs or complex objects asynchronously and with consistency guarantees. Data is critical in Android development, and how we save and persist data matters. In this chapter, we will explore using DataStore to persist our data and look at best practices using DataStore.

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

  • Implementing DataStore
  • Adding Dependency Injection to DataStore
  • Using Android Proto DataStore versus DataStore
  • Handling data migration with DataStore
  • Writing tests for our DataStore instance

Technical requirements

Implementing DataStore

When building mobile applications, it is critical to ensure that you persist your data in order to allow for smooth loading, reduce network issues, or even handle data entirely offline. In this recipe, we will look at how to store data in our Android applications using the Modern Android Development Jetpack library called DataStore.

DataStore is a data storage solution for Android applications that enables you to store key-value pairs or any typed objects with protocol buffers. Moreover, DataStore uses Kotlin coroutines and flows to store data consistently, transactionally, and asynchronously.

If you have built Android applications before, you might have used SharedPreferences. The new Preferences DataStore aims to replace this old method. It is also fair to say that Preferences DataStore harnesses the power of SharedPreferences since they are pretty similar. In addition, Google’s documentation recommends that if you’re currently using SharedPreferences...

Adding Dependency Injection to DataStore

Dependency Injection is an important design pattern in software engineering, and its use in Android app development can lead to cleaner and more maintainable code. When it comes to DataStore in Android, which is a modern data storage solution introduced in Android Jetpack, adding Dependency Injection can bring several benefits:

  • By using Dependency Injection, you can separate the concerns of creating an instance of DataStore from the code that uses it. This means that your business logic code will not have to worry about how to create a DataStore instance and can instead focus on what it needs to do with the data.
  • Dependency Injection makes it easier to write unit tests for your app. By injecting a mock DataStore instance into your tests, you can ensure that your tests are not affected by the actual state of the DataStore.
  • Dependency Injection can help you break down your code into smaller, more manageable modules. This makes...

Using Android Proto DataStore versus DataStore

Figure 5.2 shows the differences between PreferencesDataStore, SharedPreferences, and ProtoDataStore. In this recipe, we will explore how we can use Proto DataStore. The Proto DataStore implementation uses DataStore and Protocol Buffers to persist typed objects to the disk.

Proto DataStore is similar to Preferences DataStore, but unlike Preferences DataStore, Proto does not use key-value pairs and just returns the generated object in the flow. The file types and structure of the data depend on the schema of the .protoc files.

Getting ready

We will use our already created project to show how you can use Proto DataStore in Android. We will also use already created classes and just give the functions different names.

How to do it…

  1. We will need to start by setting up the required dependencies, so let’s go ahead and add the following to our Gradle app-level file:
    implementation "androidx.DataStore:DataStore...

Handling data migration with DataStore

If you have built Android applications before, you might have used SharedPreferences; the good news now is that there is support for migration, and you can migrate from SharedPreferences to DataStore using SharedPreferenceMigration. As with any data, we will always modify our dataset; for instance, we might want to rename our data model values or even change their type.

In such a scenario, we will need a DataStore to DataStore migration; that is what we will be working on in this recipe. The process is pretty similar to the migration from SharedPreferences; as a matter of fact, SharedPreferencesMigration is an implementation of the DataMigration interface class.

Getting ready

Since we just created a new PreferenceDataStore, we will not need to migrate it, but we can look at ways to implement a migration in case a need arises.

How to do it…

In this recipe, we will look at how you can utilize the knowledge learned to help you...

Writing tests for our DataStore instance

Writing tests is crucial in Android development, and in this recipe, we will write some tests for our DataStore instance. To test our DataStore instance or any DataStore instance, we first need to have instrumentation testing set up since we will be reading and writing in actual files (DataStore), and it is vital to verify that accurate updates are being made.

How to do it…

We will start by creating a simple unit test to test our view model function:

  1. In our unit test folder, create a new folder and call it test, and inside it, go ahead and create a new class called TaskViewModelTest:
      class TaskViewModelTest {}
  2. Next, we will need to add some testing dependencies:
    testImplementation "io.mockk:mockk:1.13.3"
    androidTestImplementation "io.mockk:mockk-android:1.13.3"
    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.2"
  3. Now that we have added the required dependencies...
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 $15.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