How can I import c++ code to commonMain (Kotlin/Multiplatform project)

Hello everyone,
I want to implement a few use cases before migrating my business logic code from JAVA to Kotlin/Multiplatform.
One of them is using c++ code.
There is an option to use c++ code in Kotlin/Multiplatform project on commonMain level?
Or I need to write a wrapper layer (like JNI) for each platform?
Can I get some example code?

Many thanks!

There is no way to implement a common wrapper (how do you see browser wrapper for example?). If you need to access your C++ code both from JVM and Native, you need to do it two times via expect/actual mechanism.

1 Like

@darksnake
Tnx for your answer!
So what is the best project structure to build a common business logic code for IOS & Android?
It means- what I need for this?

  1. IntelliJ IDEA (For Kotlin/Multiplatform library) for .aar & .framework
  2. Android Studio for Android application project
  3. XCode for IOS application

Once in (1) - the structure will be something like:

  1. commonMain (‘expect’ annotation)
  2. iosMain (‘actual’ implementation for c++ bridge - objective c)
  3. androidMain, JvmMain (‘actual’ implementation for c++ bridge - JNI)

if am I wrong - please let me know.
if am I right - Can I get a reference or some code example of how to actually use the C++ library for each platform? because I’m trying to do it for Android and it does not seem to work.

when I add this:

externalNativeBuild {
        cmake {
            path file('CMakeLists.txt')
        }
}

to my my build.gradle I got “Unsupported method: AndroidArtifact.getBuildConfigFields()” error.