【问题标题】:Communication through gRPC/Protobuf通过 gRPC/Protobuf 进行通信
【发布时间】:2018-07-29 20:55:24
【问题描述】:

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

我有这个 protobuf 文件:

syntax = "proto3";

option java_package = "com.emixam23.rushpoc.protobuf";
option java_outer_classname = "HelloWorld";

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

protobuf 文件来自https://grpc.io/docs/quickstart/go.html 的例子,我只是没有实现 SayHelloAgain。我想要实现的是,从我的 android 应用程序 SayHello 到我的 Go API 并得到回复......

对于 android,我遵循该教程 (https://grpc.io/docs/quickstart/android.html) 以便从 protobuf 文件与我的 API 进行通信。不过,有一个stub,不知从哪里传来的。

所以我搜索了如何创建存根 (https://grpc.io/docs/tutorials/basic/android.html) 却一无所获.. ManagedChannelBuilder 不存在,我找不到安装它的方法..

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

我的方向是正确的还是完全错误的?

我的项目结构:

APP 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 {}
            }
        }
    }
}

顶级/根 build.gradle

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

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
}

【问题讨论】:

    标签: android protocol-buffers protobuf-net grpc


    【解决方案1】:

    我还没有检查整个 gradle 文件,但我在您的屏幕截图中看到 .proto 文件位于 src/main/protobufs 中,它没有遵循您提到的任何一个教程。 protobuf gradle 插件默认不检测这个目录。所以我建议你把它改成默认目录src/main/proto。如果您想坚持将 .proto 文件放在 src/main/protobufs 中,您可能需要通过添加

    让 protobuf gradle 插件知道它
    // 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'
        }
      }
    }
    

    之后,如果没有其他错误,protobuf gradle插件会生成java代码。

    【讨论】:

    • 是的,你是对的,这是错误之一,但我从来没有注意到它应该是 'src/main/proto'
    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多