0

I have an existing project which is using gradle with kotlin DSL. I have setup protobuf code auto-generation with gradle build.

Kotlin grpc code is generated and I am able to implement my backend using flows and coroutines.

But the kotlin DSL code is not getting generated. So I am currently left with usage of verbose boilerplate driven builder pattern for creating my objects.

Below is my protobuf plugin setup.

protobuf {

//    generatedFilesBaseDir = "generated-sources"
    protoc {
        artifact = "com.google.protobuf:protoc:3.17.3"
    }
    plugins {
        id("grpc") {
            artifact = "io.grpc:protoc-gen-grpc-java:1.39.0"
        }

        id("grpckt") {
            artifact = "io.grpc:protoc-gen-grpc-kotlin:1.2.0:jdk7@jar"
        }
    }
    generateProtoTasks {
        ofSourceSet("main").forEach {
            it.plugins {
                id("grpc") {}
                id("grpckt") {}
            }
        }
    }
}

Am I missing something here?

Gopinath
  • 12,981
  • 6
  • 36
  • 50

1 Answers1

1

After a lot of searching the Internet for an answer, I have found one from here

Looks like grpc implementers have changed the way the plugin is configured. So answering my own question so it might save some time for the next developer.

protobuf {

    protoc {
        artifact = "com.google.protobuf:protoc:3.17.3"
    }
    plugins {
        id("grpc") {
            artifact = "io.grpc:protoc-gen-grpc-java:1.39.0"
        }

        id("grpckt") {
            artifact = "io.grpc:protoc-gen-grpc-kotlin:1.2.0:jdk7@jar"
        }
    }
    generateProtoTasks {
        ofSourceSet("main").forEach {
            it.plugins {
                id("grpc") {}
                id("grpckt") {}
            }

            it.builtins {
                id("kotlin")
            }
        }
    }
}

The key is adding this block. Then my protobuf messages' have kotlin DSL functions generated.

            it.builtins {
                id("kotlin")
            }

After configuring Kotlin DSL generation, compilation was failing. Then I had to add protobuf-kotlin dependency.

implementation("com.google.protobuf:protobuf-kotlin:3.19.1")
Gopinath
  • 12,981
  • 6
  • 36
  • 50