简体   繁体   中英

Communication through gRPC/Protobuf

I am coming because I can't make work my communication between my API (Go) and my client (Android).

I have this protobuf file:

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;
}

The protobuf file comes from the example of https://grpc.io/docs/quickstart/go.html , I just didn't implemented the SayHelloAgain. What i am trying to achieve is, from my android app, SayHello to my Go API and get a reply...

For android, I followed that tutorial ( https://grpc.io/docs/quickstart/android.html ) in order to, from the protobuf file, to communicate with my API. However, there is a stub , comming from I don't know where.

So I searched about how to create a stub ( https://grpc.io/docs/tutorials/basic/android.html ) and nothing.. ManagedChannelBuilder doesn't exist and I can't find the way to install it..

PS: to generate my Java class from the protobuf file, I followed that tutorial: https://proandroiddev.com/how-to-setup-your-android-app-to-use-protobuf-96132340de5c

Am I in the right direction or totally wrong?

My project structure:

在此处输入图片说明

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

TOP-LEVEL/Root build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

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
}

I haven't checked the entire gradle files yet but I see in your screenshot the .proto file was in src/main/protobufs , which was not following either of the tutorials you mentioned. The protobuf gradle plugin does not detect this directory by default. Therefore I suggest you change it into the default directory src/main/proto . If you would like to insist putting the .proto file in src/main/protobufs , you might need let the protobuf gradle plugin know it by adding

// 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'
    }
  }
}

After that, the protobuf gradle plugin will generate the java code if there's no other mistake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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