我在 Android Studio Koala 使用 gradle 8.7 创建了
HelloDataStore
。我是学习 Proto DataStore 的新手。我检查了 codelab 的原始数据存储但失败了。然后我检查了nia(现在在android中)应用程序并发现使用protoc
和protobuf-kotlin-lite
4.26.1,我确实按照codelabs说明配置了我的HelloDataStore
项目,但似乎无法生成类文件,因为我已经定义了 user_prefs.proto
详细信息如下。 UserPreferencesSerializer
目标文件猫无法解析 UserPrefs
和 build/generated/
没有 source
文件夹。如果您有兴趣,请HelloDataStore
github 分支这里。请帮帮我。
libs.versions.toml
protobufPlugin = "0.9.4"
#protobufPlugin = "0.9.0"
#protobufPlugin = "0.9.1"
protobuf = "4.26.1"
#protobuf = "3.21.12"
#protobuf = "3.17.3"
// ...
protobuf-kotlin-lite = { group = "com.google.protobuf", name = "protobuf-kotlin-lite", version.ref = "protobuf" }
protobuf-java-lite = { group = "com.google.protobuf", name = "protobuf-javalite", version.ref = "protobuf" }
protobuf-protoc = { group = "com.google.protobuf", name = "protoc", version.ref = "protobuf" }
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
protobuf = { id = "com.google.protobuf", version.ref = "protobufPlugin" }
build.gradle.kts(:应用程序)
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.protobuf)
}
android {
namespace = "com.wecanteen105.hellodatastore"
compileSdk = 35
defaultConfig {
applicationId = "com.wecanteen105.hellodatastore"
minSdk = 26
targetSdk = 35
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
viewBinding = true
}
}
// Setup protobuf configuration, generating lite Java and Kotlin classes
protobuf {
protoc {
// artifact = libs.protobuf.protoc.get().toString()
// artifact = "com.google.protobuf:protoc:3.19.4"
artifact = "com.google.protobuf:protoc:3.21.9"
}
generateProtoTasks {
all().forEach { task ->
task.builtins {
create("java") {
option("lite")
}
create("kotlin") {
option("lite")
}
}
}
}
}
//androidComponents.beforeVariants {
// android.sourceSets.register(it.name) {
// val buildDir = layout.buildDirectory.get().asFile
// java.srcDir(buildDir.resolve("generated/source/proto/${it.name}/java"))
// kotlin.srcDir(buildDir.resolve("generated/source/proto/${it.name}/kotlin"))
// }
//}
dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.androidx.constraintlayout)
implementation(libs.androidx.recyclerview)
implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.coroutines.android)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.lifecycle.livedata.ktx)
implementation(libs.androidx.lifecycle.viewModel.ktx)
implementation(libs.androidx.lifecycle.viewModel.savedstate)
implementation(libs.androidx.datastore.pref)
implementation(libs.androidx.datastore)
// implementation(libs.protobuf.java.lite)
implementation(libs.protobuf.kotlin.lite)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
}
user_prefs.proto
syntax = "proto3";
option java_package = "com.wecanteen105.hellodatastore";
option java_multiple_files = true;
message UserPrefs {
// filter for showing / hiding completed tasks
bool show_completed = 1;
}
用户首选项序列化器
object UserPreferencesSerializer:Serializer<UserPrefs>