# Finally The Official Way of Secure DataStore is Here!

DevFeed: [Finally The Official Way of Secure DataStore is Here!](<https://devfeed.tech/articles/finally-the-official-way-of-secure-datastore-is-here-91132.md>)

Original publisher: [Read original article](<https://proandroiddev.com/finally-the-official-way-of-secure-datastore-is-here-ae878f5eb457?source=rss----c72404660798---4>)

Author: Burak Karaduman

Published: 2026-10-20T16:18:09Z

Content type: tutorial

Language: en

Sources: [ProAndroidDev](<https://devfeed.tech/sources/proandroiddev-medium.md>)

Topics: [Cryptography](<https://devfeed.tech/topics/cryptography.md>), [Android Security](<https://devfeed.tech/topics/android-security.md>), [Secure token management](<https://devfeed.tech/topics/secure-token-management.md>), [Jetpack](<https://devfeed.tech/topics/jetpack.md>), [coroutines-flow](<https://devfeed.tech/topics/coroutines-flow.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [Exception](<https://devfeed.tech/topics/exception.md>), [Test-driven development](<https://devfeed.tech/topics/tdd.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [backup](<https://devfeed.tech/tags/backup.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [data](<https://devfeed.tech/tags/data.md>), [encryption](<https://devfeed.tech/tags/encryption.md>), [error-handling](<https://devfeed.tech/tags/error-handling.md>), [flow](<https://devfeed.tech/tags/flow.md>), [jetpack-datastore](<https://devfeed.tech/tags/jetpack-datastore.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [mobile-app-security](<https://devfeed.tech/tags/mobile-app-security.md>)

## AI overview

The excerpt shows how `androidx.datastore:datastore-tink` can encrypt a typed DataStore through an `AeadSerializer` backed by Tink and an Android Keystore master key, replacing custom AES/GCM storage code. It explains that encryption protects copied files but not data read by the app itself. Decryption or deserialization failures become DataStore corruption; a handler can reset a session, while irreplaceable data needs another recovery plan. Because Android backups can restore the encrypted file and keyset without the device-bound master key, the excerpt advises excluding both from cloud and device-transfer backups. It also outlines migration from legacy encrypted preferences and notes that the library is alpha, key rotation is manual, and migration was unit-tested but not tested on an install retaining real legacy data.

## Source excerpt

Securing data has always been a concern since the early days of app development. Platforms do their best to protect user data, but there are always edge cases. Attackers may still reach sensitive data, even in encrypted form, and if they get the key, the protection is meaningless. So yes, encryption helps, but it is not magic. On Android we started with SharedPreferences. It was simple, widely used, and even had built in encryption through EncryptedSharedPreferences. Then Kotlin Flow and Coroutines arrived, apps reached a global audience, and codebases grew. We now needed async operations, thread safety, type safety, custom objects, proper error handling and reactive streams. SharedPreferences was not designed for that, and DataStore was introduced to solve it. But Google deprecated the Jetpack Security (security-crypto) library, and DataStore had no built in encryption, so most of us wrote our own. Now there is androidx.datastore:datastore-tink, added in DataStore 1.3.0-alpha07. In this post we build a small encrypted session store: a login screen that saves an email and a token, and a home screen that reads them. We also show what happens when decryption fails. The full sample project (with Hilt) is on GitHub: burkido/official-datastore-encryption-sample What encryption protects, and what it does not Encrypting a file protects it when someone gets the file but not your app, for example from a rooted phone, a raw copy of the device storage or a copied backup. It does not protect against code inside your own app, because that code can simply read the decrypted value from DataStore. Encryption is one layer of security. It is not the whole solution.How we did it before Our old setup was similar to the one in this article. A security class created an AES/GCM key in the Android Keystore and managed the IV manually: fun encryptData(keyAlias: String, text: String): Pair<ByteArray, ByteArray> { val secretKey = generateSecretKey(keyAlias) cipher.init(Cipher.ENCRYPT_MODE, se