Posts Tagged: tutorial


18
Jan 13

NME Native Extension Part 2 : Calling CPP methods from haxe

Much more easier than JNI methods, all you have to do is using the “cpp.Lib” class ( “neko.Lib” for neko ).

1
Lib.load( libName , primitiveName , argsCount );

LibName:
It’s the cpp library name ( without path and extension ).

PrimitiveName:
The name of the primitive defined in the ExternalInterface class.
In the next article of the tutorial we will take a look at how to define the primitive in the ExternalInterface class, which is the backbone of the native extension.

ArgsCount:
Obviously it’s the argument count used by the primitive.

This will return like for JNI, a function which can be call with the correct number of arguments.


6
Jan 13

NME Native Extension Part 1 – Android : Calling Java methods from Haxe

The JNI class (nme.JNI) allow native haxe code to call Java methods.

For static method the function call is :

1
nme.JNI.createStaticMethod( package_name , "method_name" , signature )

for non-static method it’s :

1
nme.JNI.createMemberMethod( package_name , "method_name" , signature )

Package name:

The package is the full class package and class name separated by slash ( example: “org.shoebox.Test” is “org/shoebox/Test” )

The signature :

The signature of the method is a string:
( mapped arguments type ) return_type

First thing to know is than the arguments type must be mapped for Java.

For the basics types just follow the following table:

For the non basic types ( by example a class instance ) the mapping is:

Lpackage/of/the/ClassName;

Some examples:

On the java side :

1
2
3
4
5
static public DemoJNI getInstance( ){}

static public boolean test_ret_bool( ){}

public boolean test_method_nonstatic( i : Int , b : boolean ){}

 

On the haxe side for the statics methods :

1
2
3
4
var f1 = nme.JNI.createStaticMethod( "org/shoebox/DemoJNI" , "getInstance" , "()Lorg/shoebox/DemoJNI;" );
var instance = f1( );
var f2 = nme.JNI.createStaticMethod "org/shoebox/DemoJNI" , "test_ret_boo" , "()Z" );
var b = f2( );

 

For member methods calls you must pass as argument the JNI class instance ( by example the result of the getInstance( ) method )

1
2
var f3 = nme.JNI.createMemberMethod "org/shoebox/DemoJNI" , "test_method_nonstatic" , "(IZ)Z" );
f3( instance , 10 , false );

Quite easy isn’t it ? When you have understand the java mapping it’s quite easy to call any java method from Haxe.