# Modernizing Your Android App's Data Storage: SharedPreferences to DataStore

DevFeed: [Modernizing Your Android App's Data Storage: SharedPreferences to DataStore](<https://devfeed.tech/articles/modernizing-your-android-app-s-data-storage-sharedpreferences-to-datastore-25117.md>)

Original publisher: [Read original article](<http://michaelevans.org/blog/2024/10/10/modernizing-your-android-apps-data-storage-sharedpreferences-to-datastore/>)

Author: Michael Evans

Published: 2024-10-10T15:53:57Z

Content type: tutorial

Language: en

Sources: [Gadget Habit](<https://devfeed.tech/sources/gadget-habit.md>)

Topics: [Android](<https://devfeed.tech/topics/android.md>), [migration](<https://devfeed.tech/topics/migration.md>), [schema-evolution](<https://devfeed.tech/topics/schema-evolution.md>), [reactive](<https://devfeed.tech/topics/reactive.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [Google](<https://devfeed.tech/topics/google.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [best-practices](<https://devfeed.tech/tags/best-practices.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [flow-api](<https://devfeed.tech/tags/flow-api.md>), [migration](<https://devfeed.tech/tags/migration.md>), [pitfalls](<https://devfeed.tech/tags/pitfalls.md>), [protocol](<https://devfeed.tech/tags/protocol.md>), [reactive](<https://devfeed.tech/tags/reactive.md>), [schema-evolution](<https://devfeed.tech/tags/schema-evolution.md>), [testing](<https://devfeed.tech/tags/testing.md>)

## AI overview

This tutorial explains how to migrate Android app data from SharedPreferences to Proto DataStore. It covers dependencies, schema definition, serialization, repository design, migration testing, schema evolution, error handling, performance, and testing practices.

## Source excerpt

SharedPreferences has long been a staple for storing small pieces of data and user preferences in Android apps. However, it has notable limitations, such as a lack of type safety, no support for safe schema evolution, and potential performance issues on the main thread. Google introduced Proto DataStore as a modern and robust alternative, offering: Strong typing with Protocol Buffers Safe schema evolution Built-in migration support Flow API for reactive programming Coroutines support for main-thread safety In this post, we'll walk through the process of migrating your existing SharedPreferences data to Proto DataStore without data loss, including best practices and common pitfalls to avoid. Step 1: Add Dependencies First, add the necessary dependencies to your app's build.gradle file: 1 2 3 4 5 6 7 8 9 dependencies { def datastore_version = "1.0.0" // Proto DataStore implementation "androidx.datastore:datastore:$datastore_version" // Protocol Buffers implementation "com.google.protobuf:protobuf-javalite:3.18.0" } Step 2: Define Your Proto DataStore Schema Create a new .proto file in app/src/main/proto/my_data.proto to define your data schema: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 syntax = "proto3"; option java_package = "com.example.app"; option java_multiple_files = true; message UserPreferences { // Define your fields with unique numbers string user_name = 1; bool notifications_enabled = 2; string theme = 3; // Optional: Add a version field for future schema evolution int32 schema_version = 999; } Note the schema_version field - this helps manage schema evolution as your app grows. Step 3: Create a Proto DataStore Serializer The serializer handles reading and writing your protocol buffer messages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 object UserPreferencesSerializer : Serializer<UserPreferences> { override val defaultValue: UserPreferences = UserPreferences.getDefaultInstance() override suspend fun readFrom(input: InputStream): UserPreferences { try { return UserPrefere