【问题标题】:gRPC client: error in compiled file (io.grpc.protobuf does not exist)gRPC 客户端:编译文件出错(io.grpc.protobuf 不存在)
【发布时间】:2020-03-15 03:16:25
【问题描述】:

我用 Python 做了一个简单的 protobuf+gRPC 服务器/客户端示例,现在想用一个 Android Java 客户端来完成它。但是,我在文档中苦苦挣扎并使其大部分工作(.proto 在 Android Studio 中编译),但现在我在输出 *Grpc.java 文件中得到错误(即,如果我修复它,它只会被编译器覆盖):

error: package io.grpc.protobuf does not exist
error: package com.google.protobuf.Descriptors does not exist

由于我从 io.gprc 和 com.google 的 protobuf 中收到错误,我怀疑我的 gradle 中存在定义冲突,但我无法找到/解决它(经历了几个“教程”,混合使用 grpc 似乎很常见和谷歌资源)。

这是我的 build.gradle:


android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "***.grpcclient"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }


}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'io.grpc:grpc-okhttp:1.25.0'
    //implementation 'io.grpc:grpc-protobuf-lite:1.25.0'

    implementation 'com.google.protobuf:protobuf-javalite:3.8.0'
    implementation 'io.grpc:grpc-stub:1.25.0'
    implementation 'org.glassfish:javax.annotation:10.0-b28'
}

apply plugin: 'com.google.protobuf'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.10'
    }
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.8.0'
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.25.0'
        }
    }
    generateProtoTasks {
        all().each { task ->

                task.builtins {
                    java {
                        option "lite"
                    }
                    python { }
                }
                task.plugins {
                    grpc {outputSubDir = 'java'}
                }

        }
    }
}

感谢任何帮助!

【问题讨论】:

    标签: android protocol-buffers grpc protobuf-java


    【解决方案1】:

    Android 使用 Protobuf Lite,它是正常实现的子集,并针对 Android 进行了优化。您正确地尝试依赖io.grpc:grpc-protobuf-lite:1.25.0。这提供了io.grpc.protobuf.lite 包。

    您还正确配置了 protoc 以生成 protobuf-lite 消息。然而,grpc 插件正在生成“完整的”protobuf 服务。这就是为什么您会看到对 io.grpc.protobuf 的引用以及 protobuf 中缺少的类,例如 com.google.protobuf.Descriptors

    protobuf {
        ...
        generateProtoTasks {
            all().each { task ->
                    ...
                    task.plugins {
                        grpc {
                            outputSubDir = 'java'
                            option 'lite' // Needed this line
                        }
                    }
            }
        }
    }
    

    由于您使用带有 'lite' 选项的 protoc,而不是使用旧的 protoc-gen-javalite 插件,因此您正在使用正确的 protobuf-javalite protobuf 依赖项。但是,grpc-java 1.25.0 依赖于protobuf-lite,这将发生冲突。这在Issue 6405 中有一些讨论,并将在 grpc-java 1.26.0 中修复。但目前您需要排除 grpc-protobuf-lite 引入的 protobuf-lite 依赖项。

    dependencies {
        ...
        implementation ('io.grpc:grpc-protobuf-lite:1.25.0') {
            // Exclude will not be necessary starting in grpc 1.26
            exclude group: 'com.google.protobuf', module: 'protobuf-lite'
        }
    }
    

    【讨论】:

    • 谢谢埃里克!那工作得很好。我很高兴我走在了正确的轨道上,但 100 年后我永远不会猜到这一点……你是救生员!
    【解决方案2】:

    我的工作 build.gradle:

    
    dependencies {
    
       implementation 'com.squareup.okhttp:okhttp:2.7.5'
        implementation 'javax.annotation:javax.annotation-api:1.3.2'
        implementation 'io.grpc:grpc-core:1.27.1'
        implementation 'io.grpc:grpc-stub:1.27.1'
        implementation 'io.grpc:grpc-okhttp:1.27.1'
        implementation('io.grpc:grpc-protobuf-lite:1.27.1') {
            exclude module: "protobuf-lite"
        }
    }
    
    protobuf {
        protoc {
            artifact = 'com.google.protobuf:protoc:3.11.4'
        }
        plugins {
            grpc {
                artifact = 'io.grpc:protoc-gen-grpc-java:1.27.1'
            }
            javalite {
                artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
            }
        }
        generateProtoTasks {
            all().each { task ->
                task.builtins {
                    java {
                        option 'lite'
                    }
                }
    
                task.plugins {
                    grpc {
                        // Options added to --grpc_out
                        option 'lite'
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      相关资源
      最近更新 更多