Android Studio下GRPC 配置步骤
1.src/main/proto 添加test.proto文件
2.moduel的build.gradle 中添加protobuffer 插件
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.6'
- 3.app下build.gradle 中添加
...
apply plugin: 'com.google.protobuf'
...
def grpcVersion = '1.18.0' // CURRENT_GRPC_VERSION
def protocVersion = '3.5.1-1'
...
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:${protocVersion}"
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
}
}
generateProtoTasks {
all().each {
task ->
task.builtins {
remove java
}
task.builtins {
java {}
// Add cpp output without any option.
// DO NOT omit the braces if you want this builtin to be added.
cpp {}
}
}
all()*.plugins { grpc {} }
}
generatedFilesBaseDir = "$projectDir/src/generated"
}
packagingOptions {
pickFirst 'META-INF/INDEX.LIST'
pickFirst 'META-INF/LICENSE'
pickFirst 'META-INF/io.netty.versions.properties'
}
...
...
//proto buffer
implementation "io.grpc:grpc-protobuf:${grpcVersion}"
implementation "io.grpc:grpc-stub:${grpcVersion}"
compileOnly "javax.annotation:javax.annotation-api:1.2"
implementation "io.grpc:grpc-netty:${grpcVersion}"
4.在src/main目录添加自己的.proto文件 具体写法可以参考google的Demo,复制一个过来用也可以;
5.记得申请网络权限
6.rebuild 编译,可能需要一定时间下载资源,如果上述步骤都正确,会在src/generated 目录下生成对应代码
注意:编译可能出现第三方插件问题,但是不影响项目运行;