-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add MASTG-KNOW-0x01: Android DataStore knowledge article #3785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
4
commits into
master
Choose a base branch
from
copilot/add-mastg-know-file-for-datastore
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a943e1b
Initial plan
Copilot d9f71bb
Add MASTG-KNOW-0x01: Android DataStore knowledge file
Copilot 355d7c9
Fix review feedback: use neutral, descriptive language in DataStore K…
Copilot 7b107ed
Merge branch 'master' into copilot/add-mastg-know-file-for-datastore
cpholguera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,70 @@ | ||||||
| --- | ||||||
| masvs_category: MASVS-STORAGE | ||||||
| platform: android | ||||||
| title: Android DataStore | ||||||
| available_since: 21 | ||||||
| --- | ||||||
|
|
||||||
| [Jetpack DataStore](https://developer.android.com/topic/libraries/architecture/datastore) is an Android data storage library designed as the modern replacement for [`SharedPreferences`](https://developer.android.com/training/data-storage/shared-preferences). It stores key-value pairs or typed objects asynchronously using Kotlin coroutines and Flow, providing a non-blocking, consistent API. | ||||||
|
|
||||||
| DataStore comes in two flavors: | ||||||
|
|
||||||
| - **Preferences DataStore**: stores and accesses untyped key-value pairs, similar to `SharedPreferences` but without an XML schema. | ||||||
| - **Proto DataStore**: stores typed objects defined with [Protocol Buffers](https://protobuf.dev/) (protobuf), providing type safety at compile time. | ||||||
|
|
||||||
| ## Storage Location | ||||||
|
|
||||||
| Both DataStore variants write their data to the app's internal storage, under the app-specific directory: | ||||||
|
|
||||||
| - Preferences DataStore: `/data/data/<package-name>/files/datastore/<filename>.preferences_pb` | ||||||
| - Proto DataStore: `/data/data/<package-name>/files/datastore/<filename>.pb` | ||||||
|
|
||||||
| The data is stored in protobuf binary format, not in plain-text XML like `SharedPreferences`. The files are not encrypted by default. | ||||||
|
|
||||||
| ## API Overview | ||||||
|
|
||||||
| ### Preferences DataStore | ||||||
|
|
||||||
| A `DataStore<Preferences>` instance is typically created at the top level using a file-delegate: | ||||||
|
|
||||||
| ```kotlin | ||||||
| val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings") | ||||||
| ``` | ||||||
|
|
||||||
| Data is read via a `Flow`: | ||||||
|
|
||||||
| ```kotlin | ||||||
| val MY_KEY = stringPreferencesKey("my_key") | ||||||
| val value: Flow<String?> = context.dataStore.data.map { preferences -> | ||||||
| preferences[MY_KEY] | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| Data is written with a suspending `edit` call: | ||||||
|
|
||||||
| ```kotlin | ||||||
| context.dataStore.edit { preferences -> | ||||||
| preferences[MY_KEY] = "myValue" | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| ### Proto DataStore | ||||||
|
|
||||||
| A `DataStore<T>` instance for a protobuf-defined type `T` requires a custom `Serializer<T>` and is created with `createDataStore` or the `dataStore` delegate: | ||||||
|
|
||||||
| ```kotlin | ||||||
| val Context.settingsDataStore: DataStore<Settings> by dataStore( | ||||||
| fileName = "settings.pb", | ||||||
| serializer = SettingsSerializer | ||||||
| ) | ||||||
| ``` | ||||||
|
|
||||||
| Reads and writes follow the same coroutine-based `data` Flow and `updateData` API as Preferences DataStore. | ||||||
|
|
||||||
| ## Encryption | ||||||
|
|
||||||
| Neither Preferences DataStore nor Proto DataStore encrypts data at rest by default. The `Serializer` can be wrapped with custom encryption logic using the [Android Keystore](https://developer.android.com/training/articles/keystore) or a library such as [Tink](https://developers.google.com/tink) to encrypt data at rest. | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Tink is Google's specifically recommended library for DataStore encryption, not just one option among many. |
||||||
|
|
||||||
| ## Backup Behavior | ||||||
|
|
||||||
| DataStore files stored under the app's internal `files/datastore/` directory are included in [Android Auto Backup](https://developer.android.com/identity/data/autobackup) by default (available since Android 6.0, API level 23). Apps can opt specific files out of backup using the `android:fullBackupContent` rules or `android:dataExtractionRules` (Android 12 (API level 31) and higher). | ||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added scope limitation per official docs.
Reference: https://developer.android.com/topic/libraries/architecture/datastore