我正在尝试为grpc构建一个示例Android应用程序。
hello_world.proto文件如下:
syntax = "proto3";
package helloworld;
option java_multiple_files = true;
option java_package = "io.github.caio.grpc";
option java_outer_classname = "HelloWorldProto";
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloResponse) {}
rpc GetData (HelloRequest) returns (HelloResponse) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloResponse {
string message = 1;
}
gradle 文件如下。
应用级代码:
apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.grpctest.grpcandroidapp"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0-beta-2'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:0.12.0' // CURRENT_GRPC_VERSION
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
javanano {
// Options added to --javanano_out
option 'ignore_services=true'
}
}
task.plugins {
grpc {
// Options added to --grpc_out
option 'nano'
}
}
task.plugins {
grpc {
// Write the generated files under
// "$generatedFilesBaseDir/$sourceSet/grpcjava"
outputSubDir = ''
}
}
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'io.grpc:grpc-okhttp:0.12.0' // CURRENT_GRPC_VERSION
compile 'io.grpc:grpc-protobuf-nano:0.12.0' // CURRENT_GRPC_VERSION
compile 'io.grpc:grpc-stub:0.12.0' // CURRENT_GRPC_VERSION
compile 'javax.annotation:javax.annotation-api:1.2'
}
项目级别gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath "com.google.protobuf:protobuf-gradle-plugin:0.7.4"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
使用这个.proto文件,我们应该为生成的Java文件提供一个包结构,如下所示:
IO-> grpc-> examples->的HelloWorld
但生成的HelloRequest和HelloResponse文件采用以下包结构:
IO-> grpc-> examples-> helloworld->纳米
然而,GreeterGrpc.java文件是在正确的包结构中生成的。
GreeterGrpc中的“HelloRequest”和“HelloResposne”导入是指io.grpc.examples.helloworld包,因此编译失败。
请让我知道这个问题的解决方案。
答案 0 :(得分:0)
grpc-java 0.13及更早版本需要使用option 'nano=true'
。 grpc-java 0.14将另外支持option 'nano'
。