https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
l

Luca

11/01/2023, 8:28 PM
anyone have any idea how I can do something like this in kotlin 1.9.20:
Copy code
object Targets {

    val iosTargets = arrayOf("iosArm64", "iosX64", "iosSimulatorArm64")
    val tvosTargets = arrayOf("tvosArm64", "tvosX64", "tvosSimulatorArm64")
    val watchosTargets = arrayOf(
        "watchosArm32", "watchosArm64", "watchosX64", "watchosSimulatorArm64", "watchosDeviceArm64"
    )
    val macosTargets = arrayOf("macosX64", "macosArm64")
    val darwinTargets = iosTargets + tvosTargets + watchosTargets + macosTargets
    val linuxTargets = arrayOf("linuxX64", "linuxArm64")
    val mingwTargets = arrayOf("mingwX64")
    val androidTargets = arrayOf(
        "androidNativeArm32", "androidNativeArm64", "androidNativeX86", "androidNativeX64",
    )
    val nativeTargets = linuxTargets + darwinTargets + mingwTargets + androidTargets

}

kotlin {
    for (target in Targets.nativeTargets) {
        targets.add(presets.getByName(target).createTarget(target))
    }
}
createTarget
is being depreciated and this has been useful for me to cut down boilerplate especially when I share
object Targets
across multiple modules
d

dsavvinov

11/02/2023, 3:42 PM
I don’t know the specifics of your case, but assuming that you don’t have any unexpected more complicated/nuanced scenarios aside from what’s outlined in your snippet, I’d suggest to convert arrays of strings to helper extension functions on `KotlinMultiplatformExtension`:
Copy code
object Targets {
    fun KotlinMultiplatformExtension.iosTargets() {
        iosArm64()
        iosSimulatorArm64()
        iosX64()
    }

    fun KotlinMultiplatformExtension.watchosTargets() {
        watchosArm32()
        watchosArm64()
        watchosX64()
        watchosSimulatorArm64()
        watchosDeviceArm64()
    }

    // ... etc ..

    fun KotlinMultiplatformExtension.allNativeTargets() {
        iosTargets()
        watchosTargets()
        // ...
    }
}

kotlin {
    with(Targets) { allNativeTargets() }
}
Of course, you can play with the specific organization/syntax to suit your liking (e.g., pass
KotlinMultiplatformExtension
as parameter instead of receiver, or omit the
Target
object to not have
with
-call, etc.) P.S. If you really need to operate with collections of strings, then you’ll have write a
when
which maps those strings to invocations of specific targets on
KotlinMultiplatformExtension
(see, for example, how Apollo Kotlin project does that) I
l

Luca

11/02/2023, 4:07 PM
@dsavvinov nice that looks like it would work well, thanks for the suggestion! I'll try that out. Also, I don't care about them being defined as strings, that was just the only way I figured out how to make it work before.
👍 1
@dsavvinov so while that does work for each file… I can’t figure out how to access KotlinMultiplatformExtension in buildSrc code to declare that so it can be shared among all submodules’ build.gradle
actually still can’t get build to work using compileonly
3 Views