grpc-kotlin
grpc-kotlin copied to clipboard
编译proto文件生成的类报错,Unresolved reference: HelloRequest
你好,你能帮我解决下面的问题吗,编译生成的代码文件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;
}
下面是结果:
Does it work from Gradle and not IntelliJ?
Does it work from Gradle and not IntelliJ?它是从Gradle而不是IntelliJ工作的吗?
是的
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")
}
}
}
}
}
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