How to use cinterop in Kotlin Multiplatform Mobile projects
Asked Answered
E

0

4

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.

Edsel answered 12/2, 2021 at 8:48 Comment(7)
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.Ingunna
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?Edsel
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.Ingunna
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?Edsel
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.Edsel
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.Didymous
I have the same question as André. Did any of you solve this?Eudemonism

© 2022 - 2024 — McMap. All rights reserved.