未解析的引用:protoc -使用Gradle +协议缓冲区时

nuypyhwy  于 2023-04-06  发布在  其他
关注(0)|答案(3)|浏览(189)

我有一个使用KotlinDSL的Gradle项目

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;
}

和项目结构

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

每当我运行gradle build时,我都会得到错误:

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

我做的唯一与各种教程不同的事情是使用GradleKotlinDSL。我做错了什么?

isr3a4wc

isr3a4wc1#

我认为这是因为你在protobuf gradle插件中引用了一个单一的任务。尝试导入整个bundle以根据你的需要使用类型安全的访问器。

import com.google.protobuf.gradle.*
aemubtdh

aemubtdh2#

不管是什么原因,这是可行的

import com.google.protobuf.gradle.protoc

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

必须指定protobuf两次似乎有点难看

jgovgodb

jgovgodb3#

如果你使用的是Kotlin,在你的应用级kts gradle中,你需要根据google/protobuf-gradle-plugin从protobuf gradle import com.google.protobuf.gradle.*导入所有内容。

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

别忘了

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

您需要做的最后一项任务是将Protobuf类路径添加到您的项目级Gradle Kts中

buildscript {

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

相关问题