Jetpack 原型数据存储 - gradle 使用 Kotlin dsl 配置
Jetpack proto datastore - gradle config with Kotlin dsl
在 Jetpack 数据存储中,您必须 set the gradle plugin task 从 .proto
个文件中生成 class:
// build.gradle
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.10.0"
}
// Generates the java Protobuf-lite code for the Protobufs in this project. See
// https://github.com/google/protobuf-gradle-plugin#customizing-protobuf-compilation
// for more information.
generateProtoTasks {
all().each { task ->
task.builtins {
java {
option 'lite'
}
}
}
}
}
在我的项目中,我将 Kotlin dsl 用于我的 gradle 项目。在尝试将其转换为 kotlin dsl 后,option
属性 是未知的,我找不到它是 kotlin kts
的替代品
// build.gradle.kts
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.10.0"
}
generateProtoTasks {
all().forEach { task ->
task.builtins {
java {
option = "lite" // ** option is unknown **
}
}
}
}
}
要使用 Jetpack Proto Datastore,请将以下代码用于 Gradle Kotlin DSL
// top of file
import com.google.protobuf.gradle.*
plugins {
id("com.google.protobuf") version "0.8.12"
// ...
}
val protobufVersion = "3.18.0"
dependencies {
// ...
implementation("com.google.protobuf:protobuf-javalite:$protobufVersion")
implementation("androidx.datastore:datastore:1.0.0-alpha03")
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:$protobufVersion"
}
generateProtoTasks {
all().forEach { task ->
task.plugins{
create("java") {
option("lite")
}
}
}
}
}
FWIW,接受的答案让我走上了正确的轨道,但在同步时失败并出现以下错误:
Unable to find method ''org.gradle.api.NamedDomainObjectContainer org.gradle.kotlin.dsl.NamedDomainObjectContainerExtensionsKt.invoke(org.gradle.api.NamedDomainObjectContainer, kotlin.jvm.functions.Function1)''
'org.gradle.api.NamedDomainObjectContainer org.gradle.kotlin.dsl.NamedDomainObjectContainerExtensionsKt.invoke(org.gradle.api.NamedDomainObjectContainer, kotlin.jvm.functions.Function1)'
所以我替换了
task.plugins{
create("java") {
option("lite")
}
}
和
task.plugins.create("java") {
option("lite")
}
它按预期工作。
你也可以使用内置函数。
import com.google.protobuf.gradle.*
generateProtoTasks {
all().forEach { task ->
task.builtins {
id("java") {
option("lite")
}
}
}
}
在 Jetpack 数据存储中,您必须 set the gradle plugin task 从 .proto
个文件中生成 class:
// build.gradle
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.10.0"
}
// Generates the java Protobuf-lite code for the Protobufs in this project. See
// https://github.com/google/protobuf-gradle-plugin#customizing-protobuf-compilation
// for more information.
generateProtoTasks {
all().each { task ->
task.builtins {
java {
option 'lite'
}
}
}
}
}
在我的项目中,我将 Kotlin dsl 用于我的 gradle 项目。在尝试将其转换为 kotlin dsl 后,option
属性 是未知的,我找不到它是 kotlin kts
// build.gradle.kts
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.10.0"
}
generateProtoTasks {
all().forEach { task ->
task.builtins {
java {
option = "lite" // ** option is unknown **
}
}
}
}
}
要使用 Jetpack Proto Datastore,请将以下代码用于 Gradle Kotlin DSL
// top of file
import com.google.protobuf.gradle.*
plugins {
id("com.google.protobuf") version "0.8.12"
// ...
}
val protobufVersion = "3.18.0"
dependencies {
// ...
implementation("com.google.protobuf:protobuf-javalite:$protobufVersion")
implementation("androidx.datastore:datastore:1.0.0-alpha03")
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:$protobufVersion"
}
generateProtoTasks {
all().forEach { task ->
task.plugins{
create("java") {
option("lite")
}
}
}
}
}
FWIW,接受的答案让我走上了正确的轨道,但在同步时失败并出现以下错误:
Unable to find method ''org.gradle.api.NamedDomainObjectContainer org.gradle.kotlin.dsl.NamedDomainObjectContainerExtensionsKt.invoke(org.gradle.api.NamedDomainObjectContainer, kotlin.jvm.functions.Function1)''
'org.gradle.api.NamedDomainObjectContainer org.gradle.kotlin.dsl.NamedDomainObjectContainerExtensionsKt.invoke(org.gradle.api.NamedDomainObjectContainer, kotlin.jvm.functions.Function1)'
所以我替换了
task.plugins{
create("java") {
option("lite")
}
}
和
task.plugins.create("java") {
option("lite")
}
它按预期工作。
你也可以使用内置函数。
import com.google.protobuf.gradle.*
generateProtoTasks {
all().forEach { task ->
task.builtins {
id("java") {
option("lite")
}
}
}
}