Hi, why is `import kotlinx.cinterop.*` not availa...
# kotlin-native
n
Hi, why is
import kotlinx.cinterop.*
not available when used inside kotlin multiplatform (either commonMain or androidMain) ?
a
android has not cinterop
👍 1
n
So there is no way to use one simple Kotlin native lib that interacts with plain C code? Ideally I would want to use one .klib for both iOS/Android
a
Android CAN NOT use C code.
đźš« 1
a
Hi, @Niccolo Machiavelli! If you want to use Android native target, you need to try different Gradle project layout. Probably, you’re mixing Kotlin/JVM and Kotlin/Native here.
See Supported Platforms list. Android NDK targets are supported by the Kotlin/Native compiler, so they will be able to use
kotlinx.cinterop
contents. The “Android applications and libraries” one will not. Most of our KMM tutorials use the
android
target, so they won’t let you interop with C libraries as easy as you can do for the Kotlin/Native targets.
🙌 1
n
@Artyom Degtyarev [JB] so the only possible solution to access plain C code from Android would still be trough the JNI? Are there any plans on changing that in the future? My main goal when investigating this possibilitie was to avoid JNI and to have one single channel for iOS and Android, the iOS part is working as inteded trough Kotlin Native - cinterop.
Dart:ffi is doing exactly that 🙂 for both iOS/Android but there you are locking yourself into "Dart/Flutter" enviroment while you can still use Kotlin in native Apps.
g
There is a feature request about it, but probably not particular plans for it
To use C on Dalvik you anyway need jni, even if cinterop will one day generate it for you There are plenty of tools for this on JVM to generate interop automatically, but all of them will of course require some wrapper if you want to use the same C lib on Android and iOS BTW we use Djinny for this, to share the same C++ library between Android/iOS/C++ Server https://github.com/dropbox/djinni
🙌 1
s
Side note, Flutter on Android runs natively and not in the Dalvik vm. It is closer to a Kotlin Native project using the Android NDK than typical Kotlin Android app development. That’s why Flutter can use ffi on both platforms.
n
@Sam would it be possible to geneate something like .aar (using c lib underneath) that way? 🙂
g
Generate aar using c lib?
It's already possible to create such aar with .so and know glue inside
n
Kotlin Native project using the Android NDK
but we are still back to JNI than right ? 🙂
g
In theory yes, it should be possible, even now, but with jni generation
And there is no direct integration with Kotlin, but it doable.
The main problem that you may have different (not identical at least) api on iOS
Just because cinterop tool and {some JVM specific solution} will generate not the same code
This why this feature request is relevant: https://youtrack.jetbrains.com/issue/KT-39144
🙌 1
n
Well maybe it will be possible in the future 🙂 it is a big advantage for Flutter/Dart:ffi that you can have one C bindings for both iOS/Android, and tools/documentation seems more mature too..
Yup that is exactly what i would need!
g
Kotlin approach is different from flutter, you can wrap platform specific api/library with Kotlin code instead of using C libs on both platforms, so it covers many use cases
But indeed, case when you need the same C lib on Android also common
🙌 1
s
Flutter basically recreate the entire application stack and ignores its host environment. Your app is a self contained app running on the same stack anywhere Flutter runs. That means your app is beholden to the Flutter people to provide some integrations. Keyboard auto suggest in text fields is one example. The Kotlin approach is to integrate with the host environment and just be another normal app. I wrote in both regularly and like them both for different uses.
🙂 1
308 Views