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
staticcreateMemberField(className:String, memberName:String, signature:String):JNIMemberField
Parameters:
className | A string in the format |
---|---|
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
staticcreateMemberMethod(className:String, memberName:String, signature:String, useArray:Bool = false, quietFail:Bool = false):Dynamic
Parameters:
className | A string in the format |
---|---|
memberName | The member method's name. |
signature | A Java VM type signature. |
useArray | Set this to create a function that takes a single
|
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 |
---|---|
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
staticcreateStaticMethod(className:String, memberName:String, signature:String, useArray:Bool = false, quietFail:Bool = false):Dynamic
Parameters:
className | A string in the format |
---|---|
memberName | The static method's name. To get a constructor, use
|
signature | A Java VM type signature. To get a constructor, use |
useArray | Set this to create a function that takes a single
|
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