Jetpack proto datastore - gradle config with Kotlin dsl - android

In jetpack datastore, you have to set the gradle plugin task for generating class out of .proto files:
// 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'
}
}
}
}
}
In my project I use Kotlin dsl for my gradle project. After trying to convert this to kotlin dsl, option property is unknown and I can't find it's alternative for 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 **
}
}
}
}
}

To use Jetpack Proto Datastore use the following code for 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")
}
}
}
}
}

You can use builtins too.
import com.google.protobuf.gradle.*
generateProtoTasks {
all().forEach { task ->
task.builtins {
id("java") {
option("lite")
}
}
}
}

FWIW, the accepted answer put me on the right track but failed when syncing with the following error:
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)'
So I replaced
task.plugins{
create("java") {
option("lite")
}
}
with
task.plugins.create("java") {
option("lite")
}
and it worked as expected.

Related

Configuring Protocol Buffers 0.9.x with Kotlin DSL

I've updated the protocol buffers version from 0.8.x to 0.9.1 on my Android project and I'm getting a gradle sync error. I have removed the imports from the build.gradle.kts file as per the documentation, and I'm getting an error as shown below.
This is the code in build.gradle.kts:
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:21.0-rc-1"
}
generateProtoTasks {
all().forEach { task ->
task.plugins {
create("java") {
option("lite")
}
}
}
}
}
and this is the error, which points to the create("java") { line - how can I configure protocol buffers using the Kotlin DSL?
Caused by: org.gradle.api.UnknownDomainObjectException: ExecutableLocator with name 'java' not found.
at org.gradle.api.internal.DefaultNamedDomainObjectCollection.createNotFoundException(DefaultNamedDomainObjectCollection.java:504)
at org.gradle.api.internal.DefaultNamedDomainObjectCollection.getByName(DefaultNamedDomainObjectCollection.java:333)
at com.google.protobuf.gradle.GenerateProtoTask$_getAllExecutableLocators_closure8.doCall(GenerateProtoTask.groovy:354)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at com.google.protobuf.gradle.GenerateProtoTask.getAllExecutableLocators(GenerateProtoTask.groovy:354)
at com.google.protobuf.gradle.GenerateProtoTask.getReleaseExecutableLocators(GenerateProtoTask.groovy:350)
at com.google.protobuf.gradle.GenerateProtoTask.access$0(GenerateProtoTask.groovy)
at com.google.protobuf.gradle.GenerateProtoTask$_getExecutables_closure6.doCall(GenerateProtoTask.groovy:342)
at com.google.protobuf.gradle.GenerateProtoTask$_getExecutables_closure6.call(GenerateProtoTask.groovy)
at org.gradle.api.internal.provider.DefaultProvider.calculateOwnValue(DefaultProvider.java:72)
at org.gradle.api.internal.provider.AbstractMinimalProvider.get(AbstractMinimalProvider.java:83)
at org.gradle.api.internal.provider.ProviderResolutionStrategy$2.resolve(ProviderResolutionStrategy.java:33)
at org.gradle.api.internal.file.collections.ProviderBackedFileCollection.visitDependencies(ProviderBackedFileCollection.java:56)
at org.gradle.api.internal.tasks.CachingTaskDependencyResolveContext$TaskGraphImpl.getNodeValues(CachingTaskDependencyResolveContext.java:103)
at org.gradle.internal.graph.CachingDirectedGraphWalker$GraphWithEmptyEdges.getNodeValues(CachingDirectedGraphWalker.java:213)
at org.gradle.internal.graph.CachingDirectedGraphWalker.doSearch(CachingDirectedGraphWalker.java:121)
at org.gradle.internal.graph.CachingDirectedGraphWalker.findValues(CachingDirectedGraphWalker.java:73)
at org.gradle.api.internal.tasks.CachingTaskDependencyResolveContext.getDependencies(CachingTaskDependencyResolveContext.java:66)
... 141 more
I needed to change it as shown below
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:21.0-rc-1"
}
generateProtoTasks {
all().forEach { task ->
task.builtins {
create("java") {
option("lite")
}
}
}
}
}

grpc-kotlin: Unresolved reference

I try to compile proto definitions into kotlin stubs, but get import issues. My gradle is
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.21.2"
}
plugins {
grpckt {
artifact = "io.grpc:protoc-gen-grpc-kotlin:1.3.0:jdk8#jar"
}
}
generateProtoTasks {
all().forEach { task ->
task.builtins {
grpckt {
option 'lite'
}
}
task.plugins {
kotlin {
option 'lite'
}
}
}
}
}
dependencies {
//grpc dependency
implementation("io.grpc:grpc-kotlin-stub:1.3.0")
implementation 'io.grpc:grpc-okhttp:1.47.0'
implementation("io.grpc:grpc-protobuf:1.47.0")
implementation("com.google.protobuf:protobuf-kotlin:3.21.12")
implementation 'io.grpc:grpc-protobuf-lite:1.47.0'
}
I have few proto files. After proto compiled I get the files with import errors:
option java_multiple_files = true - doesn`t help.

how can I go from groggy to kotlin Dsl protobuf

I am applying Proto DataStore and I am applying protobuf in my Kotlin Dsl gradle but I can't implement it correctly. enter the code here.
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("kotlin-kapt")
id("dagger.hilt.android.plugin")
id ("com.google.protobuf") version "0.8.12"
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.14.0"
}
generateProtoTasks {
all().each { task ->
task.builtins {
java {
option 'lite'
}
}
}
}
}
thank you very much!.
So I could adjust it in Gradle in kotlin
plugins {
id ("com.google.protobuf") version ("0.8.12")
}
protobuf {
protoc {
artifact = if (osdetector.os == "osx") {
("com.google.protobuf:protoc:3.14.0:osx-x86_64")
} else {
("com.google.protobuf:protoc:3.14.0")
}
}
generateProtoTasks {
all().forEach {
it.builtins {
create("java") {
option("lite")
}
}
}
}
}
In this way configure the gradle for proto DataStore

Grpc protoc configuration cannot upgrade to latest version with gradle 7.0 but possible in gradle 4.2

This is my protoc setup in build.gradle which works in gradle 4.2
protobuf {
protoc { artifact = 'com.google.protobuf:protoc:3.10.0' }
plugins {
javalite { artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" }
grpc { artifact = 'io.grpc:protoc-gen-grpc-java:1.25.0' // CURRENT_GRPC_VERSION
}
}
generateProtoTasks {
all().each { task ->
task.plugins {
javalite {}
grpc { // Options added to --grpc_out
option 'lite' }
}
}
}
}
How is it not building in only gradle 7.0?
The protoc setup for as described in the current documentation is:
protobuf {
protoc { artifact = 'com.google.protobuf:protoc:3.12.0' }
plugins {
grpc { artifact = 'io.grpc:protoc-gen-grpc-java:1.36.0' // CURRENT_GRPC_VERSION
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
java { option 'lite' }
}
task.plugins {
grpc { // Options added to --grpc_out
option 'lite' }
}
}
}
}
The main difference from what you have is the lack of definition in task.plugins and setting mode 'lite' and using the grpc-java library and not the java-lite.

How do I pass option to JavaPluginExtension using Kotlin DSL

I'm having issue converting these gradle groovy code to kotlin dsl.
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.8.0'
}
generateProtoTasks {
all().each { task ->
task.builtins {
java {
option "lite"
}
}
}
}
}
Especially the option "lite" in java block.
Thanks.
You can try this with Kotlin DSL.
build.gradle.kts
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.17.3"
}
generateProtoTasks {
all().forEach {
it.builtins {
create("java") {
option("lite")
}
}
}
}
}

Categories

Resources