Skip to content
FletchMcKeePublic

About

Generates JNI headers from Kotlin, Scala, and Java

Topics

Resources

Stars

16 stars

Watchers

1 watching

Forks

Repository files navigation

Ktjni

Maven Central Build status JVM Android

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.

Getting Started

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.

Usage

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 generateJniHeaders

Note

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"

Example

The following Kotlin class would produce the below JNI Header:

Kotlin

package com.example

class Crypto {
  external fun sha256(input: ByteArray): ByteArray
}
Examples in Java and Scala that would produce the same header output

Java

package com.example;

public class Crypto {
  public native byte[] sha256(byte[] input);
}

Scala

package com.example

class Crypto {
  @native
  def sha256(input: Array[Byte]): Array[Byte]
}

JNI Header

/* 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

Supported Plugins

  • Kotlin JVM (org.jetbrains.kotlin.jvm)
  • Kotlin Multiplatform (org.jetbrains.kotlin.multiplatform)
  • Kotlin Android (org.jetbrains.kotlin.android)
  • Java (via java or java-library plugins)
  • Scala (scala plugin)
  • Android Application (com.android.application)
  • Android Library (com.android.library)
  • Android Multiplatform Library (com.android.kotlin.multiplatform.library)

License

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.

About

Generates JNI headers from Kotlin, Scala, and Java

Topics

Resources

Stars

16 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages