Ktjni is a Gradle plugin that generates JNI headers from Kotlin, Java, or Scala classes containing external native methods.
Manually writing JNI headers is tedious, error-prone, and a common source of runtime crashes due to signature mismatches. This plugin eliminates that pain by scanning compiled .class files using the ASM library to generate accurate, up-to-date headers that match your JVM method signatures.
Since all JVM languages compile native method declarations to the same core bytecode structure, Ktjni works consistently across Kotlin, Java, and Scala codebases, regardless of compiler-specific attributes or metadata differences.
1. Add Maven Central to your pluginManagement repositories
pluginManagement {
repositories {
mavenCentral() // Release versions
maven {
url = uri("https://central.sonatype.com/repository/maven-snapshots/") // Snapshot versions
}
}
}2. Add the Ktjni plugin to your project's build.gradle.kts and choose your header output directory
plugins {
id("io.github.fletchmckee.ktjni") version "0.1.2"
}
// If no `outputDir` is specified, it defaults to the following location:
// {projectDir}/build/generated/ktjni/{sourceSet}
ktjni {
outputDir = layout.buildDirectory.dir("custom")
}Whether using the default outputDir or a custom location, the sourceSet is added as subdirectory for configuration cache correctness.
Header generation currently requires manual execution and does not automatically integrate with the build process.
Execute the following aggregate command to generate JNI headers for all variants of files containing external native methods:
./gradlew generateJniHeadersNote
Executing this task will compile the corresponding source sets if not already compiled. For a more fine-grained approach, you can discover the relevant Ktjni commands with the following:
./gradlew tasks --group "ktjni"The following Kotlin class would produce the below JNI Header:
package com.example
class Crypto {
external fun sha256(input: ByteArray): ByteArray
}Examples in Java and Scala that would produce the same header output
package com.example;
public class Crypto {
public native byte[] sha256(byte[] input);
}package com.example
class Crypto {
@native
def sha256(input: Array[Byte]): Array[Byte]
}/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_Crypto */
#ifndef _Included_com_example_Crypto
#define _Included_com_example_Crypto
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_Crypto
* Method: sha256
* Signature: ([B)[B
*/
JNIEXPORT jbyteArray JNICALL Java_com_example_Crypto_sha256
(JNIEnv *, jobject, jbyteArray);
#ifdef __cplusplus
}
#endif
#endif- Kotlin JVM (
org.jetbrains.kotlin.jvm) - Kotlin Multiplatform (
org.jetbrains.kotlin.multiplatform) - Kotlin Android (
org.jetbrains.kotlin.android) - Java (via
javaorjava-libraryplugins) - Scala (
scalaplugin) - Android Application (
com.android.application) - Android Library (
com.android.library) - Android Multiplatform Library (
com.android.kotlin.multiplatform.library)
Copyright 2025 Colin McKee
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.