Hi, I am trying to define build types (debug/relea...
# multiplatform
a
Hi, I am trying to define build types (debug/release) for my kmp library using the latest kotlin multiplatform plugin, but am not able to do so. • I have already tried by specifying the following:
Copy code
kotlin {
    ...
    androidTarget {
        publishLibraryVariantsGroupedByFlavor = false
        publishLibraryVariants("release", "debug")
    }
    ...
}
but this does not work. (
The target 'android' already exists, but it was not created with the 'android' preset
) • I have even tried manually applying the
com.android.library
alongside the existing multiplatform library plugin (
com.android.kotlin.multiplatform.library
) to configure the build variants manually, but that throws the error that both plugins cant be used together. Attaching my build.gradle.kts for reference:
Copy code
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidKotlinMultiplatformLibrary)
    alias(libs.plugins.androidLint)
    alias(libs.plugins.composeMultiplatform)
    alias(libs.plugins.composeCompiler)
    alias(libs.plugins.composeHotReload)
    id("org.jetbrains.kotlin.plugin.serialization")
}

compose.resources {
    publicResClass = true
    generateResClass = always
}

kotlin {
    androidLibrary {
        namespace = "com.sample.library"
        compileSdk = 36
        minSdk = 21

        androidResources {
            enable = true
        }

        compilerOptions {
            jvmTarget.set(JvmTarget.JVM_17)
        }

        withHostTestBuilder {
        }

        withDeviceTestBuilder {
            sourceSetTreeName = "test"
        }.configure {
            instrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        }
    }

    val xcfName = "composeAtomicKit"
    iosX64 {
        binaries.framework {
            baseName = xcfName
        }
    }
    iosArm64 { .. }
    iosSimulatorArm64 { ..}

    jvm()

    js {
        browser()
        binaries.executable()
    }

    @OptIn(ExperimentalWasmDsl::class)
    wasmJs {
        browser()
        binaries.executable()
    }

    sourceSets {
        commonMain {
            dependencies { .. }
        }

        androidMain {
            dependencies {
                ..
            }
        }

        iosMain { ... }
    }

}
e
com.android.kotlin.multiplatform.library does not support variants, https://developer.android.com/kotlin/multiplatform/plugin#features
😢 1
p
Variants don't scale well
a
So are there any alternatives for that?
p
Basically functionality abstraction. Make your code rely on an interface, then implement that interface for each flavor in different modules. And decide what module to include in the App at build time. Check this https://github.com/pablichjenkov/macao-marketplace/blob/dev/composeApp%2Fbuild.gradle.kts#L14
z
By the way, there's a tracking issue for Kotlin Multiplatform projects to have variants: https://youtrack.jetbrains.com/issue/KT-33432/MPP-build-variant-feature It would be really useful if you could add a comment there describing what your use case is for needing variants.
👍 2
p
Flavors are challenging specially when you have many libraries, bc unit test has to run per flavor, so it adds build time. Plus managing the publication and consumption of libraries is verbose and confusing
If I could give some feedback to the KMP team. It would be nice to have something similar to the concept of iOS targets. Looks similar to flavors but it scales better
a
We cant define multiple
android library
targets, right? that would solve the problem. I saw that we can do multiple targets for jvm("targetName").