Unresolved reference: protoc - when using Gradle + Protocol Buffers

Viewed 2254

I have a gradle project which uses the Kotlin DSL

build.gradle.kts

plugins {
    kotlin("jvm") version "1.4.21"
    id("com.google.protobuf") version "0.8.10"
}

group = "schema"
version = "0.0.1-SNAPSHOT"

repositories {
    mavenCentral()
}
protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.0.0"
    }
}

schema.proto

syntax = "proto3";
package schema;

message Message {
  string content = 1;
  string date_time = 2;
}

and project structure

schema
-src/main/proto/schema.proto
-build.gradle.kts

Whenever I run gradle build I get error:

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/x/schema/build.gradle.kts' line: 13

* What went wrong:
Script compilation errors:

   Line 15:      protoc {
        ^ Unresolved reference: protoc

   Line 16:              artifact = "com.google.protobuf:protoc:3.0.0"
         ^ Unresolved reference: artifact

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 8s

The only thing I am doing different to various tutorials is using the Gradle Kotlin DSL. What am I doing wrong?

2 Answers

I think this is happening because you are referencing a single task within the protobuf gradle plugin. Try importing the whole bundle to make use of type-safe accessors as you desired.

import com.google.protobuf.gradle.*

For whatever reason this works

import com.google.protobuf.gradle.protoc

protobuf {
    protobuf.protoc {
        artifact = "com.google.protobuf:protoc:3.0.0"
    }
}

It seems a little ugly having to specify protobuf twice mind

Related