🛠️ Setup
To use Kotlin-JNI in your project, add the dependencies to your build.gradle.kts file.
Module | Purpose |
|---|---|
| Common extensions for working with JNI types. |
| KSP generator for easy bi-directional communication. |
| Annotations used by the KSP generator. |
[versions]
kni = "2.0.4-rc02"
[libraries]
kni = { module = "com.dshatz.kni:jni", version.ref = "kni" }
kni-processor = { module = "com.dshatz.kni:ksp", version.ref = "kni" }
kni-annotations = { module = "com.dshatz.kni:annotations", version.ref = "kni" }
Java Native Interface
Using the common JNI library is fairly easy, just add the dependency for your native targets.
val commonMain by getting {
dependencies {
implementation(libs.kni)
}
}
KSP
Using the KSP module to auto-generate JNI-compatible function stubs can be easily done as well.
First ensure your project is configured for using KSP.
plugins {
id("com.google.devtools.ksp")
}
Then add the KSP processor and annotations to your module's dependencies.
val commonMain by getting {
dependencies {
// Add the annotations dependency for KSP
implementation(libs.kni.annotations)
}
}
dependencies {
// Add the processor for the native targets you need.
add("kspLinuxX64", libs.kni.processor)
add("kspLinuxArm64", libs.kni.processor)
add("kspMingwX64", libs.kni.processor)
add("kspMacosArm64", libs.kni.processor)
add("kspAndroidNativeX64", libs.kni.processor)
add("kspAndroidNativeArm64", libs.kni.processor)
add("kspAndroidNativeArm32", libs.kni.processor)
}
Last modified: 03 February 2026