grpc-kotlin icon indicating copy to clipboard operation
grpc-kotlin copied to clipboard

编译proto文件生成的类报错,Unresolved reference: HelloRequest

Open AngelRiven opened this issue 1 year ago • 4 comments
trafficstars

你好,你能帮我解决下面的问题吗,编译生成的代码文件Unresolved reference: HelloRequest。这只是其中一个错误,其他的message 也找不到。

下面是protobuf 配置

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:$protobufVersion"
    }

    plugins {
        create("grpc") {
            artifact = "io.grpc:protoc-gen-grpc-java:$grpcVersion"
        }
        create("grpckt") {
            artifact = "io.grpc:protoc-gen-grpc-kotlin:${grpcKotlinVersion}:jdk8@jar"
        }
    }

    generateProtoTasks {
        all().forEach {
            it.plugins {
                create("grpc")
                create("grpckt")
            }
            it.builtins {
                create("kotlin")
            }
        }
    }
}

这是proto文件

syntax = "proto3";

package helloworld;

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello(HelloRequest) returns (HelloReply) {}

  // Sends a greeting to a group
  rpc ClientStreamSayHello(stream HelloRequest) returns (HelloReply) {}

  // Sends a number of hellos
  rpc ServerStreamSayHello(MultiHelloRequest) returns (stream HelloReply) {}

  // Sends interactive hellos
  rpc BidiStreamSayHello(stream HelloRequest) returns (stream HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

message MultiHelloRequest {
  repeated string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

下面是结果:

5545e2db9dfe76789d9a2191f2698d5

AngelRiven avatar Jan 25 '24 06:01 AngelRiven

Does it work from Gradle and not IntelliJ?

jamesward avatar Jan 25 '24 20:01 jamesward

Does it work from Gradle and not IntelliJ?它是从Gradle而不是IntelliJ工作的吗?

是的

AngelRiven avatar Jan 26 '24 02:01 AngelRiven

Does it work from Gradle and not IntelliJ?它是从Gradle而不是IntelliJ工作的吗?

我把protobuf配置修改成下面这样,增加了java代码的生成,最终编译通过了。这个是必须要生成java代码的吗?

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:$protobufVersion"
    }

    plugins {
        create("grpc") {
            artifact = "io.grpc:protoc-gen-grpc-java:$grpcVersion"
        }
        create("grpckt") {
            artifact = "io.grpc:protoc-gen-grpc-kotlin:${grpcKotlinVersion}:jdk8@jar"
        }
        create("java"){
            artifact = "io.grpc:protoc-gen-grpc-java:$grpcVersion"
        }
    }

    generateProtoTasks {
        all().forEach {
            it.plugins {
                create("grpc"){
                    option("lite")
                }
                create("grpckt"){
                    option("lite")
                }
                create("java"){
                    option("lite")
                }
            }
            it.builtins {
                create("kotlin"){
                    option("lite")
                }
            }
        }
    }
}

AngelRiven avatar Jan 26 '24 02:01 AngelRiven

I believe that the Gradle Protobuf plugin is automatically adding the Java code gen based on something in the project configuration. In Android projects it doesn't see the configuration so you have to configure it manually like in: https://github.com/grpc/grpc-kotlin/blob/master/examples/stub-android/build.gradle.kts

jamesward avatar Jan 26 '24 22:01 jamesward