Skip to content

JNI Modules

s edited this page Aug 1, 2026 · 4 revisions

JNI Modules

The CLI calls this backend Native JNI Module. Use it when implementation belongs in C or C++ and a Promise-based JavaScript call is appropriate. The generator owns the Kotlin/Java Native Interface (JNI) bridge and registration.

Generate a module

supernote-module add local-math-jni --type jni --yes

The derived JavaScript name is MathJni and the Android namespace is com.example.math_jni.

Check the C/C++ toolchain first when needed:

supernote-module doctor --type jni

Write the C++ export

Edit the user-owned C/C++ tree:

local_modules/local-math-jni/android/src/main/cpp/

For example, edit math.cpp:

// @SupernoteExport
double add(double left, double right) {
  return left + right;
}

The marker must be immediately before an ordinary top-level C++ definition. Do not write a JNI function, JNI_OnLoad, native registration, a loader, or generated CMake entries.

Call it

JNI value returns are asynchronous at the JavaScript boundary:

import MathJni from 'local-math-jni';

const total = await MathJni.add(20, 22);

Generated JNI transports strings as UTF-8 byte arrays instead of JNI modified UTF-8.

C and C++ roles

  • .cc, .cpp, and .cxx files may contain marked exports.
  • .c files compile as C23 and may provide internal helpers, but cannot export.
  • C++ and generated bindings compile as C++23.
  • Use extern "C" guards in headers when C++ calls C helpers.

Supported boundary values are bool, double, and UTF-8 std::string by value, plus void returns. See Export Functions for restrictions.

Regenerate and validate

supernote-module validate local-math-jni --build --verbose

This regenerates bindings and compile-checks them through the existing parent Android project. Continue with the plugin's established workflow after the generator reports success.

Ownership

Update preserves the complete android/src/main/cpp/ tree, including your choice to delete starter files. It replaces metadata, wrappers, declarations, Gradle/CMake, generated Kotlin/C++, loading, registration, and the generated README. Remove deletes everything in the module, including the C/C++ tree.

Clone this wiki locally