如何使用 Kotlin 将 gRPC 添加到 Android Studio?

Adi*_*vie 9 android gradle kotlin grpc grpc-kotlin

任务

我需要使用 gRPC 将 Android 客户端与 python 服务器连接。在 Python 中制作服务器和生成原型很容易,但 Kt 客户端缺乏教程和令人困惑的文档使其显得极其复杂。

背景

到目前为止,我已经使用 Kotlin 制作了一些简单的 Android 应用程序,我习惯了向模块或应用程序级别的 build.gradle 添加依赖项。

我尝试了什么?

我的第一个想法是像使用 Python 一样查阅官方文档。我发现那里的指南非常令人困惑(我觉得那篇文章缺少一些东西),所以我去他们的GitHub上查看完整的示例。我还克隆了存储库并使用gradlew installDist命令编译了原型。然后事情变得非常复杂:

  • 当你创建一个 Android Studio 项目时,你会得到一堆 gradle 的东西(模块和应用程序级别的 build.gradle、gradlew 和 gradlew.bat、设置等)
  • 克隆存储库后,您会在 grpc-kotlin 文件夹中获得另一堆 gradle 内容。
  • 您还可以获得 build.gradle.kts,它似乎是相同的构建逻辑/包管理器帮助程序文件,但具有其他依赖项和 Kotlin 脚本语法。

当时我去 YouTube 上寻找一个简单的实现,结果发现关于gRPC 和 Kotlin主题的视频屈指可数,而且大多数都是关于在 Kotlin 中使用协程时介绍 gRPC 特性的视频。

我到现在为止所拥有的

我将所有 build.gradle 迁移到.kts。这就是我的模块级build.gradle.kts的样子:

buildscript {
    val kotlin_version = "1.5.10"
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:4.2.1")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}")
        classpath("org.jetbrains.kotlin:kotlin-android-extensions:${kotlin_version}")
        classpath("com.google.gms:google-services:4.3.8")
        classpath ("com.google.protobuf:protobuf-gradle-plugin:0.8.14")

    }
}

allprojects {
    repositories {
        google()
        mavenCentral()

    }
}

tasks.register("clean",Delete::class){
    delete(rootProject.buildDir)
}
Run Code Online (Sandbox Code Playgroud)

这就是我的应用程序级别build.gradle.kts的样子:

import com.google.protobuf.gradle.generateProtoTasks
import com.google.protobuf.gradle.id
import com.google.protobuf.gradle.plugins
import com.google.protobuf.gradle.protobuf
import com.google.protobuf.gradle.protoc

plugins {
    id("com.android.application")
    id("com.google.protobuf")
    kotlin("android")
}

android {
    compileSdkVersion(30)
    buildToolsVersion = "30.0.3"

    defaultConfig {
        applicationId = "com.example.myapplication"
        minSdkVersion(26)
        targetSdkVersion(30)
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
        }
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    buildFeatures {
        viewBinding = true
    }
}

protobuf {
    protoc { artifact = "com.google.protobuf:protoc:3.12.0" }
    plugins {
        id("grpc") {
            artifact = "io.grpc:protoc-gen-grpc-java:1.35.0"
        }
    }
    generateProtoTasks {
        all().forEach { task ->
            task.plugins.create("java") {
                option("lite")
            }
            task.plugins {
                id("grpc") {
                    this.option("lite")
                }

            }
        }


    }
}

dependencies {

    val kotlin_version = "1.5.10"
    implementation("org.jetbrains.kotlin:kotlin-stdlib:${kotlin_version}")
    implementation("androidx.core:core-ktx:1.5.0")
    implementation("androidx.appcompat:appcompat:1.3.0")
    implementation("com.google.android.material:material:1.3.0")
    implementation("androidx.constraintlayout:constraintlayout:2.0.4")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.2")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")

    // GRPC Deps
    implementation("io.grpc:grpc-okhttp:1.37.0")
    implementation("io.grpc:grpc-protobuf-lite:1.37.0")
    implementation("io.grpc:grpc-stub:1.36.0")
    implementation("org.apache.tomcat:annotations-api:6.0.53")
}

Run Code Online (Sandbox Code Playgroud)

我可以生成原型,但它们有些不对劲。

问题

在实现请求函数(分别是双向流)时,我发现我的所有 rpc 函数都需要额外的 StreamObserver 参数(我在互联网上找到的所有教程中都没有该参数)。仔细一看,我发现所有生成的文件都是java格式的,并且在官方文档上,生成的文件都是POJO和Kotlin。

这是我生成的存根类的样子:

buildscript {
    val kotlin_version = "1.5.10"
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:4.2.1")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}")
        classpath("org.jetbrains.kotlin:kotlin-android-extensions:${kotlin_version}")
        classpath("com.google.gms:google-services:4.3.8")
        classpath ("com.google.protobuf:protobuf-gradle-plugin:0.8.14")

    }
}

allprojects {
    repositories {
        google()
        mavenCentral()

    }
}

tasks.register("clean",Delete::class){
    delete(rootProject.buildDir)
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何为我的项目复制 gradle 脚本,我发现互联网上没有人解释所有这些 build.gradle 是如何链接在一起的(我发现模块级别 build.gradle 正在描述它们所在的模块)应该构建和应用程序级别的 build.gradle 是同上,但对于整个应用程序)。我找到的大部分文章和官方文档是一样的。

我想要的是

我只想要一个简单的项目或一步一步的教程,没有“克隆它并在终端中运行命令,它就可以工作”。

我不会责怪开发人员或编写官方文档的人,我实际上打赌我是这里最愚蠢的人,但我很难理解这些概念,如果有人能向我解释我做错了什么或在哪里学习,我将不胜感激。

另外,很抱歉这个长问题,我试图尽我所能地暴露我的观点,这是我开始学习编程以来的第二个问题,如果问题和我的目标不够明确,我很抱歉,我会编辑任何东西,如果需要的话。

小智 2

我没有可以分享的分步过程,也不想轻视一个提出得很好的问题。然而,我想回应的是,在研究类似问题时,我发现 Square 有一个似乎对 Kotlin 更友好的库:

https://square.github.io/wire/#wire-kotlin