【问题标题】:How to setup protobuf in Android/IOS Kotlin Multiplatform project如何在 Android/IOS Kotlin Multiplatform 项目中设置 protobuf
【发布时间】:2022-11-23 02:37:52
【问题描述】:

第一次使用 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 项目等级: 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?”

【问题讨论】:

  • Kotlin 的“主要”原型实现不支持多平台。

标签: android ios kotlin protocol-buffers kotlin-multiplatform


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-10
    • 2020-12-08
    • 2021-05-13
    • 2023-02-10
    • 2021-02-01
    • 2022-11-23
    • 2021-04-07
    • 2022-10-05
    相关资源
    最近更新 更多