我是参考文档引入了依赖
implementation "androidx.datastore:datastore:1.0.0"
Run Code Online (Sandbox Code Playgroud)
然后定义架构app/src/main/proto/.proto
syntax = "proto3";
option java_package = "com.freedom.android.config.work";
option java_multiple_files = true;
message WorkItemVO {
bool enabled = 1;
string title = 2;
string repeat_interval = 3;
string repeat_interval_timeUnit = 4;
string last_update_time = 5;
string last_update_result = 6;
}
Run Code Online (Sandbox Code Playgroud)
之后app build,却build/generated/source/proto/没有生成WorkItemVOclass文件。
你能告诉我我错过了什么吗?
正如标题所提到的,有什么方法可以获取存储在 Android Jetpack Preferences DataStore中的所有密钥,例如,dataStore
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
Run Code Online (Sandbox Code Playgroud) 我正在努力在 Android Jetpack Compose 中实现 DataStore 首选项库,以在我的应用程序中保留一些用户设置。每当我尝试从可组合组件访问 SettingsViewModel 时,应用程序就会崩溃并收到以下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.boldmethod, PID: 5415
java.lang.RuntimeException: Cannot create an instance of class com.boldmethod.Models.SettingsViewModel
...
Caused by: java.lang.InstantiationException: java.lang.Class<com.packageName.Models.SettingsViewModel> has no zero argument constructor
Run Code Online (Sandbox Code Playgroud)
我正在按照文档创建数据存储和视图模型,所以也许我没有在可组合项中正确使用它们。这是相关的源代码:
摇篮
dependencies {
// Kotlin/Core
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.20"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
// Compose
implementation "androidx.compose.ui:ui:1.0.0-alpha08"
implementation "androidx.compose.material:material:1.0.0-alpha08"
implementation "androidx.compose.runtime:runtime:1.0.0-alpha08"
implementation "androidx.compose.runtime:runtime-livedata:1.0.0-alpha08"
implementation "androidx.compose.runtime:runtime-rxjava2:1.0.0-alpha08"
// Navigation
def nav_compose_version = "1.0.0-alpha03"
implementation "androidx.navigation:navigation-compose:$nav_compose_version"
// Architecture
implementation "androidx.datastore:datastore-preferences:1.0.0-alpha05"
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
//implementation "android.arch.lifecycle:runtime:2.1.0"
// UI/Material
implementation 'com.google.android.material:material:1.2.1'
implementation …Run Code Online (Sandbox Code Playgroud) android android-viewmodel android-jetpack android-jetpack-compose android-jetpack-datastore
SharedPreferences(尽管有很多缺点)的优点是可以访问存储数据的 XML。
Jetpack DataStore 在哪里存储数据以及文件是否可访问?
android android-preferences sharedpreferences android-jetpack-datastore
android ×3