代码之家  ›  专栏  ›  技术社区  ›  Emixam23

通过GRPC/Protobuf进行通信

  •  0
  • Emixam23  · 技术社区  · 7 年前

    我来这里是因为我无法在我的API(Go)和客户机(Android)之间进行通信。

    我有这个原始文件:

    syntax=“proto3”;
    
    选项java_package=“com.emixam23.rushpoc.protobuf”;
    选项java_outer_classname=“helloworld”;
    
    包装地狱世界;
    
    //问候语服务定义。
    服务迎宾员{
    //发送问候语
    rpc sayhello(hellorequest)返回(helloreply)
    }
    
    //包含用户名的请求消息。
    消息HelloRequest{
    字符串名称=1;
    }
    
    //包含问候语的响应消息
    消息helloreply{
    字符串消息=1;
    }
    < /代码> 
    
    

    protobuf文件来自https://grpc.io/docs/quickstart/go.html>的示例,我只是没有实现sayhelloagain。我正在尝试实现的是,从我的Android应用程序,向我的GoAPI问好,并得到回复…。

    对于Android,我遵循了该教程(https://grpc.io/docs/quickstart/android.html)以便从protobuf文件与我的API通信。但是,有一个stub,comng from I don't know where.

    因此,我搜索了如何创建存根(https://grpc.io/docs/tutorials/basic/android.html)和Nothing.。ManagedChannelBuilder不存在,我找不到安装它的方法。

    PS:从原BoFF文件生成Java类,我遵循该教程:

    我的方向是对的还是完全错了?

    我的项目结构:

    应用程序

    应用插件:“com.android.application” 应用插件:“com.google.protobuf” 机器人{ 编译dkversion 27 Debug TCONFIG { applicationID“com.rushpoc.emixam23.androidapp” Minsdk版本21 目标版本27 版本代码1 版本名“1.0” TestInstrumentRunner“android.support.test.runner.androidjunitrunner” } 构建类型{ 释放{ MinifyEnabled错误 proguardfiles getdefaultproguardfile('proguard-android.txt'),'proguard rules.pro' } } } 依赖项{ 实现文件树(dir:'libs',include:'*.jar']) 实现'com.android.support:appcompat-v7:27.1.1' 实现“com.android.support.constraint:constraint layout:1.1.2” “JUnit:JUnit:4.12”测试实施 Android测试实现'com.android.support.test:runner:1.0.2' android测试实施'com.android.support.test.espresso:espresso core:3.0.2' //PrimBuf 实现“com.google.protobuf:protobuf-lite:3.0.0” 实现“io.grpc:grpc okhttp:1.13.2” 实现“io.grpc:grpc protobuf lite:1.13.2” 实现“io.grpc:grpc stub:1.13.2” } 原Buff{ generatedfilesbasedir=“$projectdir/generated” 普罗托克 //在非Android情况下,您仍然需要Protoc artifact='com.google.protobuf:protoc:3.0.0' } 插件{ 贾瓦利特{ //Lite的codegen作为单独的工件提供 artifact='com.google.protobuf:protoc gen javalite:3.0.0' } GRPC { 伪像=“IO。GRPC:PrtoC GRPC Java:1.132” } } 生成协议任务{ 所有()。每个任务-> 任务集 爪哇 } 插件 GRPC {} } } } } < /代码>

    顶层/rootbuild.gradle

    //顶级构建文件,您可以在其中添加所有子项目/模块通用的配置选项。

    buildscript{
    ext.protobfversion='0.8.6'
    
    储存库{
    GoGoE()
    JCSENT()
    }
    依赖项{
    类路径'com.android.tools.build:gradle:3.1.3'
    classpath“com.google.protobuf:protobuf-gradle插件:$protobfversion”
    
    
    //注意:不要将应用程序依赖项放在此处;它们属于
    //在单个模块build.gradle文件中
    }
    }
    
    所有项目
    储存库{
    GoGoE()
    JCSENT()
    }
    }
    
    任务清理(类型:删除){
    删除rootproject.builddir
    }
    < /代码> <

    protobuf文件来自https://grpc.io/docs/quickstart/go.html我只是没能实现sayhelloagain。我想实现的是,从我的Android应用程序,向我的GoAPI问好,并得到回复…

    对于Android,我遵循了那个教程(https://grpc.io/docs/quickstart/android.html)以便从protobuf文件与我的API通信。但是,有一个stub我不知道在哪里。

    所以我搜索了如何创建存根(https://grpc.io/docs/tutorials/basic/android.html)什么都没有……ManagedChannelBuilder不存在,我找不到安装它的方法。

    PS:要从原始Bufff文件生成Java类,我遵循了该教程:https://proandroiddev.com/how-to-setup-your-android-app-to-use-protobuf-96132340de5c

    我的方向是对的还是完全错了?

    我的项目结构:

    enter image description here

    应用程序build.gradle

    apply plugin: 'com.android.application'
    apply plugin: 'com.google.protobuf'
    
    android {
        compileSdkVersion 27
        defaultConfig {
            applicationId "com.rushpoc.emixam23.androidapp"
            minSdkVersion 21
            targetSdkVersion 27
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:27.1.1'
        implementation 'com.android.support.constraint:constraint-layout:1.1.2'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    
        //Protobuf
        implementation 'com.google.protobuf:protobuf-lite:3.0.0'
    
        implementation 'io.grpc:grpc-okhttp:1.13.2'
        implementation 'io.grpc:grpc-protobuf-lite:1.13.2'
        implementation 'io.grpc:grpc-stub:1.13.2'
    }
    
    protobuf {
        generatedFilesBaseDir = "$projectDir/generated"
        protoc {
            // You still need protoc like in the non-Android case
            artifact = 'com.google.protobuf:protoc:3.0.0'
        }
        plugins {
            javalite {
                // The codegen for lite comes as a separate artifact
                artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
            }
            grpc {
                artifact = 'io.grpc:protoc-gen-grpc-java:1.13.2'
            }
        }
        generateProtoTasks {
            all().each { task ->
                task.builtins {
                    java
                }
                task.plugins {
                    grpc {}
                }
            }
        }
    }
    

    顶层/根平地

    //顶级生成文件,您可以在其中添加所有子项目/模块通用的配置选项。

    buildscript {
        ext.protobufVersion = '0.8.6'
    
        repositories {
            google()
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.1.3'
            classpath "com.google.protobuf:protobuf-gradle-plugin:$protobufVersion"
    
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            google()
            jcenter()
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   user675693    7 年前

    我还没有检查整个Gradle文件,但我在你的截图中看到.proto文件在 src/main/protobufs ,它没有遵循您提到的任何一个教程。Protobuf Gradle插件默认不检测此目录。因此,我建议您将其更改为默认目录 src/main/proto . 如果您坚持要将.proto文件放入 SRC/主/原型 ,您可能需要通过添加

    // see https://github.com/google/protobuf-gradle-plugin#customizing-source-directories    
    sourceSets {
      main {
        proto {
          // In addition to the default 'src/main/proto'
          srcDir 'src/main/protobufs'
        }
      }
    }
    

    之后,如果没有其他错误,OptoBuF分级插件将生成Java代码。