代码之家  ›  专栏  ›  技术社区  ›  Sudheesh Singanamalla

Android上protoc生成的Grpc服务中的方法Marshaller错误

  •  0
  • Sudheesh Singanamalla  · 技术社区  · 8 年前

    我有一台服务器运行 grpc 我想使用android客户端连接到的端点。我正在尝试生成所需的 ..Grpc.java 这样我就可以使用 .newStub() 用于创建 grpc公司 要求

    我有一个proto文件,其中包含以下服务:

    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.
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Sudheesh Singanamalla    8 年前

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

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

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