首页 > 解决方案 > 在 Android 上使用最新的 gRPC 插件时出现汇编错误 -> 输入在 --proto_path (Gradle 7.0.1) 中隐藏

问题描述

我正在尝试从当前使用的 3.6.4 更新 Android 项目以使用最新的 gradle 插件(7.0.1)。为了做到这一点,考虑到项目正在使用 protobuf,我需要更新 protobuf 和 gRPC 依赖项,因为当前的依赖项与最新的插件不兼容。

我已关注https://github.com/grpc/grpc-java以使用最新的依赖版本。我将依赖项更新为以下版本:

implementation 'io.grpc:grpc-okhttp:1.40.1'
implementation 'io.grpc:grpc-protobuf-lite:1.40.1'
implementation 'io.grpc:grpc-stub:1.40.1'
compileOnly 'org.apache.tomcat:annotations-api:6.0.53'
protobuf "com.google.protobuf:protobuf-java:3.17.3"

我正在使用最新的 protobuf 插件

plugins {
    id 'com.google.protobuf' version '0.8.17'
}

并将以下块用于代码生成

protobuf {
  protoc {
    artifact = "com.google.protobuf:protoc:3.17.3"
  }
  plugins {
    grpc {
      artifact = "io.grpc:protoc-gen-grpc-java:1.40.1"
    }
  }
  generateProtoTasks {
    all().each { task ->
      task.builtins {
        java { option 'lite' }
      }
      task.plugins {
        grpc { option 'lite' }
      }
    }
  }
}

使用这些时 gradle 同步成功,问题是当我尝试组装项目时,出现以下错误:

任务 ':App:generateDebugProto' 执行失败。协议:标准输出:。标准错误:C:\Users\phantom\AndroidStudioProjects\Protobuf\App\build\extracted-protos\main\google\protobuf\any.proto:输入在 --proto_path 中被“C:/Users/phantom/AndroidStudioProjects/ Protobuf/App/build/extracted-include-protos/debug/google/protobuf/any.proto”。要么使用后一个文件作为您的输入,要么重新排序 --proto_path 以便前一个文件的位置排在第一位。

根据我在阅读错误时的理解,问题是现在在提取的原型提取的包含原型构建文件中都生成了 proto 文件,而后者掩盖了第一个。我检查过,在以前的版本中,这些文件仅在提取的原型构建文件中生成。有没有办法跳过在提取的包含原型中生成文件?或者能够组装该项目的行动方案是什么?

标签: androidandroid-gradle-pluginprotocol-buffersgrpcgrpc-java

解决方案


我昨天遇到了同样的问题。这更像是一种解决方法,而不是完整的答案。它让我使用谷歌语音到文本,但如果你添加非测试版的文本到语音,它就不起作用,所以如果有人有更好的答案,请发布。

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.17.3'
    }
    plugins {
        javalite {
            artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
        }
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:1.40.1"
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                // In most cases you don't need the full Java output
                // if you use the lite output.
                remove java
            }
            task.plugins {
                javalite {}
                grpc {
                    // Options added to --grpc_out
                    option 'lite'
                }
            }
        }
    }
}

implementation 'io.grpc:grpc-okhttp:1.40.1'
implementation 'io.grpc:grpc-protobuf-lite:1.25.0'
implementation 'io.grpc:grpc-stub:1.40.1'
compileOnly 'org.apache.tomcat:annotations-api:6.0.53'
protobuf "com.google.protobuf:protobuf-java:3.17.3"

implementation("com.google.cloud:google-cloud-speech:1.22.1") {
    exclude group: 'com.google.protobuf', module: 'protobuf-java'
    exclude group: 'com.google.api.grpc'
}

注意 grpc-protobuf-lite 和 google-cloud-speech 的版本。我不得不从最新的降级他们两个。


推荐阅读