Kotlin multiplatform mobile and C interop

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.

Is this possible? What am I missing?

1 Like

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.

1 Like

But isn’t he doing something like that in the above given post? At least for iOS.

Is it possible to do it via Kotlin Native? I know that I will only need Android/iOS so no JS and stuff.

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.


From my understanding it should be possible to some extent, especially for plain C code without any platform specific dependencies.

The main goal for me would be to avoid using JNI and any other kind of unnecessary wrappers. So only one C code for both iOS / Android.

I have already tested this with Flutter and Dart:ffi and works as charm, but there you are locking yourself into Dart/Flutter environment.

It would be nice if more of you could join the discussion and present your point of view on this topic.

Below is the same problem that should “work” on iOS, but i didn’t test it yet and don’t know how to make it work on Android: android - Access C/C++ lib from a multiplatform kotlin project - Stack Overflow

If no direct solution is possible, maybe the best way would be to export .aar and .framework from .klib? Just a thought.

Hey there,

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).

How did your investigation go?

1 Like

Hi Xander,

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 :slight_smile:. 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 :wink:


1 Like

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.