Hello everyone. I'm developing a library for Kotli...
# kotlin-native
m
Hello everyone. I'm developing a library for Kotlin/Native only. When I publish the library to Maven, two libraries are produced,
library_name
and
library_name-native
. Is this how it is supposed to work or should I be publishing only
library_name
? I've seen this: https://kotlinlang.org/docs/multiplatform-publish-lib.html#avoid-duplicate-publications But I don't fully understand if that fits my use-case or if it's just for kmm. Thanks in advance!
a
yes, it’s expected that Kotlin Multiplatform projects always publish one ‘common’ artifact, and one artifact per target
for example, if you look at the publications for Okio then you’ll see loads of variants https://repo1.maven.org/maven2/com/squareup/okio/
m
I see, thank you Adam!
a
no problem!
by the way, are you using some conditional logic to switch the Kotlin Native target based on the current operating system? Some code that looks like this:
Copy code
kotlin {
   when (currentHost) {
        KonanTarget.LINUX_X64 -> linuxX64("native", configure)
        KonanTarget.MACOS_X64 -> macosX64("native", configure)
        KonanTarget.MINGW_X64 -> mingwX64("native", configure)
        else -> error("unsupported host '${currentHost.name}'")
    }
}
m
I am indeed:
Copy code
val nativeTarget = when {
        hostOs == "Mac OS X" -> macosX64("native")
        hostOs == "Linux" -> linuxX64("native")
        isMingwX64 -> mingwX64("native")
        else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
a
I ask because I see you said your library ends with
-native
. And so if you were using that conditional trick, then the published library would be randomly changing depending on which machine was used to publish the library. The trick is usually used for testing an executable library locally.
m
I see. I should probably remove that then, thank you
a
it’s a good trick for if you want to have a subproject for testing things locally, but for publishing it’s best to just publish all targets. Newer versions of Kotlin will automatically disable publishing of targets that are unsupported by the current machine. (Currently macOS is the only OS that can build all targets)
m
Ah, that makes sense!
Newer versions of Kotlin will automatically disable publishing of targets that are unsupported by the current machine
Out of curiosity, does this mean in order to publish an ios library, you'd have to publish it on a mac machine?
a
yes, that’s right
m
Interesting, thanks for all the info!
a
no problem!
for publishing my KMP libraries I use a GitHub Action, because that has macOS machines, so I can just use one workflow to publish all targets. I’m sure there are other options too
m
Right, I assumed! For now only linux, but I'll keep that in mind 😉
e
it takes a bit more work to set up but to save on github action minutes (since mac is more expensive), you can build almost everything on linux and only the apple targets on mac
s
Agreed. The Mac runner with factor 10 is really expensive and I have an complex GitHub workflow to save my company as many minutes as possible. 👀 Luckily for the open source projects build minutes are free and I let just run the one task that builds all run on macOS. 😅
s
```kotlin {
when (currentHost) {
KonanTarget.LINUX_X64 -> linuxX64("native", configure)
KonanTarget.MACOS_X64 -> macosX64("native", configure)
KonanTarget.MINGW_X64 -> mingwX64("native", configure)
else -> error("unsupported host '${currentHost.name}'")
}
}```
I really, really advise not to use such configurations anymore. They were outdated in 2021 already.