I’m trying to set up a multiplatform project to wr...
# multiplatform
s
I’m trying to set up a multiplatform project to wrap a native (C++) library built with CMake. I have a small cmake project with
extern C
bindings to use via JNI on Kotlin/JVM and cinterop on Kotin/Native. However, I’m a bit lost when it comes to the gradle config for this, specifically with the jvm (desktop) target. On Android, it’s easy; just set up
externalNativeBuild
with cmake in gradle and
System.loadLibrary("foo")
at runtime. The IDE even matches up
external
declarations in Kotlin to their corresponding native headers. On desktop JVM, how would I go about accomplishing the Gradle config? The general goal is to have gradle use
cmake
to build my native sources and include the produced .dylib, .so, and .dll in my library’s jar artifact. I can then use a helper at runtime to extract that lib to a directory I can load from. The gradle tasks dependencies should be wired up so whatever task builds the JVM jar includes the library in its resources.
👀 1
Okay, I’m partially there with this. I’ll still need different classifiers of the artifact for different platforms (OS + arch)
f
did you try https://github.com/java-native-access/jna ? Easier to use C lib in KMP.
s
I'm using jnr-ffi which seems very similar. But my problem isn't the C calls itself, it's the project config (gradle build and publish) for the library, including desktop jars with platform specific classifiers, and iOS/Mac cinterop. Android aar I understand at least, using externalNativeBuild
z
@Samuel Chen have a look at my project. maybe it can help. https://github.com/Zoxcore/trifa_material/blob/master/build.gradle.kts its desktop only. but uses lots of JNI libs
n
@Sargun Vohra, sorry for disturb, I'm having the same trouble with gradle on K/JVM. Wonder if you can shed some light?