Kotlin-JNI Help

🛠️ Setup

Tests Maven Central Version

To use Kotlin-JNI in your project, add the dependencies to your build.gradle.kts file.

Module

Purpose

com.dshatz.kni:jni

Common extensions for working with JNI types.

com.dshatz.kni:ksp

KSP generator for easy bi-directional communication.

com.dshatz.kni:annotations

Annotations used by the KSP generator.

com.dshatz.kni:buffers

java.nio.ByteBuffer support

[versions] kni = "2.1.0" [plugins] kni = { id = "com.dshatz.kni", version.ref = "kni" } [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" } kni-buffers = { module = "com.dshatz.kni:buffers", version.ref = "kni" }

Setup options

Gradle plugin

The KNI plugin significantly simplifies building KMP apps with native targets and facilitates correct KNI integration into the project.

All plugin features are optional and independent of each other. In fact, you can attempt to skip the plugin altogether and just go with KSP.

Autowire

plugins { alias(libs.plugins.kni) id("com.google.devtools.ksp") } kni { autoWire { kspDependency = libs.kni.ksp } }

2 source sets will be created for better code organization:

  • jniJvm[Main|Test] containing jvm[*] and android sourcesets

  • jniNative[Main|Test] containing linux[*}, macos[*], mingw[*], androidNative[*] sourcesets.

Additionally, the provided ksp dependency will be added to all relevant targets.

Automatic Kotlin/Native bundling

Configure shared libraries built from your Kotlin/Native targets to be automatically bundled into the final JAR/AAR.

plugins { alias(libs.plugins.kni) } kotlin { val linux64 = linuxX64 { binaries.sharedLib() // define what you need here, like cinterops } val androidNative = androidNativeArm64 { binaries.sharedLib() } // linuxX64 .so artifact will // be added to lib/ directory inside the JAR. jvm() bundlesNatives listOf(linux64) androidLibrary { // androidNativeArm64 .so artifact will // be added to jniLibs inside the AAR. bundlesNatives(listOf(androidNative)) } }

Define optional Kotlin targets

Controlling which Kotlin targets are enabled can be vital for faster development and CI jobs.

plugins { alias(libs.plugins.kni) } kotlin { optionalTargets { androidNativeX64() androidNativeArm64() androidNativeX86() androidNativeArm32() linuxX64 { binaries.sharedLib() } linuxArm64() mingwX64() macosX64() macosArm64() iosX64() iosArm64() iosSimulatorArm64() js() wasmJs() wasmWasi() // other targets } }

Pass the whitelist of targets

Example

gradle property

./gradlew allTests -PkniAllowedTargets=linuxX64,androidNativeArm64

local.properties

kniAllowedTargets=linuxX64,androidNativeArm64

To configure a source set that is optional, use gettingOptional:

sourceSets { val jsMain by gettingOptional { dependencies.implementation("...") } }

Manual setup

Manual setup only requires adding the ksp processor to all targets.

dependencies { add("kspLinuxX64", libs.kni.ksp) add("kspAndroidNativeArm64", libs.kni.ksp) add("kspJvm", libs.kni.ksp) add("kspAndroid", libs.kni.ksp) }
Last modified: 23 April 2026