Skip to content

Kotlin and Java Modules

s edited this page Aug 1, 2026 · 4 revisions

Kotlin and Java Modules

The CLI calls this backend Native Module. Use it for Kotlin/Java libraries and Android APIs. Value-returning exports are Promise-based in JavaScript.

Generate a module

Interactive:

supernote-module

Choose Add module and Native Module.

Repeatable command:

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

For local-math, the default identities are JavaScript name Math and Android namespace com.example.math.

Write an export

The generated starter file is:

local_modules/local-math/android/src/main/java/com/example/math/Example.kt
package com.example.math

import com.example.math.nativemodule.annotation.SupernoteExport

class Example {
  @SupernoteExport
  fun add(left: Double, right: Double): Double = left + right

  @SupernoteExport(name = "greet")
  fun makeGreeting(name: String): String = "Hello, $name"
}

The annotation import is specific to the generated Android namespace. Java sources use the same annotation.

Call it

import Math from 'local-math';

const total = await Math.add(20, 22);
const message = await Math.greet('Supernote');

Use await for Boolean, Double, or String returns. A Unit/void export is fire-and-forget:

@SupernoteExport
fun setEnabled(enabled: Boolean) {
  // Apply the setting.
}
Math.setEnabled(true);

Constructor rules

An exported concrete public class needs one of these public constructors, used in this preference order:

  1. ReactApplicationContext;
  2. android.content.Context;
  3. no arguments.

Activity injection and arbitrary constructor parameters are not supported.

Build and validate

supernote-module validate local-math --build --verbose
./buildPlugin.sh

On Windows PowerShell, use \.\buildPlugin.ps1 for packaging. Build after changing exports because the Android/KSP step regenerates declarations.

Editable and generated paths

Your Kotlin/Java implementation below android/src/main/java/ is preserved by Update, except generated packages. Do not edit the generated annotation, processor, bridge, registration, Gradle, wrapper, or declaration files. Remove deletes the entire module.

See Exports and JavaScript API for the complete signature rules and Managing Modules before Update/Remove.

Clone this wiki locally