Search code examples
javaandroidkotlinandroid-jetpackandroid-jetpack-datastore

Is new Jetpack Datastore only for Kotlin?


I've read about new library from Jetpack (now in alpha) - Jetpack Datastore.

It's clear from documentation that it's a sort of Shared Preferences' killer

Jetpack DataStore is a data storage solution that allows you to store key-value pairs or typed objects with protocol buffers

DataStore uses Kotlin coroutines and Flow to store data asynchronously, consistently, and transactionally

If you're currently using SharedPreferences to store data, consider migrating to DataStore instead

If I don't miss anything you couldn't use this library in Java. Am I right? Personally I use Kotlin, but as for me it's a peculiar precedent for AndroidX library.


Solution

  • January 13, 2021

    Version 1.0.0-alpha06 was released. Support for RxJava 2/3 was added, so Datastore can be used in Java now.

    Added RxJava wrappers for DataStore. The datastore-rxjava2/3 artifacts contain the wrappers for the core DataStore APIs (RxDataStore, RxDataStoreBuilder, and RxDataMigration). The datastore-preferences-rxjava2/3 artifacts contain a builder to construct a Preferences DataStore.

    For that you should add dependencies:

    // optional - RxJava2 support
    implementation "androidx.datastore:datastore-rxjava2:1.0.0-alpha06"
    
    // optional - RxJava3 support
    implementation "androidx.datastore:datastore-rxjava3:1.0.0-alpha06"
    

    In addition now Datastore's official documentation holds code's examples' equivalents for Java.