Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate gRPC code with protobuf on Kotlin for Android client?

I created a gRPC server (hosted by AWS) in nodejs and I can connect to it with a nodejs client implementation from my local machine.

I'm using the com.google.protobuf plugin to auto generate code from my .proto file. My gradle sync works and the app builds successfully but I can't find the generated code classes. I'm struggling to find a good implementation example for a Kotlin for Android gRPC client but I followed these articles:

  • gRPC In Kotlin(Android)
  • Java Basic Tutorial

My app/src/main/protos/responder.proto file

syntax = "proto3";

option java_package = "protos.grpc";
package responder;

message ConnectionRequest {
    int32 userId = 2;
}

message ConnectionResponse {
    string response = 1;
}

service ResponderService {
    rpc responderConnect (stream ConnectionRequest) returns (stream ConnectionResponse) {};
}

Project build.gradle

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

buildscript {
    ext {
        kotlin_version = '1.3.72'
        protobufPluginVersion = '0.8.6'
        grpcVersion = '1.12.0'
        protocVersion = '3.2.0'
    }
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.protobuf:protobuf-gradle-plugin:$protobufPluginVersion"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        def nav_version = "2.2.2"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

And my app's build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.protobuf'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "organisation.responder.two"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 31
        versionName "2.0"
        multiDexEnabled true

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }

}

dependencies {
    ...
    implementation 'io.grpc:grpc-okhttp:1.30.0'
    implementation 'io.grpc:grpc-protobuf-lite:1.30.0'
    implementation 'io.grpc:grpc-stub:1.30.0'
    implementation 'io.grpc:grpc-core:1.30.0'
    compileOnly 'org.apache.tomcat:annotations-api:6.0.53'

    def multidex_version = "2.0.1"
    implementation "androidx.multidex:multidex:$multidex_version"
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.12.0"
    }
    plugins {
        javalite {
            artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
        }
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:1.30.0"
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.plugins {
                javalite {}
                grpc {
                    // Options added to --grpc_out
                    option 'lite'
                }
            }
        }
    }
}

As I said above, the gradle sync is successful and the project builds successfully. But where is the autogenerated code?

like image 309
Daniel Avatar asked Jun 12 '20 08:06

Daniel


1 Answers

After following this build.gradle file as an example, I managed to generate the grpc code using the gradle protobuf plugin.

Major changes to my app level build.gradle file included:

  1. Adding path to .proto file in sourceSets block under android block.
android {
    ...
    sourceSets {
        main {
            proto {
                srcDir 'src/main/protos' <-- path to .proto file
            }
        }
    }
}
  1. Change the protobuf block in app level build.gradle file
protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.12.0"
    }
    plugins {
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:1.30.0"
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove javanano
                java {
                    option 'lite'
                }
            }

            task.plugins {
                grpc {
                    // Options added to --grpc_out
                    option 'lite'
                }
            }
        }
    }
}

After a successful sync and build, the generated code files where located under app/build/generated/source/proto/debug/grpc/protos/grpc/ResponderServiceGrpc.java and app/build/generated/source/proto/debug/java/protos/grpc/Responder.java

like image 96
Daniel Avatar answered Oct 22 '22 12:10

Daniel