This sample app demonstrates how to migrate from the deprecated EncryptedSharedPreferences to the modern Proto DataStore secured by Google Tink.
As of 2025, EncryptedSharedPreferences is deprecated. This project serves as a reference implementation for migrating your secure storage to a robust, future-proof architecture using:
- Jetpack DataStore (Proto): Type-safe, asynchronous, and main-thread safe storage.
- Google Tink (StreamingAead): Industry-standard encryption that encrypts the data stream on-the-fly.
- 🔐 Secure Storage: Uses AES-128 GCM HKDF 4KB streaming encryption via Google Tink.
- 📦 Type Safety: Uses Protocol Buffers (
user_prefs.proto) to define the data schema. - 🔄 Seamless Migration: Includes a
SharedPreferencesMigrationstrategy to automatically move data from oldSharedPreferences(orEncryptedSharedPreferences) to DataStore upon first launch. - ⚡ Modern Android: Built with Kotlin, Coroutines, Flow, and Hilt.
- EncryptedSharedPreferences: Wrapped
SharedPreferenceswith an encryption layer. - Issues: Synchronous I/O on main thread, strict mode violations, and stability issues on some devices.
- CryptoManager: Manages the Tink keyset (stored in Android Keystore) and provides
encrypt/decryptstreams. - UserPreferencesSerializer: Intercepts DataStore read/write operations. It decrypts the stream before parsing the Proto object and encrypts it before writing to disk.
- DataStoreRepository: Exposes a clean API (
Flow<UserPreferences>) and handles the migration logic.
- Clone this repository.
- Open in Android Studio (Koala or newer recommended).
- Run on an emulator or physical device.
To verify the encryption:
- Run the app and save some data.
- Open Device File Explorer in Android Studio.
- Navigate to
/data/data/com.encryptedsharedprefrencesdemo/files/datastore/user_prefs.pb. - Try to open the file. You will see unreadable binary data, confirming that Tink is working!
- Medium Article: Encrypted Preferences with Google Tink (Part 1)
- Goodbye EncryptedSharedPreferences: A 2026 Migration Guide (Part 2)
This project is licensed under the MIT License.