Configuring Protocol Buffers 0.9.x with Kotlin DSL - android

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")
}
}
}
}
}

Related

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.

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")
}
}
}
}
}

Jetpack proto datastore - gradle config with Kotlin dsl

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.

Compile time error while gradle build when using protobuf with android studio

While integrating protobuf into my android project, I am getting this error during gradle build:
Type com.google.protobuf.SourceContextOrBuilder is defined multiple times: /Users/kchaubal/.gradle/caches/transforms-2/files-2.1/4af300e60802588311eb01ecc52e7bea/jetified-protobuf-java-3.0.0.jar:com/google/protobuf/SourceContextOrBuilder.class, /Users/kchaubal/Dev/GitHubRepo/RPCExample/app/build/intermediates/javac/debug/classes/com/google/protobuf/SourceContextOrBuilder.class
This is how my grade file looks like
dependencies {
implementation 'io.grpc:grpc-okhttp:1.29.0'
protobuf 'io.grpc:grpc-protobuf-lite:1.29.0'
implementation 'io.grpc:grpc-stub:1.29.0'
implementation 'javax.annotation:javax.annotation-api:1.3.2'
implementation 'com.google.api.grpc:googleapis-common-protos:0.0.3'
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0'
}
plugins {
javalite {
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
}
grpc { artifact = 'io.grpc:protoc-gen-grpc-java:1.29.0'
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
remove java
}
task.plugins {
javalite { }
grpc { option 'lite' }
}
}
}
files("${protobuf.generatedFilesBaseDir}/main/proto")
}
I am a newbie with protobuf so any kind of help is much appreciated
dependencies {
...
protobuf 'com.google.protobuf:protobuf-java:3.7.1'
}

Categories

Resources