What is DataStore?

DataStore is a new and improved data storage solution aimed at replacing SharedPreferences. Built on Kotlin coroutines and Flow, DataStore provides two different implementations: Proto DataStore, that lets you store typed objects (backed by protocol buffers) and Preferences DataStore, that stores key-value pairs. Data is stored asynchronously, consistently, and transactionally, overcoming some of the drawbacks of SharedPreferences.

What you'll learn

What you will build

In this codelab, you're going to start with a sample app that displays a list of tasks that can be filtered by their completed status and can be sorted by priority and deadline.

The boolean flag for the Show completed tasks filter is saved in memory. The sort order is persisted to disk using a SharedPreferences object.

As DataStore has 2 different implementations: Preferences DataStore and Proto DataStore, you will learn how to use Proto DataStore completing the following tasks in each implementation:

We recommend working through the Preferences DataStore codelab too, so you better understand the difference between the two.

What you'll need

In this step, you will download the code for the entire codelab and then run a simple example app.

To get you started as quickly as possible, we have prepared a starter project for you to build on.

If you have git installed, you can simply run the command below. To check whether git is installed, type git --version in the terminal or command line and verify that it executes correctly.

 git clone https://github.com/googlecodelabs/android-datastore

The initial state is in the master branch. The solution code is located in the proto_datastore branch.

If you do not have git, you can click the following button to download all of the code for this codelab:

Download source code

  1. Unzip the code, and then open the project in Android Studio version 3.6 or higher.
  2. Run the app run configuration on a device or emulator.

The app runs and displays the list of tasks:

The app allows you to see a list of tasks. Each task has the following properties: name, completed status, priority, and deadline.

To simplify the code we need to work with, the app allows you to do only two actions:

The app follows the architecture recommended in the "Guide to app architecture". Here's what you will find in each package:



TasksViewModel - holds all the elements necessary to build the data that needs to be displayed in the UI: the list of tasks, the show completed and sort order flags, wrapped in a TasksUiModel object. Every time one of these values changes, we have to reconstruct a new TasksUiModel. To do this, we combine 3 elements:

To ensure that we're updating the UI correctly, only when the Activity is started, we expose a LiveData<TasksUiModel>.

We have a couple of problems with our code:

Although both the show completed and sort order flags are user preferences, currently they're represented as two different objects. So one of our goals will be to unify these two flags under a UserPreferences class.

Let's find out how to use DataStore to help us with these issues.

Often you might find yourself needing to store small or simple data sets. For this, in the past, you might have used SharedPreferences but this API also has a series of drawbacks. Jetpack DataStore library aims at addressing those issues, creating a simple, safer and asynchronous API for storing data. It provides 2 different implementations:







Async API

✅ (only for reading changed values, via listener)

✅ (via Flow)

✅ (via Flow)

Synchronous API

✅ (but not safe to call on UI thread)

Safe to call on UI thread


✅ (work is moved to Dispatchers.IO under the hood)

✅ (work is moved to Dispatchers.IO under the hood)

Can signal errors

Safe from runtime exceptions


Has a transactional API with strong consistency guarantees

Handles data migration

✅ (from SharedPreferences)

✅ (from SharedPreferences)

Type safety

✅ with Protocol Buffers

* SharedPreferences has a synchronous API that can appear safe to call on the UI thread, but which actually does disk I/O operations. Furthermore, apply() blocks the UI thread on fsync(). Pending fsync() calls are triggered every time any service starts or stops, and every time an activity starts or stops anywhere in your application. The UI thread is blocked on pending fsync() calls scheduled by apply(), often becoming a source of ANRs.

** SharedPreferences throws parsing errors as runtime exceptions.

Preferences vs Proto DataStore

While both Preferences and Proto DataStore allow saving data, they do this in different ways:

Room vs DataStore

If you have a need for partial updates, referential integrity, or large/complex datasets, you should consider using Room instead of DataStore. DataStore is ideal for small or simple datasets and does not support partial updates or referential integrity.

One of the downsides of SharedPreferences and Preferences DataStore is that there is no way to define a schema or to ensure that keys are accessed with the correct type. Proto DataStore addresses this problem by using Protocol buffers to define the schema. Using protos DataStore knows what types are stored and will just provide them, removing the need for using keys.

Let's see how to add Proto DataStore and Protobufs to the project, what Protocol buffers are and how to use them with Proto DataStore and how to migrate SharedPreferences to DataStore.

Adding dependencies

To work with Proto DataStore and get Protobuf to generate code for our schema, we'll have to make several changes to the build.gradle file:

plugins {
    id "com.google.protobuf" version "0.8.12"

dependencies {
    implementation  "androidx.datastore:datastore-core:1.0.0-alpha01"
    implementation  "com.google.protobuf:protobuf-javalite:3.10.0"

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.10.0"

    // Generates the java Protobuf-lite code for the Protobufs in this project. See
    // https://github.com/google/protobuf-gradle-plugin#customizing-protobuf-compilation
    // for more information.
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {
                    option 'lite'

Protocol buffers are a mechanism for serializing structured data. You define how you want your data to be structured once and then the compiler generates source code to easily write and read the structured data.

Create the proto file

You define your schema in a proto file. In our codelab we have 2 user preferences: show completed and sort order; currently they're represented as two different objects. So one of our goals is to unify these two flags under a UserPreferences class that gets stored in DataStore. Instead of defining this class in Kotlin, we will define it in protobuf schema.

Check out the Proto language guide for in depth info on the syntax. In this codelab we're only going to focus on the types we need.

Create a new file called user_prefs.proto in the app/src/main/proto directory. If you don't see this folder structure, switch to Project view. In protobufs, each structure is defined using a message keyword and each member of the structure is defined inside the message, based on type and name and it gets assigned a 1-based order. Let's define a UserPreferences message that, for now, just has a boolean value called showCompleted.

syntax = "proto3";

option java_package = "com.codelab.android.datastore";
option java_multiple_files = true;

message UserPreferences {
  // filter for showing / hiding completed tasks
  bool show_completed = 1;

Create the serializer

To tell DataStore how to read and write the data type we defined in the proto file, we need to implement a Serializer. Create a new file called UserPreferencesSerializer in the data package:

object UserPreferencesSerializer : Serializer<UserPreferences> {
    override fun readFrom(input: InputStream): UserPreferences {
        try {
            return UserPreferences.parseFrom(input)
        } catch (exception: InvalidProtocolBufferException) {
            throw CorruptionException("Cannot read proto.", exception)

    override fun writeTo(t: UserPreferences, output: OutputStream) = t.writeTo(output)

Creating the DataStore

The show completed flag is kept in memory, in TasksViewModel. Let's create a DataStore<UserPreferences> private field in UserPreferencesRepository, based on Context.createDataStore() extension method. The method has two mandatory parameters:

private val dataStore: DataStore<UserPreferences> =
        fileName = "user_prefs.pb",
        serializer = UserPreferencesSerializer)

Reading data from Proto DataStore

Proto DataStore exposes the data stored in a Flow<UserPreferences>. Let's create a public userPreferencesFlow: Flow<UserPreferences> value that gets assigned dataStore.data:

val userPreferencesFlow: Flow<UserPreferences> = dataStore.data

Handling exceptions while reading data

As DataStore reads data from a file, IOExceptions are thrown when an error occurs while reading data. We can handle these by using the catch Flow transformation and just log the error:

private val TAG: String = "UserPreferencesRepo"

val userPreferencesFlow: Flow<UserPreferences> = dataStore.data
    .catch { exception ->
        // dataStore.data throws an IOException when an error is encountered when reading data
        if (exception is IOException) {
            Log.e(TAG, "Error reading sort order preferences.", exception)
        } else {
            throw exception

Writing data to Proto DataStore

To write data, DataStore offers a suspending DataStore.updateData() function, where we get as parameter the current state of UserPreferences. To update it, we'll have to transform the preferences object to builder, set the new value and then build the new preferences.

updateData() updates the data transactionally in an atomic read-write-modify operation. The coroutine completes once the data is persisted on disk.

Let's create a suspend function that allows us to update the show completed property of UserPreferences, called updateShowCompleted(), that calls dataStore.updateData() and sets the new value:

suspend fun updateShowCompleted(completed: Boolean) {
    dataStore.updateData { preferences ->

At this point, the app should compile but the functionality we just created in UserPreferencesRepository is not used.

Defining the data to be saved in proto

The sort order is saved in SharedPreferences. Let's move it to DataStore. To do this, let's start by updating UserPreferences in the proto file to also store the sort order. As the sort order is an enum we will have to define it in our UserPreference. enums are defined in protobufs similarly to Kotlin.

For enumerations, the default value is the first value listed in the enum's type definition. But, when migrating from SharedPreferences we need to know whether the value we got is the default value or the one previously set in SharedPreferences. To help with this, we define a new value to our SortOrder enum: UNSPECIFIED and list it first, so it can be the default value.

Our user_prefs.proto file should look like this:

syntax = "proto3";

option java_package = "com.codelab.android.datastore";
option java_multiple_files = true;

message UserPreferences {
  // filter for showing / hiding completed tasks
  bool show_completed = 1;

  // defines tasks sorting order: no order, by deadline, by priority, by deadline and priority
  enum SortOrder {
    NONE = 1;
    BY_DEADLINE = 2;
    BY_PRIORITY = 3;

  // user selected tasks sorting order
  SortOrder sort_order = 2;

Clean and rebuild your project to ensure that a new UserPreferences object is generated, containing the new field.

Now that SortOrder is defined in the proto file, we can remove the declaration from UserPreferencesRepository. Delete:

enum class SortOrder {

Make sure the right SortOrder import is used everywhere:

import com.codelab.android.datastore.UserPreferences.SortOrder

In the TasksViewModel.filterSortTasks() we're doing different actions based on the SortOrder type. Now that we also added the UNSPECIFIED option, we need to add another case for the when(sortOrder) statement. As we don't want to handle other options than the ones we are right now, we can just throw an UnsupportedOperationException in other cases.

Our filterSortTasks() function looks like this now:

private fun filterSortTasks(
    tasks: List<Task>,
    showCompleted: Boolean,
    sortOrder: SortOrder
): List<Task> {
    // filter the tasks
    val filteredTasks = if (showCompleted) {
    } else {
        tasks.filter { !it.completed }
    // sort the tasks
    return when (sortOrder) {
        SortOrder.UNSPECIFIED -> filteredTasks
        SortOrder.NONE -> filteredTasks
        SortOrder.BY_DEADLINE -> filteredTasks.sortedByDescending { it.deadline }
        SortOrder.BY_PRIORITY -> filteredTasks.sortedBy { it.priority }
        SortOrder.BY_DEADLINE_AND_PRIORITY -> filteredTasks.sortedWith(
            compareByDescending<Task> { it.deadline }.thenBy { it.priority }
        // We shouldn't get any other values
        else -> throw UnsupportedOperationException("$sortOrder not supported")

Migrating from SharedPreferences

To help with migration, DataStore defines the SharedPreferencesMigration class. Let's create it in UserPreferencesRepository. The migrate block gives us two parameters:

We will have to return a UserPreferences object.

When implementing the migrate block, we'll have to do the following steps:

  1. Check the sortOrder value in UserPreferences.
  2. If this is SortOrder.UNSPECIFIED it means that we need to retrieve the value from SharedPreferences. If the SortOrder is missing then we can use SortOrder.NONE as default.
  3. Once we get the sort order, we'll have to convert the UserPreferences object to builder, set the sort order and then build the object again by calling build(). No other fields will be affected with this change.
  4. If the sortOrder value in UserPreferences is not SortOrder.UNSPECIFIED we can just return the current data we got in migrate since the migration must have already ran successfully.
private val sharedPrefsMigration = SharedPreferencesMigration(
) { sharedPrefs: SharedPreferencesView, currentData: UserPreferences ->
        // Define the mapping from SharedPreferences to UserPreferences
        if (currentData.sortOrder == SortOrder.UNSPECIFIED) {
        } else {

Now that we defined the migration logic, we need to tell DataStore that it should use it. For this, update the DataStore builder and assign to the migrations parameter a new list that contains an instance of our SharedPreferencesMigration:

private val dataStore: DataStore<UserPreferences> = context.createDataStore(
    fileName = "user_prefs.pb",
    serializer = UserPreferencesSerializer,
    migrations = listOf(sharedPrefsMigration)

Saving the sort order to DataStore

To update the sort order when enableSortByDeadline() and enableSortByPriority() are called, we have to do the following:

Here's how the enableSortByDeadline() implementation looks like. We'll let you do the changes for enableSortByPriority() by yourself.

suspend fun enableSortByDeadline(enable: Boolean) {
    // updateData handles data transactionally, ensuring that if the sort is updated at the same
    // time from another thread, we won't have conflicts
    dataStore.updateData { preferences ->
        val currentOrder = preferences.sortOrder
        val newSortOrder =
            if (enable) {
                if (currentOrder == SortOrder.BY_PRIORITY) {
                } else {
            } else {
                if (currentOrder == SortOrder.BY_DEADLINE_AND_PRIORITY) {
                } else {

Now that UserPreferencesRepository stores both show completed and sort order flags in DataStore and exposes a Flow<UserPreferences>, let's update the TasksViewModel to use them.

Remove showCompletedFlow and sortOrderFlow and instead, create a value called userPreferencesFlow that gets initialised with userPreferencesRepository.userPreferencesFlow:

private val userPreferencesFlow = userPreferencesRepository.userPreferencesFlow

In the tasksUiModelFlow creation, replace showCompletedFlow and sortOrderFlow with userPreferencesFlow. Replace the parameters accordingly.

When calling filterSortTasks pass in the showCompleted and sortOrder of the userPreferences. Your code should look like this:

private val tasksUiModelFlow = combine(
    ) { tasks: List<Task>, userPreferences: UserPreferences ->
        return@combine TasksUiModel(
            tasks = filterSortTasks(
            showCompleted = userPreferences.showCompleted,
            sortOrder = userPreferences.sortOrder

The showCompletedTasks() function should now be updated to call userPreferencesRepository.updateShowCompleted(). As this is a suspend function, create a new coroutine in the viewModelScope:

fun showCompletedTasks(show: Boolean) {
    viewModelScope.launch {

userPreferencesRepository functions enableSortByDeadline() and enableSortByPriority() are now suspend functions so they should also be called in a new coroutine, launched in the viewModelScope:

fun enableSortByDeadline(enable: Boolean) {
    viewModelScope.launch {

fun enableSortByPriority(enable: Boolean) {
    viewModelScope.launch {

Clean up UserPreferencesRepository

Let's remove the fields and methods that are no longer needed. You should be able to delete the following:

Our app should now compile successfully. Let's run it to see if the show completed and sort order flags are saved correctly.

Check out the proto branch of the codelab repo to compare your changes.

Now that you migrated to Proto DataStore, let's recap what we've learned: