gRPC ❤ Kotlin
您知道gRPC Java现在已经为使用Gradle构建的Kotlin项目提供了开箱即用的支持吗?Kotlin是一种由JetBrains开发的现代、静态类型语言,它以JVM和Android为目标平台。Kotlin程序通常可以轻松地与现有Java库进行互操作。为了进一步改善这种体验,我们已向protobuf-gradle-plugin添加了支持,以便Kotlin能够自动识别生成的Java库。您现在可以将protobuf-gradle-plugin添加到您的Kotlin项目中,并像使用典型的Java项目一样使用gRPC。
原生Kotlin支持
正在寻找gRPC的原生Kotlin支持?请参阅Kotlin,邂逅gRPC。以下示例展示了如何为使用Kotlin的JVM应用程序和Android应用程序配置项目。
Kotlin gRPC 客户端和服务器
完整示例可在此处找到。
为Kotlin项目配置gRPC与为Java项目配置gRPC相同。
以下是示例项目的build.gradle
文件片段,其中突出显示了与Kotlin相关的一些部分
apply plugin: 'kotlin'
apply plugin: 'com.google.protobuf'
// Generate IntelliJ IDEA's .idea & .iml project files.
// protobuf-gradle-plugin automatically registers *.proto and the gen output files
// to IntelliJ as sources.
// For best results, install the Protobuf and Kotlin plugins for IntelliJ.
apply plugin: 'idea'
buildscript {
ext.kotlin_version = '1.2.21'
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.5'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
// The rest of the projects dep are added below, refer to example URL
}
// The standard protobuf block, same as normal gRPC Java projects
protobuf {
protoc { artifact = 'com.google.protobuf:protoc:3.5.1-1' }
plugins {
grpc { artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" }
}
generateProtoTasks {
all()*.plugins { grpc {} }
}
}
现在Kotlin源文件可以使用proto生成的消息和gRPC存根。默认情况下,Kotlin源文件应放置在src/main/kotlin
和src/test/kotlin
。如果需要,运行./gradlew generateProto generateTestProto
并刷新IntelliJ,以便生成的源文件显示在IDE中。最后,运行./gradlew installDist
来构建项目,然后使用./build/install/examples/bin/hello-world-client
或./build/install/examples/bin/hello-world-server
来运行示例。
您可以在此处阅读更多关于配置Kotlin的信息。
Kotlin Android gRPC 应用程序
完整示例可在此处找到。
为Kotlin Android项目配置gRPC与为普通Android项目配置gRPC相同。
在顶层build.gradle
文件中
buildscript {
ext.kotlin_version = '1.2.21'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.5"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
以及在应用程序模块的build.gradle
文件中
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.protobuf'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
// refer to full example for remaining deps
}
protobuf {
// The normal gRPC configuration for Android goes here
}
android {
// Android Studio 3.1 does not automatically pick up 'src/main/kotlin' as source files
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
与非Android项目一样,运行./gradlew generateProto generateProto
来运行proto代码生成器,并运行./gradlew build
来构建项目。
最后,通过在Android Studio中打开项目并选择Run > Run 'app'
来测试Android应用程序。


我们很高兴能改善Kotlin开发人员的gRPC体验。请将改进建议或错误报告提交至protobuf-gradle-plugin 问题跟踪器或grpc-java 问题跟踪器。