Posts Tagged: jni


20
Jan 13

NativeMirror : Easier way to make JNI/CPP calls from haxe

It’s still useful to know how to make native calls from haxe manually ( for debugging by example ) but there is a quicker and easier way.
It’s stricly type, flexible and simple.
It use a simple macro you can find here : here.

All you have to do is to add it on build of yours haxe class.

1
2
3
package org.shoebox.test;
@:build(org.shoebox.utils.JNIMirror.build( )) class Test{
}

Then all you need to do is to create mirrors for the native methods.
 

Mirrors for JNI Methods

Basic example:

In the following case the JNI class name and method name are not defined.
So the macro use the same classpath classname and function name than on the haxe side.

1
2
@JNI
static public function test( s : String , i : Int ) : String {}
If the haxe method name or package is not the same on the java side:

You can customize both by adding argments to the JNI meta:

1
2
@JNI("org.shoebox.Test","TestFunc")
static public function test( s : String , i : Int ) : String {}
For non native types:

It works too for non-native type if the class exists on both side ( with the same package and class name ).
By example we are trying to get the instance of the java class Test inside haxe, the method is defined this way:

1
2
3
@JNI
static public function getInstance( ):Test{
}

 

For CPP Methods

It works the same way, but the first meta argument ( library_name ) must be always defined ( for now ).
The second meta argument ( method_name ) is optional.

1
2
@CPP("library_name","method_name")
public function test_cpp( instance : Dynamic , sTest : String , i : Int , b : Bool ) : Void {}

Hope it helps.


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.