gpt4 book ai didi

java - Android 上的协议(protocol)生成的 Grpc 服务中的方法编码器错误

转载 作者:行者123 更新时间:2023-12-02 11:20:20 25 4
gpt4 key购买 nike

我有一个运行 grpc 端点的服务器,我想使用 Android 客户端连接到该端点。我正在尝试生成所需的 ..Grpc.java ,以便我可以使用 .newStub() 方法来创建 grpc 请求。

我有一个原始文件,其中包含如下服务:

service StorageNode {
rpc SayHello (StrWrapper) returns (StrWrapper) {}
}

message StrWrapper {
string message = 1;
}

我的 Android 应用 build.gradle 包含 protobuf 任务和依赖项,如下所示:

protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0'
}
plugins {
javalite {
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
}
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.9.0'
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
remove java
}
task.plugins {
javalite { }
grpc {
option 'lite'
}
}
}
}
}

dependencies {
...
compile 'com.google.protobuf:protobuf-lite:3.0.0'
compile 'io.grpc:grpc-okhttp:1.11.0'
compile 'io.grpc:grpc-protobuf-lite:1.11.0'
compile 'io.grpc:grpc-stub:1.11.0'
}

生成的 ProtoFileGrpc.java 文件在构建后包含错误,如下所示:

Error:(51, 64) error: method marshaller in class ProtoUtils cannot be applied to given types;
required: T
found: StrWrapper
reason: inferred type does not conform to upper bound(s)
inferred: StrWrapper
upper bound(s): Message
where T is a type-variable:
T extends Message declared in method <T>marshaller(T)

Error:(927, 40) error: cannot find symbol method getDescriptor()

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

最佳答案

问题似乎是因为 javax.annotation-api 未包含在依赖项中。这对于生成的 ...Grpc.java 似乎正在使用的 @javax.annotation.Generate 是必需的。

包括以下内容似乎已经解决了问题。

dependencies {
...
compile "javax.annotation:javax.annotation-api:1.2"
}

关于java - Android 上的协议(protocol)生成的 Grpc 服务中的方法编码器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49964390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com