The Java Native Interface (JNI) allows C++ code to call Java functions, and vice versa. On Android, Haxe code compiles to C++, but only Java code can access the Android system API, so it's often necessary to use both.

For a working example, run lime create extension MyExtension, then look at MyExtension.hx and MyExtension.java.

You can pass Haxe objects to Java, much like any other data. In Java, they'll have type org.haxe.lime.HaxeObject, meaning the method that receives them might have signature (Lorg/haxe/lime/HaxeObject;)V. Once sent, the Java class can store the object and call its functions.

Note that most Java code runs on a different thread than Haxe, meaning that you can get thread-related errors in both directions. Java functions can use Extension.callbackHandler.post() to switch to the UI thread, while Haxe code can avoid the problem using lime.system.JNI.JNISafety.

Static methods

staticcallMember(method:Dynamic, jobject:Dynamic, a:Array<Dynamic>):Dynamic

staticcallStatic(method:Dynamic, a:Array<Dynamic>):Dynamic

staticcreateMemberField(className:String, memberName:String, signature:String):JNIMemberField

Parameters:

className

A string in the format "com/package/ClassName" or "com/package/ClassName$NestedClass". If dots are used instead, JNI will attempt to replace them with the correct symbols.

memberName

The member field's name.

signature

A Java VM type signature.

See also:

  • Java VM type signatures: https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html#wp16432

@:value({ quietFail : false, useArray : false })staticcreateMemberMethod(className:String, memberName:String, signature:String, useArray:Bool = false, quietFail:Bool = false):Dynamic

Parameters:

className

A string in the format "com/package/ClassName" or "com/package/ClassName$NestedClass". If dots are used instead, JNI will attempt to replace them with the correct symbols.

memberName

The member method's name.

signature

A Java VM type signature.

useArray

Set this to create a function that takes a single Array<Dynamic> argument, instead of multiple Dynamic arguments.

quietFail

Set this to suppress the "method not found" error.

See also:

  • Java VM type signatures: https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html#wp16432

staticcreateStaticField(className:String, memberName:String, signature:String):JNIStaticField

Parameters:

className

A string in the format "com/package/ClassName" or "com/package/ClassName$NestedClass". If dots are used instead, JNI will attempt to replace them with the correct symbols.

memberName

The static field's name.

signature

A Java VM type signature.

See also:

  • Java VM type signatures: https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html#wp16432

@:value({ quietFail : false, useArray : false })staticcreateStaticMethod(className:String, memberName:String, signature:String, useArray:Bool = false, quietFail:Bool = false):Dynamic

Parameters:

className

A string in the format "com/package/ClassName" or "com/package/ClassName$NestedClass". If dots are used instead, JNI will attempt to replace them with the correct symbols.

memberName

The static method's name. To get a constructor, use <init> as the method name.

signature

A Java VM type signature. To get a constructor, use V as the function's return value.

useArray

Set this to create a function that takes a single Array<Dynamic> argument, instead of multiple Dynamic arguments.

quietFail

Set this to suppress the "method not found" error.

See also:

  • Java VM type signatures: https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html#wp16432

staticgetEnv():Dynamic

staticpostUICallback(callback:() ‑> Void):Void