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

3

第一次使用Protobuf,找不到如何将其连接到Kotlin Multiplatform的示例。我创建了一个Multiplatform项目Android/IOS,其中包含“shared”模块,我需要在其中使用protobuf。项目结构:

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 项目级别
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文件 - 路径

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


Kotlin的“main”原型实现不支持多平台。 - Louis Wasserman
当然可以,这是一个本地使用的示例(所以我决定尝试),我想要一个多平台的示例。 - Denys Voronets
我不确定我理解了。我的意思是说你不能让它适用于多平台。 - Louis Wasserman
哦,我误解了,抱歉。那么我将尝试使用序列化库。谢谢! 也许你有什么其他想法可以在多平台上使用protobuf? - Denys Voronets
1个回答

2
如果您想在通用代码的shared模块中使用ProtoBuf库,则必须使用支持Kotlin Multiplatform的库。您可以查看官方kotlinx.serialization,它支持ProtoBuf,并且您可以在commonMain模块中使用它。
还可以查看社区库
或者了解更多关于特定平台实现并使用expect/actual机制的信息。
另一个选择是仅在androidApp中使用您的ProtoBuf库和插件,而对于iOS部分则使用另一个特定于iOS的ProtoBuf库。

谢谢,我使用了社区库,它帮了我很大的忙!
  1. kotlinx.serialization库 - 处于实验状态,所以我不想使用它。
  2. 分离IOS/Android:如果第三方库无法工作,我将把它留在计划B上。
- Denys Voronets

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接