Hi folks, I want to know if in Kotlin 1.4 we can p...
# multiplatform
s
Hi folks, I want to know if in Kotlin 1.4 we can publish multiplatform libraries that uses only kotlin stdlib in common without needing to compile to every single platform
m
You mean compiling a Windows lib on MacOS for an exemple?
Or compiling a subset of targets ? (like only iOS)
d
You can compile it individually for each target
r
Libraries still need to declare what targets to use and build artifacts for each, even if the code is all common.
f
So, if my lib is only common and I want to make it compatible with everything I need to add all different platforms? Or there is an alias to that?
a
I heard this mentioned a few months ago and it was called "kotlin library format". It looks like 1.40 does not have this at least from what I could find so maybe the feature got delayed a bit. It sounds like internally it uses this to build each target but not exposed for re-use for devs at the moment. It was announced for "1.40 and beyond" so I guess it really means the beyond in this case.
m
As far as I know, you need to compile the different targets. i.e. you put all your code in
commonMain
but still need to declare your targets:
Copy code
kotlin {
   jvm()
   iOS()
   android()
   etc...
}
f
@Andrew when I started a project a few days ago, it was doing that (Here is an example of how it was https://github.com/faogustavo/fluks/blob/3877c22c3e34579a49daccd6c95480a53f9eb704/build.gradle.kts). Today I changed the default project structure to make more flexible to add other modules and control them apart from the lib, but the “nativeMain” and “nativeTest” sourcesets were not available anymore 😕
r
You can do something like
Copy code
kotlin {
  presets.forEach {
    targetFromPreset(it)  
  }
}
but you'll need to special-case some of them so it isn't that simple. In particular you need extra config to publish Android (but you might not want to have Android at all if you're pure Kotlin and JVM is sufficient), there's a KotlinJvmWithJavaTargetPreset you probably don't need unless you're including Java sources, and Javascript has targets for Legacy, IR, or Both and you can only pick one.
☝️ 1
f
I was looking on how coroutines does, they declare every platform they want to compile and for the android case that you said they use the jvm version (https://github.com/Kotlin/kotlinx.coroutines/blob/master/gradle/compile-native-multiplatform.gradle)
But they have an open PR to change and start following this graph, maybe it’s a better way to do it
r
That only matters if you need different code at those different levels.
f
but if I don’t declare the iOSArmX64 it will not work there when I import, right? If I want to make compatible, I have to declare the target