如何在 Android/IOS Kotlin 多平台项目中设置 protobuf

Den*_*ets 6 android protocol-buffers ios kotlin kotlin-multiplatform

第一次使用 protobu,找不到如何将其连接到 Kotlin Multiplatform 的示例。我创建了带有“共享”模块的多平台项目 Android/IOS,我需要在其中使用 protobuf。项目结构:

build.gradle中的代码shared.module lvl build.gradle共享.module lvl

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

kotlin {
    android()
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "shared"
        }
    }

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
            }

        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val androidMain by getting
        val androidTest by getting
        val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosX64Main.dependsOn(this)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
        }
        val iosX64Test by getting
        val iosArm64Test by getting
        val iosSimulatorArm64Test by getting
        val iosTest by creating {
            dependsOn(commonTest)
            iosX64Test.dependsOn(this)
            iosArm64Test.dependsOn(this)
            iosSimulatorArm64Test.dependsOn(this)
        }
    }
}


android {
    namespace = "com.example.someproject"
    compileSdk = 33
    defaultConfig {
        minSdk = 21
        targetSdk = 33
    }

    protobuf {
        protoc {
            artifact = ("com.google.protobuf:protoc:3.21.9")
        }
        generateProtoTasks {
            all().forEach { task ->
                task.builtins {
                    java {
 //                   option 'lite' //Error - Unresolved reference: option
                    }
                    kotlin {
//                    option 'lite' //Error - Unresolved reference: option
                    }
                }
            }
        }
    }
    dependencies {
        implementation("com.google.protobuf:protobuf-javalite:3.21.9")
    }
}
Run Code Online (Sandbox Code Playgroud)

我的build.gradle项目lvl: 构建gradle项目lvl

buildscript {
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.9.1'
    }
}
plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
    id 'com.google.protobuf' version '0.9.1' apply false
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用 sourceSets 添加 .proto 文件的路径,但出现错误:

android {
    namespace = "com.example.someproject"
    compileSdk = 33
    defaultConfig {
        minSdk = 21
        targetSdk = 33
    }
    sourceSets {
        main { //Error - Unresolved reference: main
            proto { //Error - Unresolved reference: proto
                srcDir 'src/main/protobuf' //Error - Unresolved reference: srcDir
            }
            java {
                srcDirs 'build/generated/source/proto/main/java' //Error - Unresolved reference: srcDirs
                srcDirs 'build/generated/source/proto/main/kotlin' //Error - Unresolved reference: srcDirs
            }
        }
    }

    protobuf {
        protoc {
            artifact = ("com.google.protobuf:protoc:3.21.9")
        }
        generateProtoTasks {
            all().forEach { task ->
                task.builtins {
                    java {
 //                   option 'lite' //Error - Unresolved reference: option
                    }
                    kotlin {
//                    option 'lite' //Error - Unresolved reference: option
                    }
                }
            }
        }
    }
    dependencies {
        implementation("com.google.protobuf:protobuf-javalite:3.21.9")
    }
}

Run Code Online (Sandbox Code Playgroud)

当我尝试在没有 sourceSets 的情况下构建项目时,我没有在 build/ generated/source -路径中生成 java/kotlin 文件

我的问题是:“如何在 Android/IOS 项目 Kotlin 多平台中设置 protobuf?”

Evg*_*y K 4

如果你想在shared通用代码的模块中使用 ProtoBuf 库,那么你必须使用支持 Kotlin Multiplatform 的库。你可以看一下官方kotlinx.serialization,它支持ProtoBuf。您可以在commonMain模块中使用它。

还可以看看社区图书馆

或者了解更多平台特定的实现和使用expect/actual机制

另一种选择是androidApp仅使用 ProtoBuf 库和插件,对于 iOS 部分,使用另一个 iOS 特定的 ProtoBuf 库。