datastore

Asynchronous, consistent, transactional storage replacing SharedPreferences; supports preferences and typed objects with Proto/JSON serialization, optional Rx adapters, and atomic updates.

Android JVMJVMKotlin/NativeWasmJS
GitHub stars5900
Authorsandroidx
Open issues
LicenseApache License 2.0
Creation datealmost 6 years ago

Last activity5 days ago
Latest release1.3.0-alpha06 (5 days ago)

DataStore

Store data asynchronously, consistently, and transactionally, overcoming some of the drawbacks of SharedPreferences

[User Guide]

[API Reference]

Declaring dependencies

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:

Preferences DataStore

Add the following lines to the dependencies part of your gradle file:

Kotlin

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")
}
Groovy
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:

Kotlin

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")
}
Groovy
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"
}

DataStore

Add the following lines to the dependencies part of your gradle file:

Kotlin

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")
}
Groovy
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:

Kotlin

dependencies {
    // optional - RxJava2 support
    implementation("androidx.datastore:datastore-rxjava2:1.2.0")

    // optional - RxJava3 support
    implementation("androidx.datastore:datastore-rxjava3:1.2.0")
}
Groovy
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.

JSON serialization

To use JSON serialization, add the following to your Gradle file:

Kotlin
plugins {
    id("org.jetbrains.kotlin.plugin.serialization") version "2.2.20"
}

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
}
Groovy
plugins {
    id("org.jetbrains.kotlin.plugin.serialization") version "2.2.20"
}

dependencies {
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0"
}

Protobuf serialization

To use Protobuf serialization, add the following to your Gradle file:

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")
            }
        }
    }
}
Groovy
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")
            }
        }
    }
}

Issue tracker

Issue Tracker

Android JVMJVMKotlin/NativeWasmJS
GitHub stars5900
Authorsandroidx
Open issues
LicenseApache License 2.0
Creation datealmost 6 years ago

Last activity5 days ago
Latest release1.3.0-alpha06 (5 days ago)

DataStore

Store data asynchronously, consistently, and transactionally, overcoming some of the drawbacks of SharedPreferences

[User Guide]

[API Reference]

Declaring dependencies

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:

Preferences DataStore

Add the following lines to the dependencies part of your gradle file:

Kotlin

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")
}
Groovy
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:

Kotlin

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")
}
Groovy
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"
}

DataStore

Add the following lines to the dependencies part of your gradle file:

Kotlin

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")
}
Groovy
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:

Kotlin

dependencies {
    // optional - RxJava2 support
    implementation("androidx.datastore:datastore-rxjava2:1.2.0")

    // optional - RxJava3 support
    implementation("androidx.datastore:datastore-rxjava3:1.2.0")
}
Groovy
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.

JSON serialization

To use JSON serialization, add the following to your Gradle file:

Kotlin
plugins {
    id("org.jetbrains.kotlin.plugin.serialization") version "2.2.20"
}

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
}
Groovy
plugins {
    id("org.jetbrains.kotlin.plugin.serialization") version "2.2.20"
}

dependencies {
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0"
}

Protobuf serialization

To use Protobuf serialization, add the following to your Gradle file:

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")
            }
        }
    }
}
Groovy
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")
            }
        }
    }
}

Issue tracker

Issue Tracker

Survey iconComplete survey to improve klibs.io ↗
Let’s go