Kotlin-JNI Help

⚙️ Kotlin JNI Library

Loading native libraries (JVM/Android)

The jni module provides a helper for seamless integration with the Gradle Plugin.

This will call System.loadLibrary(), with some pre-processing and name calculation depending on the platform.

BundledLibLoader.loadBundledLibrary("demo")

Extensions

Some of these extensions can be useful when not using the KSP generator and writing your own @CName-annotated functions.

Converting from JNI types to Kotlin types

Converting between JNI, Kotlin and back is easy:

import com.dshatz.kni.utils.* import com.dshatz.kni.JNIEnvVar import com.dshatz.kni.binding.jstring import com.dshatz.kni.binding.jintArray import com.dshatz.kni.binding.jbyteArray import kotlinx.cinterop.CPointer val jMessage: jstring = ... val kotlinString: String? = jMessage.toKString(env) require(jMessage == kotlinString?.toJString(env)) val jInts: jintArray = ... val kInts = jInts.toKIntArray(env) require(jInts == kInts?.toJIntArray(env)) val jBytes: jbyteArray = ... val kBytes: ByteArray? = jBytes.toKByteArray(env) require(jBytes == kBytes?.toJByteArray(env))

For a detailed list of functions, see the API Reference.

Last modified: 23 April 2026