Book a free call

Jetpack DataStore in Android Architecture Explained

Jetpack DataStore is Android's modern way to store small app data, replacing SharedPreferences. Here is where it sits in app architecture.

Hertiv engineering teamReviewed by a senior engineer
6 min read
Jetpack DataStore in Android Architecture Explained
On this page

Short answer

Jetpack DataStore is Android's modern data storage library for small, simple pieces of app data, such as settings, preferences and flags, and it is the replacement for SharedPreferences. In app architecture it sits in the data layer, behind a repository, and it exposes the stored values as a stream that the rest of the app observes.

What is Jetpack DataStore?

Jetpack DataStore is a data storage library in Android Jetpack that stores small pieces of app data, such as a user's theme choice, notification settings or onboarding state. It replaces SharedPreferences, the older Android API for the same job, and it is built for modern Android apps that use Kotlin coroutines and Flow.

DataStore comes in two forms. Preferences DataStore stores key-value pairs, such as a boolean for dark mode or a string for a username, without a fixed schema. Proto DataStore stores typed objects defined with protocol buffers, which suits data that needs a strict shape and type safety.

The important difference from SharedPreferences is how data is read. DataStore exposes values as a Flow, a stream that emits a new value whenever the stored data changes, so the interface can update itself instead of polling. It also runs its work off the main thread, so reading or writing data does not freeze the screen.

Preferences DataStore or Proto DataStore: which one?

Preferences DataStore and Proto DataStore solve the same problem in different ways, and the choice depends on whether your data has a fixed shape.

FactorPreferences DataStoreProto DataStore
Data shapeKey-value pairs, no schemaTyped objects, fixed schema
Setup effortSmall, no extra definitionsLarger, needs a proto definition
Type safetyKeys are typed, values are basic typesFull type safety on the whole object
Best forSettings, flags, simple preferencesStructured data, such as a user profile
MigrationBuilt-in migration from SharedPreferencesNeeds a custom migration

Most apps start with Preferences DataStore, because settings and flags are the most common use. Proto DataStore is worth the extra setup when the data has a shape that must stay consistent, such as a cached user profile with several fields that change together.

Where does DataStore sit in Android app architecture?

DataStore sits in the data layer of Android app architecture, behind a repository that the rest of the app talks to. The UI layer never touches DataStore directly; it observes state from a ViewModel, which reads from the repository, which reads from DataStore.

The layers look like this:

  1. UI layer. Composables or fragments observe state from a ViewModel and send user actions back to it.
  2. ViewModel. Holds UI state, converts DataStore flows into state the UI can display, and forwards save requests.
  3. Repository. The single place that knows where data lives. It exposes a Flow of settings and a suspend function to save them.
  4. DataStore. The actual storage, reading and writing key-value pairs or typed objects on disk.

This separation matters because it keeps the storage detail in one place. If the team later moves a setting from DataStore to a server or a database, only the repository changes, and the ViewModel and UI stay the same.

How do you add DataStore to an Android app?

You add DataStore to an Android app by declaring the dependency, creating one DataStore instance for the app, and wiring it into a repository.

  1. Add the dependency. Add the DataStore Preferences dependency to the app module, using the same version as the rest of your Jetpack libraries.
  2. Create the DataStore. Define a single DataStore instance, usually as an extension on Context, with a file name such as settings.preferences_pb.
  3. Define typed keys. Create a key for each setting, such as a boolean key for dark mode, so reads and writes are type-checked.
  4. Build a repository. Write a small class that exposes a Flow for each setting and a suspend function to save it, and inject it into the ViewModel.
  5. Observe from the ViewModel. Collect the Flow in the ViewModel, turn it into UI state, and let the UI react when the stored value changes.
  6. Handle first run. Give every setting a default value, so the app behaves sensibly before the user changes anything.

Keep one DataStore per app, or one per clear group of settings. Many small DataStore instances create many files and many places to maintain, for no benefit.

How do you migrate from SharedPreferences?

You migrate from SharedPreferences by telling DataStore to read the old file once, when it is first created, and copy its values into the new store. Preferences DataStore has a built-in migration for this, so the work is mostly configuration.

The migration runs automatically the first time the DataStore is used. It reads each key from the old SharedPreferences file and writes it to DataStore, then never runs again. The old file can be left in place or deleted later, but the app should not keep writing to it.

Two rules make migration safe. First, use the same key names and types in DataStore as in SharedPreferences, so every old value lands in the right place. Second, test the migration on a device that already has the old app installed, because a migration that only works on a fresh install is not a migration at all.

DataStore or Room: when to use which?

DataStore and Room solve different problems, and most apps end up using both. DataStore is for small, simple data that the app reads as a whole, such as settings. Room is a database for larger, structured data that the app queries, such as lists of items.

QuestionUse DataStoreUse Room
What is stored?Settings, flags, small preferencesLists, records, anything queryable
How is it read?Whole value at once, as a FlowQueries, filters, sorting, paging
How big is it?Small, a few dozen keysLarge, hundreds or thousands of rows
Relations?None, values are independentYes, tables link to each other

A simple test: if you need to search, filter or sort the data, use Room. If you need to remember a handful of user choices and react when they change, use DataStore. Storing a list of a thousand items in DataStore is a mistake, and storing a dark-mode flag in Room is overkill.

Example (hypothetical): migrating 50,000 users to DataStore

Example (hypothetical): a notes app with 50,000 users stores six settings per user in SharedPreferences: theme, font size, sort order, sync on or off, notifications on or off, and a first-run flag. The team decides to migrate to Preferences DataStore.

The migration moves 6 keys per user, so across 50,000 users it moves 300,000 values in total. The team tests the migration on devices with the old app installed and finds that 0.5% of values fail to copy, because a few users have corrupted preference files.

That is 300,000 multiplied by 0.005, or 1,500 values that fall back to defaults. The team accepts this, because the affected settings are minor and the defaults are safe, and it adds a log so the next release can measure the real failure rate. The migration ships, and support tickets about lost settings stay flat.

What are the common mistakes?

The most common mistakes with DataStore come from treating it like a database, reading it on the wrong thread, or creating too many instances.

  • Storing large data. DataStore reads the whole file when it starts. A thousand items in DataStore means a slow start and a large file. Use Room for lists.
  • Reading DataStore from the UI. The UI should observe a ViewModel, not DataStore. Direct reads from composables scatter storage logic through the interface.
  • Blocking the main thread. DataStore is built for coroutines. Reading it with a blocking call on the main thread can freeze the screen, which is exactly what it was designed to avoid.
  • Many DataStore instances. One DataStore per setting creates many files and many failure points. Group related settings in one store.
  • No default values. A setting with no default crashes or shows nothing on first run. Every key needs a sensible default.
  • Skipping migration testing. A migration that works on a fresh install but fails on a real user's old data is worse than no migration, because it loses settings silently.

Key takeaways

  • DataStore is Jetpack's replacement for SharedPreferences, built for Kotlin coroutines and Flow
  • Preferences DataStore stores key-value pairs; Proto DataStore stores typed objects with a fixed schema
  • In clean architecture, DataStore lives in the data layer behind a repository, never in the UI
  • Use DataStore for settings and flags, and Room for anything you need to search, filter or sort
  • Test migration on a device with the old app installed, or it is not a real migration test

Frequently asked questions

Is DataStore a database?

No. DataStore is a storage library for small pieces of data such as settings and preferences. It does not support queries, indexes or relations. For structured data that needs to be searched or filtered, Room is the right tool.

Should I still use SharedPreferences in a new app?

No. Google recommends DataStore for new apps, because it is built for coroutines, runs off the main thread and handles errors more safely. SharedPreferences still works, but it is the older API and it blocks the calling thread on reads.

What is the difference between Preferences and Proto DataStore?

Preferences DataStore stores key-value pairs with no schema, which is quick to set up and fits settings and flags. Proto DataStore stores typed objects defined with protocol buffers, which gives full type safety but needs more setup. Most apps start with Preferences.

Can DataStore store large files or images?

No. DataStore is for small values such as settings, flags and short strings. Large data belongs in files or in a database. Storing large values in DataStore slows down startup, because DataStore reads the whole file when it starts.

Does DataStore work with Jetpack Compose?

Yes. The ViewModel collects the DataStore Flow and exposes it as UI state, which Compose observes and redraws when the stored value changes. This is the standard pattern: DataStore in the data layer, ViewModel in the middle, Compose on top.

How do I handle errors when reading DataStore?

Catch the exception on the Flow and emit a default value instead, so a corrupted file degrades to defaults rather than crashing the app. Log the error so the team can see how often it happens, and consider a repair path for the next release.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: software products in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Building an Android app with modern architecture? Talk to Hertiv.

A senior engineer replies within one business day. Or see our Mobile App Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to contact@hertiv.com.

Prefer email? Write to contact@hertiv.com
Estimate costBook a call