Unresolved reference: protoc - when using Gradle + Protocol Buffers
Asked Answered
B

5

8

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?

Boanerges answered 21/12, 2020 at 10:6 Comment(1)
FWIW you will get similar errors in Kotlin DSL (e.g. Unresolved reference: id) unless you import everything explicitly, e.g. import com.google.protobuf.gradle.idDilute
E
25

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.*
Embarkation answered 7/1, 2021 at 3:57 Comment(0)
B
9

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

Boanerges answered 21/12, 2020 at 11:55 Comment(0)
I
2

This is works for me:

In project-level build.gradle.kts add this:

buildscript {
        dependencies {
           classpath("com.google.protobuf:protobuf-gradle-plugin:0.9.4")
        }
    }

In module-level build.gradle.kts add this:

plugins {
    ...        
            id("com.google.protobuf")
}
    
    android {
    ...
        protobuf {
            protoc {
                // find latest version number here:
                // https://mvnrepository.com/artifact/com.google.protobuf/protoc
                artifact = "com.google.protobuf:protoc:3.25.1"
            }
            generateProtoTasks {
                all().forEach { task ->
                    task.plugins{
                        create("java") {
                            option("lite")
                        }
                    }
                }
            }
        }
    }
    
    dependencies {
    ...
        implementation("androidx.datastore:datastore:1.0.0")
        implementation("com.google.protobuf:protobuf-javalite:3.25.1")
     }
Incredulity answered 19/12, 2023 at 7:9 Comment(1)
Please edit & elaborate what this adds or changes over @Wadia SALEM‘s answer.Skirmish
O
0

If you're still encountering the same problem, consider upgrading to the latest version of protobuf. I'm currently using plugin version of 0.9.4 and dependency version 4.27.0 , and it seems to have resolved the issue for me. You can view the latest version here:

Plugin: https://plugins.gradle.org/plugin/com.google.protobuf

Dependencies:https://repo1.maven.org/maven2/com/google/protobuf/protoc/

I have done the following in my projects. For your reference,

plugins {
id("com.google.protobuf") version "0.9.4"
}

dependencies {
implementation("com.google.protobuf:protobuf-javalite:4.27.0")
}

protobuf {
  protoc {
    artifact = "com.google.protobuf:protoc:4.27.0"
}
generateProtoTasks {
    all().forEach { task ->
        task.builtins {
            create("java") {
                option("lite")
            }
        }
    }
}
}
Oppen answered 27/5, 2024 at 5:12 Comment(0)
S
-1

If you are using Kotlin, in your app-level kts gradle you need to import all from protobuf gradle import com.google.protobuf.gradle.* depending on google/protobuf-gradle-plugin

import com.google.protobuf.gradle.*

plugins{
    ...
    id(""com.google.protobuf"")
}

android{
    ...
    protobuf {
        protoc {
           // find latest version number here:
           // https://mvnrepository.com/artifact/com.google.protobuf/protoc
            artifact = "com.google.protobuf:protoc:21.7"
        }
        generateProtoTasks {
            all().forEach { task ->
                task.plugins{
                    create("java") {
                        option("lite")
                    }
                    create("kotlin") {
                        option("lite")
                    }
                }
            }
        }
    }
}

And don't forget to implement

implementation("com.google.protobuf:protobuf-javalite:3.18.0")
implementation("com.google.protobuf:protobuf-kotlin-lite:3.18.0")
implementation("androidx.datastore:datastore:1.0.0")

The last task you need to do is to add the protobuf classpath to your project-level gradle kts

buildscript {

    ...
    dependencies {
        ...
        classpath("com.google.protobuf:protobuf-gradle-plugin:0.8.19")
    }
}
Sisely answered 5/4, 2023 at 14:35 Comment(2)
Didn't worked for meCompetitor
The version protoc:21.7 is like myth and source of compile errorSuch

© 2022 - 2025 — McMap. All rights reserved.