android studio Kotlin中使用 GRPC和protobuf
gRPC Java现在对使用Gradle构建的Kotlin项目提供了开发和使用的支持。
//添加grpc的三个核心包
// You need to build grpc-java to obtain these libraries below.
implementation "io.grpc:grpc-okhttp:$CURRENT_GRPC_VERSION" // CURRENT_GRPC_VERSION 1.14.0-SNAPSHOT
implementation "io.grpc:grpc-protobuf-lite:$CURRENT_GRPC_VERSION" // CURRENT_GRPC_VERSION
implementation "io.grpc:grpc-stub:$CURRENT_GRPC_VERSION" // CURRENT_GRPC_VERSION
Kotlin是由JetBrains开发的一种现代的静态类型语言,面向JVM和Android。Kotlin程序通常很容易与现有的Java库进行互操作。
现在可以将protobuf-gradle-plugin添加到您的Kotlin项目中,并像使用典型的Java项目一样使用gRPC。
//添加protobuf-gradle-plugin
classpath "com.google.protobuf:protobuf-gradle-plugin:$protobufGP_version""
*以下示例说明如何使用Ko
tlin为JVM应用程序和Android应用程序配置项目。*
项目的app目录下的build.gradle 配置
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.protobuf'
android {
compileSdkVersion 28
defaultConfig {
applicationId "io.grpc.helloworldexample"
// API level 14+ is required for TLS since Google Play Services v10.2
minSdkVersion 14
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
buildTypes {
debug { minifyEnabled false }
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
disable 'GoogleAppIndexingWarning', 'HardcodedText', 'InvalidPackage'
textReport true
textOutput "stdout"
}
// Android Studio 3.1 does not automatically pick up '<src_set>/kotlin' as source input
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
main.java.srcDirs += 'src/test/kotlin'
androidTest.java.srcDirs += 'src/androidTest/kotlin'
}
lintOptions {
// Do not complain about outdated deps, so that this can javax.annotation-api can be same
// as other projects in this repo. Your project is not required to do this, and can
// upgrade the dep.
disable 'GradleDependency'
// The Android linter does not correctly detect resources used in Kotlin.
// See:
// - https://youtrack.jetbrains.com/issue/KT-7729
// - https://youtrack.jetbrains.com/issue/KT-12499
disable 'UnusedResources'
textReport true
textOutput "stdout"
}
}
protobuf {
protoc { artifact = 'com.google.protobuf:protoc:3.5.1-1' }
plugins {
javalite { artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" }
grpc { artifact = 'io.grpc:protoc-gen-grpc-java:1.13.1' // CURRENT_GRPC_VERSION
}
}
generateProtoTasks {
all().each { task ->
task.plugins {
javalite {}
grpc { // Options added to --grpc_out
option 'lite' }
}
}
}
}
def CURRENT_GRPC_VERSION='1.13.1'
dependencies {
implementation 'com.android.support:appcompat-v7:28.+'
implementation 'javax.annotation:javax.annotation-api:1.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
//使用grpc的三个核心包
// You need to build grpc-java to obtain these libraries below.
implementation "io.grpc:grpc-okhttp:$CURRENT_GRPC_VERSION" // CURRENT_GRPC_VERSION 1.14.0-SNAPSHOT
implementation "io.grpc:grpc-protobuf-lite:$CURRENT_GRPC_VERSION" // CURRENT_GRPC_VERSION
implementation "io.grpc:grpc-stub:$CURRENT_GRPC_VERSION" // CURRENT_GRPC_VERSION
}
repositories { mavenCentral() }
项目的项目(application) 目录下的build.gradle 配置
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.51'
repositories {
google()
jcenter()
mavenLocal()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.6"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
mavenLocal()
}
}
gRPC是基于Protobuf开发的RPC框架,简化了protobuf的开发,提供了服务端和客户端网络交互这一块的代码。
参考地址: https://github.com/grpc/grpc-java
demo下载地址:https://download.csdn.net/download/jeff_yaojie/10520064