Asynchronous, consistent, transactional storage replacing SharedPreferences; supports preferences and typed objects with Proto/JSON serialization, optional Rx adapters, and atomic updates.
Store data asynchronously, consistently, and transactionally, overcoming some of the drawbacks of SharedPreferences
To add a dependency on DataStore, you must add the Google Maven repository to your project. Read Google's Maven repository for more information.
DataStore provides different options for serialization, choose one or the other. You can also add Android-free dependencies to either implementation.
Add the dependencies for the implementation you need in the build.gradle file for your app or module:
Add the following lines to the dependencies part of your gradle file:
dependencies {
// Preferences DataStore (SharedPreferences like APIs)
implementation("androidx.datastore:datastore-preferences:1.2.0")
// Alternatively - without an Android dependency.
implementation("androidx.datastore:datastore-preferences-core:1.2.0")
}dependencies {
// Preferences DataStore (SharedPreferences like APIs)
implementation "androidx.datastore:datastore-preferences:1.2.0"
// Alternatively - without an Android dependency.
implementation "androidx.datastore:datastore-preferences-core:1.2.0"
}To add optional RxJava support, add the following dependencies:
dependencies {
// optional - RxJava2 support
implementation("androidx.datastore:datastore-preferences-rxjava2:1.2.0")
// optional - RxJava3 support
implementation("androidx.datastore:datastore-preferences-rxjava3:1.2.0")
}dependencies {
// optional - RxJava2 support
implementation "androidx.datastore:datastore-preferences-rxjava2:1.2.0"
// optional - RxJava3 support
implementation "androidx.datastore:datastore-preferences-rxjava3:1.2.0"
}Add the following lines to the dependencies part of your gradle file:
dependencies {
// Typed DataStore for custom data objects (for example, using Proto or JSON).
implementation("androidx.datastore:datastore:1.2.0")
// Alternatively - without an Android dependency.
implementation("androidx.datastore:datastore-core:1.2.0")
}dependencies {
// Typed DataStore for custom data objects (for example, using Proto or JSON).
implementation "androidx.datastore:datastore:1.2.0"
// Alternatively - without an Android dependency.
implementation "androidx.datastore:datastore-core:1.2.0"
}Add the following optional dependencies for RxJava support:
dependencies {
// optional - RxJava2 support
implementation("androidx.datastore:datastore-rxjava2:1.2.0")
// optional - RxJava3 support
implementation("androidx.datastore:datastore-rxjava3:1.2.0")
}dependencies {
// optional - RxJava2 support
implementation "androidx.datastore:datastore-rxjava2:1.2.0"
// optional - RxJava3 support
implementation "androidx.datastore:datastore-rxjava3:1.2.0"
}To serialize content, add dependencies for either Protocol Buffers or JSON serialization.
To use JSON serialization, add the following to your Gradle file:
plugins {
id("org.jetbrains.kotlin.plugin.serialization") version "2.2.20"
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
}plugins {
id("org.jetbrains.kotlin.plugin.serialization") version "2.2.20"
}
dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0"
}To use Protobuf serialization, add the following to your Gradle file:
plugins {
id("com.google.protobuf") version "0.9.5"
}
dependencies {
implementation("com.google.protobuf:protobuf-kotlin-lite:4.32.1")
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:4.32.1"
}
generateProtoTasks {
all().forEach { task ->
task.builtins {
create("java") {
option("lite")
}
create("kotlin")
}
}
}
}plugins {
id("com.google.protobuf") version "0.9.5"
}
dependencies {
implementation "com.google.protobuf:protobuf-kotlin-lite:4.32.1"
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:4.32.1"
}
generateProtoTasks {
all().forEach { task ->
task.builtins {
create("java") {
option("lite")
}
create("kotlin")
}
}
}
}Store data asynchronously, consistently, and transactionally, overcoming some of the drawbacks of SharedPreferences
To add a dependency on DataStore, you must add the Google Maven repository to your project. Read Google's Maven repository for more information.
DataStore provides different options for serialization, choose one or the other. You can also add Android-free dependencies to either implementation.
Add the dependencies for the implementation you need in the build.gradle file for your app or module:
Add the following lines to the dependencies part of your gradle file:
dependencies {
// Preferences DataStore (SharedPreferences like APIs)
implementation("androidx.datastore:datastore-preferences:1.2.0")
// Alternatively - without an Android dependency.
implementation("androidx.datastore:datastore-preferences-core:1.2.0")
}dependencies {
// Preferences DataStore (SharedPreferences like APIs)
implementation "androidx.datastore:datastore-preferences:1.2.0"
// Alternatively - without an Android dependency.
implementation "androidx.datastore:datastore-preferences-core:1.2.0"
}To add optional RxJava support, add the following dependencies:
dependencies {
// optional - RxJava2 support
implementation("androidx.datastore:datastore-preferences-rxjava2:1.2.0")
// optional - RxJava3 support
implementation("androidx.datastore:datastore-preferences-rxjava3:1.2.0")
}dependencies {
// optional - RxJava2 support
implementation "androidx.datastore:datastore-preferences-rxjava2:1.2.0"
// optional - RxJava3 support
implementation "androidx.datastore:datastore-preferences-rxjava3:1.2.0"
}Add the following lines to the dependencies part of your gradle file:
dependencies {
// Typed DataStore for custom data objects (for example, using Proto or JSON).
implementation("androidx.datastore:datastore:1.2.0")
// Alternatively - without an Android dependency.
implementation("androidx.datastore:datastore-core:1.2.0")
}dependencies {
// Typed DataStore for custom data objects (for example, using Proto or JSON).
implementation "androidx.datastore:datastore:1.2.0"
// Alternatively - without an Android dependency.
implementation "androidx.datastore:datastore-core:1.2.0"
}Add the following optional dependencies for RxJava support:
dependencies {
// optional - RxJava2 support
implementation("androidx.datastore:datastore-rxjava2:1.2.0")
// optional - RxJava3 support
implementation("androidx.datastore:datastore-rxjava3:1.2.0")
}dependencies {
// optional - RxJava2 support
implementation "androidx.datastore:datastore-rxjava2:1.2.0"
// optional - RxJava3 support
implementation "androidx.datastore:datastore-rxjava3:1.2.0"
}To serialize content, add dependencies for either Protocol Buffers or JSON serialization.
To use JSON serialization, add the following to your Gradle file:
plugins {
id("org.jetbrains.kotlin.plugin.serialization") version "2.2.20"
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
}plugins {
id("org.jetbrains.kotlin.plugin.serialization") version "2.2.20"
}
dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0"
}To use Protobuf serialization, add the following to your Gradle file:
plugins {
id("com.google.protobuf") version "0.9.5"
}
dependencies {
implementation("com.google.protobuf:protobuf-kotlin-lite:4.32.1")
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:4.32.1"
}
generateProtoTasks {
all().forEach { task ->
task.builtins {
create("java") {
option("lite")
}
create("kotlin")
}
}
}
}plugins {
id("com.google.protobuf") version "0.9.5"
}
dependencies {
implementation "com.google.protobuf:protobuf-kotlin-lite:4.32.1"
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:4.32.1"
}
generateProtoTasks {
all().forEach { task ->
task.builtins {
create("java") {
option("lite")
}
create("kotlin")
}
}
}
}