Hello everyone, In every KMM project there are external libaries at the initialization of the projec...
a
Hello everyone, In every KMM project there are external libaries at the initialization of the project. Among them also a dozen Kotlin/Native libaries. Can I use this?
โœ… 1
In the KMM Platfrom-print-example one of these Kotlin/Native libaries has been addressed.
import platform.UIKit.UIDevice
When I tried that, I only got errors. For examle: Why cant I use:
importplatform.UserNotifications.UNNotification
j
You should be able to use the platform libraries through the
platform.*
namespace. But only in the platform-specific code. https://kotlinlang.org/docs/native-objc-interop.html#name-translation
s
You can use some libraries using posix API on all platforms (except JS). For example zlib. Here is a sample how that looks like: https://github.com/RealAshampoo/kim/blob/main/src/posixMain/kotlin/com/ashampoo/kim/common/ZLib.kt If in your appleMain the UIDevice is not found you may have a macOS target. macOS has no UIDevice.
Kotlin/Native has the feature that it disables all API that is missing on one of the targets.
a
Thank you for your replies ๐Ÿ™‚ But isnt that showing, that is
platform.UserNotifications.UNNotification
is avalibile to use? My target should be iOS. I try to use it in iosMain.
s
How does your build.gradle.kts look like?
Do you have a macosX64() or macosArm64() in your kotlin{} block?
a
Neither ๐Ÿ˜Ÿ
Copy code
kotlin {
    android {
        compilations.all {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
    }
    iosX64()
    iosArm64()
    iosSimulatorArm64()
https://github.com/TheCragon/kmmDemo
s
Did you try if it works executed with Gradle even if it does not compile in IDEA?
Sometimes IDEA shows all red here for me, but it still works. Some kind of tooling problem.
a
oh, I haven't tried this yet. I will do. Thank you @Stefan Oltmann
s
I checked your project out. For IntelliJ IDEA 2023.1 CE the import works. ( But you need to use 7.4.0 instead of 8.0.0 as your Android Gradle Dependency )
I stopped using the latest versions of IDEA because the tooling around Kotlin Multiplatform easily brakes. When you find a stable version that works and you don't need features of a newer IDEA version consider stay with it.
Why do you have a separate iosArm64Main source folder? I don't think that you need that. You can combine them into one using this: https://github.com/RealAshampoo/kim/blob/e52e3a6ac0b9c412c35beef57e2661c9d32d0f0e/build.gradle.kts#L243-L246
a
Okay, thank you very much for checking ๐Ÿ˜€! To understand that right I can use all of Koltin/Native libaries?
s
You can use all Kotlin/Native libraries that the targets have in common. If you merge iosArm64Main and iosSimulatorArm64Main together like in the link above it does not change your ability to use UIDevice. If you add macosX64Main, too, you will only have access to everything iosArm64Main & macosX64Main have in common.
a
I understand ๐Ÿ™‚ Thank you very much
This was the hint I needed.
s
Yes, if you now have a stable configuration donโ€™t change it without good reason.
If I update dependencies I do it one by one with good testing between. More often than not something is broken because of incompatibility. You donโ€™t need always the latest Android grade plugin or coroutine version. That just costs a lot of time.
The best way to start a new project is to clone a template like KMM-production-sample or others and just keep the lib versions as they are because someone tested the compatibility already and you can focus and developing your mains app logic.