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

问题描述 投票:0回答:2

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

build.gradle中shared.module lvl中的代码 build.gradle shared.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")
    }
}

我的build.gradle项目lvl: build gradle project 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
}

我尝试使用 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")
    }
}

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

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

android ios kotlin protocol-buffers kotlin-multiplatform
2个回答
3
投票

如果你想在通用代码中的

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

还可以看看社区图书馆

或者了解更多关于特定于平台的实现并使用

expect/actual
机制

另一种选择是仅在

androidApp
中使用 ProtoBuf 库和插件,对于 iOS 部分,使用另一个 iOS 特定的 ProtoBuf 库。


0
投票

我找到了一个很好的解决方案,用来自 Squareup 的wire搜索答案,它提供了一种非常简单的方法来在 kmm 模块中添加 protobuf。

© www.soinside.com 2019 - 2024. All rights reserved.