RSS

gRPC ❤ Kotlin

您是否知道 gRPC Java 现在对使用 Gradle 构建的 Kotlin 项目提供了开箱即用的支持?Kotlin 是一种由 JetBrains 开发的现代静态类型语言,目标是 JVM 和 Android。Kotlin 程序通常很容易与现有的 Java 库进行互操作。为了进一步改善这种体验,我们向 protobuf-gradle-plugin 添加了支持,以便 Kotlin 可以自动获取生成的 Java 库。现在您可以将 protobuf-gradle-plugin 添加到您的 Kotlin 项目中,并像使用典型的 Java 项目一样使用 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/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 项目配置 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()
  }
}

在 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 问题跟踪器