-
Notifications
You must be signed in to change notification settings - Fork 0
Kotlin and Java Modules
The CLI calls this backend Native Module. Use it for Android APIs and Kotlin or Java libraries. Value-returning functions use Promises in JavaScript.
Create the module using Add a Module, choosing Native Module.
Write .kt or .java files below:
local_modules/<package>/android/src/main/java/<namespace-path>/
The starter implementation is Example.kt.
package com.example.calculator
import com.example.calculator.nativemodule.annotation.SupernoteExport
class Example {
@SupernoteExport
fun add(left: Double, right: Double): Double = left + right
}If you prefer Java, replace the starter with Example.java:
package com.example.calculator;
import com.example.calculator.nativemodule.annotation.SupernoteExport;
public class Example {
public Example() {}
@SupernoteExport
public double add(double left, double right) {
return left + right;
}
}The annotation import changes with the Android namespace selected for the module.
import Calculator from 'local-calculator';
const total = await Calculator.add(20, 22);Use await for Boolean/boolean, Double/double, or String returns.
Functions returning Unit or void are called without await:
@SupernoteExport
fun setEnabled(enabled: Boolean) {
// Apply the setting.
}Calculator.setEnabled(true);An exported concrete public class needs one of these public constructors, used in this preference order:
-
ReactApplicationContext; -
android.content.Context; - no arguments.
Activity injection and arbitrary constructor parameters are not supported.
supernote-module validate local-calculator --buildThe Android/KSP build regenerates declarations and the bridge. Add --verbose
when diagnosing a failed build.
Update preserves your Kotlin and Java implementation. See Files the Generator Creates for the files Update may replace, and Export Functions for all supported signatures.