How do I pass option to JavaPluginExtension using Kotlin DSL - android

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

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.

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.

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