RSS

gRPC ❤️ Kotlin

您知道 gRPC Java 现在已对使用 Gradle 构建的 Kotlin 项目提供开箱即用支持吗?Kotlin 是由 JetBrains 开发的一种现代、静态类型语言,它面向 JVM 和 Android 平台。Kotlin 程序通常可以轻松地与现有 Java 库进行互操作。为了进一步改进这种体验,我们已向 protobuf-gradle-plugin 添加了支持,以便生成的 Java 库能被 Kotlin 自动识别。您现在可以将 protobuf-gradle-plugin 添加到您的 Kotlin 项目中,并像在典型的 Java 项目中一样使用 gRPC。

以下示例展示了如何为使用 Kotlin 的 JVM 应用和 Android 应用配置项目。

Kotlin gRPC 客户端和服务器

完整示例可在此处找到这里

为 Kotlin 项目配置 gRPC 与为 Java 项目配置相同。

以下是示例项目的 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/kotlinsrc/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 项目配置相同。

在顶层 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()
  }
}

并在 app 模块的 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 Android app example

我们很高兴能改善 Kotlin 开发者的 gRPC 使用体验。请将改进想法或错误报告添加到protobuf-gradle-plugin 问题跟踪器grpc-java 问题跟踪器