Reputation: 33
I created HelloDataStore
at Android Studio Koala with gradle 8.7. I am new to learn Proto DataStore. I checked codelab for proto DataStore but failed. Then I checked nia (now in Android) app and found use protoc
and protobuf-kotlin-lite
4.26.1, I did follow codelabs insrtuctions to config my HelloDataStore
project but seems I cannot generate class file because I had defined user_prefs.proto
which details is below.
The UserPreferencesSerializer
object file cat NOT resolve UserPrefs
and build/generated/
have no source
folder. HelloDataStore
github branch here if you are interested.
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(:app)
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;
}
UserPreferencesSerializer
object UserPreferencesSerializer:Serializer<UserPrefs>
Upvotes: 0
Views: 53
Reputation: 33
proto
folder should be stored in src/main/proto
, not in src/main/java/proto
as I did before. I was reminded by a great person from this issues.
Upvotes: 0