简体   繁体   English

Protobuf gRPC - google protobuf package 不存在

[英]Protobuf gRPC - google protobuf package does not exist

I am trying to implement gRPC and now I'm having all sorts of issues, but I just don't get what I'm doing wrong.我正在尝试实现 gRPC,现在我遇到了各种各样的问题,但我只是不明白我做错了什么。 I am following this doc: https://github.com/grpc/grpc-java/blob/master/README.md我正在关注这个文档: https://github.com/grpc/grpc-java/blob/master/README.md

And now I keep getting such errors when I'm trying to build my project现在,当我尝试构建我的项目时,我不断收到这样的错误

error: package com.google.protobuf.GeneratedMessageV3 does not exist
com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements

In my Android Studio external libraries I have protobuf-java-3.12.1 jar .在我的 Android Studio 外部库中,我有protobuf-java-3.12.1 jar

In my project gradle file I've added this to dependencies:在我的项目 gradle 文件中,我已将其添加到依赖项中:

classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.14'

And in my app gradle file:在我的应用程序 gradle 文件中:

apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'

In dependencies I added:在依赖项中我添加了:

implementation 'io.grpc:grpc-okhttp:1.35.0'
implementation 'io.grpc:grpc-protobuf-lite:1.35.0'
implementation 'io.grpc:grpc-stub:1.35.0'
compileOnly 'org.apache.tomcat:annotations-api:6.0.53'
implementation 'com.google.protobuf:protobuf-javalite:3.12.1'

And outside of the android tag:在 android 标签之外:

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.12.1"
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.35.0'
        }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

Finally, my proto file:最后,我的原型文件:

syntax = "proto3";
import "google/protobuf/timestamp.proto"; 
option java_package = "com.xxx.xxx.proto.log";
option java_outer_classname = "MyClass";

message MyObject {
    string name = 1;
    string unit = 2;

    oneof value {
         bool bool_value = 3;
         sint32 int32_value = 4;
         uint32 u_int32_value = 5;
         google.protobuf.Timestamp timestamp_value = 6;
    }
}   

When I run: protoc --version in terminal, this is the output:当我在终端运行: protoc --version 时,这是 output:

libprotoc 3.12.1

Do I have to add something else or I missed something in my Gradle setup?我是否必须添加其他内容,或者我在 Gradle 设置中遗漏了某些内容?

It looks like you are hoping to use protobuf lite (as is normal for Android projects):看起来您希望使用 protobuf lite(对于 Android 项目来说是正常的):

implementation 'io.grpc:grpc-protobuf-lite:1.35.0'
...
implementation 'com.google.protobuf:protobuf-javalite:3.12.1'

However, currently you are generating full protobuf code.但是,目前您正在生成完整的 protobuf 代码。 You need to tell the java and grpc-java code generators to generate for lite via options.您需要通过选项告诉 java 和 grpc-java 代码生成器为 lite 生成。 As seen in the grpc-java Android helloworld example :grpc-java Android helloworld 示例中所示:

protobuf {
    ...
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java { option 'lite' }
            }
            task.plugins {
                grpc { option 'lite' }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM