3

I want to integrate C code into a Kotlin Multiplatform Mobile project using the cinterop tool. I already spent some time on the documentation on Kotlin Multiplatform and Kotlin/Native but I can't seem to get it working.

Here is my build.gradle.kts:

kotlin {
    android {
        publishLibraryVariants("release", "debug")
        publishLibraryVariantsGroupedByFlavor = true
    }
    ios()

    androidNativeArm32 {
        compilations.getByName("main") {
            val myInterop by cinterops.creating {
                defFile(project.file("foobar.def"))
                packageName("org.sample")
            }
        }
    }

    ...
}

I want to do the same for the native iOS part.

Watching the gradle output, it seems to me that the cinterop configuration is completely ignored. It doesn't matter whether the def file exists or not. It does not make a difference.

Sorry, if I miss the obvious here. I am a bit confused by all the different Kotlin extensions and especially how they are supposed to work together.

What am I missing in my configuration? Is it even possible to use cinterop in a KMM project.

Jan Deinhard
  • 19,645
  • 24
  • 81
  • 137
  • 2
    Please note that `android` and `androidNativeArm32` are different targets. If you want to use the native binaries built for `androidNativeArm32` from your `android` target, you will have to configure that manually using Android NDK support. – hotkey Feb 12 '21 at 12:00
  • Thanks @hotkey. Good point. I wonder whether it makes sense to use `cinterop` at all in this case. Why not use NDK/CMake directly then? – Jan Deinhard Feb 12 '21 at 17:57
  • 2
    You need `cinterop` only if you want to write Kotlin/Native code that references the native libraries. In your case, if you want to introduce some wrappers around the native lib and use Kotlin for that, then you have to use `cinterop`, but you most likely want to do that in a different subproject from your `android` target, because in one project all common code will get compiled for both `android` and `androidNativeArm32`. If you only need to call the native library from Android code, then you don't need `cinterop` and NDK is your best choice. – hotkey Feb 12 '21 at 18:19
  • Actually, I need to call the native code from iOS and Android code. I tried to move the `compilations...` part in the `android` section but then it complains about `cinterop` not being available. I looked for examples that do that but could not find any. I also found the documentation to be not very helpful or I miss some background. Possible. Could you point me to an example project? – Jan Deinhard Feb 12 '21 at 18:45
  • I still can't make cinterop work for Android. My current workaround for Android is using JNI bindings with the Android NDK. For iOS cinterop works fine. – Jan Deinhard Feb 26 '21 at 22:41
  • 2
    Any progress on this? I'm also trying to find a way to call C++ from KMM code. I have a cross-platform project at the moment - where the cross-platform code is written in C++ - that I want to gradually migrate to KMM, and to do that I need to allow the KMM code to call the existing shared (C++) code. – André Morujão May 17 '21 at 10:26
  • I have the same question as André. Did any of you solve this? – Jørgen Svennevik Notland Dec 08 '22 at 18:32

0 Answers0