0
votes

I'd like to have 3 java applications (a backend, a front end, and an Android app) using protocol buffers (gRPC) to communicate. So I would like the 3 apps all to be able to have access to a shared protobuf repo (Github) where I manage the .proto files. I am new to using Gradle and protobufs, so I'm not sure what the proper way to manage this is, and any help or guidance would be appreciated. Can I have each Gradle project declare my github protobuf repo as a dependency, and then pull it down and compile it when I build the project? I would assume this way would be a good way to do it, rather than storing the compiled protobuf classes, since the Android app might need a different "Java-lite" version of the protobufs? I am using the google/protobuf-gradle-plugin to compile the .proto files, and see documentation for compile from local files, or pulling in projects that have precompiled .proto files, but no documentation for pulling in remote .proto files. Am I on the right track?

1

1 Answers

3
votes

In what form is your remote .proto file/repo? If it is just a url, then you can use Download task:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
    }
}

plugins {
    id "de.undercouch.download" version "3.2.0"
}

group 'testtest'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'com.google.protobuf'


sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

task downloadFile << {
    download {
        src 'https://raw.githubusercontent.com/grpc/grpc-java/master/compiler/src/test/proto/test.proto'
        dest "$projectDir/src/main/proto/test.proto"
        overwrite true
    }
}

build.dependsOn downloadFile

dependencies {
    compile "io.grpc:grpc-protobuf-lite:1.5.0"
    compile "io.grpc:grpc-stub:1.5.0"
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.3.0'
    }
    plugins {
        javalite {
            artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
        }
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:1.5.0"
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.plugins {
                javalite {}
                grpc {
                    option 'lite'
                }
            }
        }
    }
}