Issue
First time using protobu and could not find an example of how to connect it to Kotlin Multiplatform. I created Multiplatform project Android/IOS with "shared" module where I need to use protobuf. Project structure:
Code in 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")
}
}
My build.gradle project 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
}
I tried to add path to my .proto files with sourceSets, but I got errors:
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")
}
}
When I try to build project without sourceSets, I didn't get generated java/kotlin files in build/generated/source - path
And my question is: "How setup protobuf in Android/IOS project Kotlin multiplatform?"
Solution
If you want to use ProtoBuf library in shared
module in common code, then you have to use library that supports Kotlin Multiplatform. You can take a look at official kotlinx.serialization
library, it supports ProtoBuf. And you can use it in commonMain
module.
Also take a look at community library
Or learn more about platform-specific implementations and use expect/actual
mechanism
Another option will be use your ProtoBuf library and plugin in androidApp
only and for iOS part use another iOS specific ProtoBuf library.
Answered By - Evgeny K
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.