简体   繁体   English

gRPC 客户端:编译文件出错(io.grpc.protobuf 不存在)

[英]gRPC client: error in compiled file (io.grpc.protobuf does not exist)

I made a simple protobuf+gRPC server/client example with Python, and wanted now to complete it with an Android Java client.我用 Python 做了一个简单的 protobuf+gRPC 服务器/客户端示例,现在想用 Android Java 客户端完成它。 However, I struggled through the documentation and got it mostly working (.proto compiles in Android Studio), BUT now I get Errors in the output *Grpc.java file (ie if I fix it it will just be overwritten by the compiler): However, I struggled through the documentation and got it mostly working (.proto compiles in Android Studio), BUT now I get Errors in the output *Grpc.java file (ie if I fix it it will just be overwritten by the compiler):

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

Since I get errors from io.gprc's and com.google's protobuf, I suspect a definition conflict in my gradle, but I cannot find/resolve it (went through several "tutorials" and it seemed common to use a mix of grpc and google sources). Since I get errors from io.gprc's and com.google's protobuf, I suspect a definition conflict in my gradle, but I cannot find/resolve it (went through several "tutorials" and it seemed common to use a mix of grpc and google sources )。

Here is my build.gradle:这是我的 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'}
                }

        }
    }
}

Any help is appreciated!任何帮助表示赞赏!

Android uses Protobuf Lite, which is a subset of the normal implementation and optimized for Android. Android 使用 Protobuf Lite,它是正常实现的子集,并针对 Android 进行了优化。 You correctly tried to depend on io.grpc:grpc-protobuf-lite:1.25.0 .您正确地尝试依赖io.grpc:grpc-protobuf-lite:1.25.0 This provides the io.grpc.protobuf.lite package.这提供了io.grpc.protobuf.lite package。

You also correctly configured protoc to generate protobuf-lite messages .您还正确配置了 protoc 以生成 protobuf-lite消息 However, the grpc plugin is generating "full" protobuf services .但是,grpc 插件正在生成“完整”的 protobuf服务 That's why you're seeing references to io.grpc.protobuf and classes missing in protobuf like com.google.protobuf.Descriptors .这就是为什么您会看到对io.grpc.protobuf的引用以及 protobuf 中缺少的类,例如com.google.protobuf.Descriptors

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

Since you are using protoc with the 'lite' option, instead of using the older protoc-gen-javalite plugin, you are using the correct protobuf-javalite dependency for protobuf.由于您使用带有 'lite' 选项的 protoc,而不是使用旧的 protoc-gen-javalite 插件,因此您使用的是 protobuf 的正确protobuf-javalite依赖项。 However, grpc-java 1.25.0 depends on protobuf-lite which will collide.但是,grpc-java 1.25.0 依赖于protobuf-lite会发生冲突。 This is discussed some in Issue 6405 and will be fixed in grpc-java 1.26.0.这在Issue 6405中有一些讨论,并将在 grpc-java 1.26.0 中修复。 But for the moment you'll need to exclude the protobuf-lite dependency brought in by grpc-protobuf-lite.但目前您需要排除 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'
    }
}

My working build.gradle:我的工作 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'
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM