I am currently developing an Android/iOS app that will use some older non platfrom specific libs written in C++ and exposed with extern C for usage in cinterop.
I have created the .klib and it runs just fine when compiled as .kexe. The problem is that i can not find the way how to include this .klib in Kotlin multipartform mobile project. I would want it to be accessible from commonMain so i could add some additional logic and bussines stuff above it.
I went trough posts like this:
But i cant get to import my “custom” lib in commonMain in any way. It is plain C code no system or platform specific calls.
No it’s not possible. You can only call C-code from kotlin-native (or from kotlin-jvm using something like jni). It’s not something you can do from common code or js.
I would be happy if anyone can correct me but right now it is not possible to share C-interop code between native and jvm. You can share that code between different native targets though (eg. macos and ios).
I’m not an expert on this but my understanding is that the klib format is used not only for native libraries but also for any kind of kotlin multiplatform library. That means that it can contain both common and native parts.
We’re about to plan for our future mobile app (current one is written in Qt/QML/C++ ugh).
I also want to avoid cross-platform frameworks because I don’t believe they’re sustainable when you need platform-specific and can’t afford working around glitches, incompatibilities.
For those reasons, I have in mind to go SwiftUI/Jetpack Compose (Beta released a few days ago, yes!) + KMM for reusability. However, like you, we have a C++ core that handles communication with our device and works fine. Instead of writing bindings for Swift and Kotlin separately, it would be nice to have our shared KMM layer link to our C++ lib (C API).
It works like a charm on iOS, but direct bindings on Android are not possible yet (you still need JNI part and import/export in Android part of the project). There is a feature request open regarding this issue, but no timeline given yet.
After some messing around we decided to go with Flutter (Dart:ffi for C interoperability), since it offered us the fastest and friendliest experience . Other contenders were Xamarin , KMM.
I also want to avoid cross-platform frameworks because I don’t believe they’re sustainable when you need platform-specific and can’t afford working around glitches, incompatibilities.
I agree with you here. But still even though Kotlin Multiplatform looks excellent on paper and probably has a bright future, it is in my opinion currently still a little bit to young to use in some serious use case.
Sorry for the late reply.
Thank you for taking the time to answer!
I understand. In our case, since it’s going to be a relatively small app and we don’t have the manpower to absorb not only native dev (Swift/Kotlin) but also Dart/Flutter, we will go full native and link with our C++ cross-platform library that contains the communication layer with our hardware product. We will need to provide and maintain native bindings on top of that C++ library. Eventually, we will see if adding KMM can save us repeating a substantial amount of business logic code.
I had too many hurdles with previous cross-platform frameworks. I want us to be free like the wind now. heh
After much trying I think I’m going to go down that route too. I’m sticking with a C++ core. KMM looks promising to expose C++ to both Android and iOS via a single Kotlin library, but so far I just haven’t been able to expose my .klib (which I compiled using cinterop) to the Android app. Didn’t even try exposing to iOS as if I can’t get it to work with Android too then there’s no point.
Will definitely be switching over if this works one day. Working with a single Kotlin code base for iOS and Android would be much nicer than maintaing Swift and Kotlin code.