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.
android
andandroidNativeArm32
are different targets. If you want to use the native binaries built forandroidNativeArm32
from yourandroid
target, you will have to configure that manually using Android NDK support. – Ingunnacinterop
at all in this case. Why not use NDK/CMake directly then? – Edselcinterop
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 usecinterop
, but you most likely want to do that in a different subproject from yourandroid
target, because in one project all common code will get compiled for bothandroid
andandroidNativeArm32
. If you only need to call the native library from Android code, then you don't needcinterop
and NDK is your best choice. – Ingunnacompilations...
part in theandroid
section but then it complains aboutcinterop
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