Android Studio中Grpc的配置
Grpc:是一个高性能、开源和通用的 RPC 框架,通过官方提供各种语言的实现包,我们可以快速接入到项目中。Android中接入的是grpc-java。
为了方便接入,在Android Studio内配置如下,即可自动编译proto文件生成相关的代码,然后直接调用即可。
apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "com.yuchenfw.myapplication"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0'
}
plugins {
javalite {
artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" //javalite版本
}
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.0.0' //grpc-java版本
}
}
generateProtoTasks {
all().each { task ->
task.plugins {
javalite {}
grpc {
// Options added to --grpc_out
option 'lite' //生成类型
}
}
}
}
}
}
buildscript {
repositories {
mavenCentral()
// maven {
mavenCentral()
// url 'https://repo1.maven.org/maven2'
// }
}
dependencies {
classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.1"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.+'
testCompile 'junit:junit:4.12'
//grpc need
compile 'io.grpc:grpc-okhttp:1.0.0'
compile 'io.grpc:grpc-protobuf-lite:1.0.0'
compile 'io.grpc:grpc-stub:1.0.0'
compile 'com.google.code.findbugs:jsr305:3.0.0'
compile 'com.google.guava:guava:20.0'
compile 'javax.annotation:javax.annotation-api:1.2'
}