如何使用版本为“androidx.datastore:datastore-core:1.0.0-beta01”的DataStore?

How to use DataStore with version "androidx.datastore:datastore-core:1.0.0-beta01”?

此版本 (beta01) 中缺少 DataStore 和 preferencesDataStore。

使用下面的依赖项

implementation("androidx.datastore:datastore-preferences:1.0.0-beta01")

为此,您需要:

plugins {
id("com.google.protobuf")
}

protobuf {
protoc {
    artifact = "com.google.protobuf:protoc:3.17.3"
}
generateProtoTasks {
    all().forEach { task ->
        task.builtins {
            create("java") {
                option("lite")
            }
        }
    }
}

implementation("androidx.datastore:datastore-core:1.0.0-beta01")
implementation("com.google.protobuf:protobuf-javalite:3.11.0")
implementation("androidx.preference:preference-ktx:1.1.1")

并初始化

    val data: DataStore<SomeMessage> = DataStoreFactory.create(
    serializer = SessionSerializer, // your Serializer
    corruptionHandler = null,
    migrations = emptyList(),
    scope = CoroutineScope(Dispatchers.IO + Job())

和以前一样继续

    data.updateData {
        it.toBuilder().setAddress("address").build()
    }

  data.collect { ChargingSession ->
            ChargingSession.address
    }