To be able to compile JNI code you need to create a make file. You can fin examples of working make files in the samples here. I named the Vuforia Android samples since it was because of this library that I had to work with JNI :P
In windows you just have to type the following the commands promt:
cd <path_to_jni_files>
<path_to_android_ndk>\ndk-build
For example:
cd C:\Users\dev\Desktop\Repositories\MyProject\jni
C:\android-ndk-r9c\ndk-build
You can find the Android NDK here. You can find the oficial documentation about JNI here.
Loading libs
The compiled library needs to be placed inside "/libs" inside, most likely, the folder named "armeabi-v7a".
For example, to load the library named "libdetection_based_tracker.so" somewhere in your code you have to call the following method: "System.loadLibrary("detection_based_tracker");".
Create an object
Example: Instantiate a Float object
jclass floatClass = env->FindClass("java/lang/Float"); // 1
jmethodID floatConstructor = env->GetMethodID(floatClass, "<init>", "(F)V"); // 2
jobject float object = env->NewObject(floatClass, floatConstructor, 5.0); // 3
1) We first need to find the class will be working on, in this case, the float class. env is the environment pointer is of type (JNIEnv).2) Get the constructor method.
<init> indicates its a constructor.
(F)V defines the constructor arguments (the characters inside the parentheses) and the return type (just after the parentheses). In this case the arguments is just a single float (F) and the return type is void (V). You can check all type signatures here.
3) Create a java object given the class (floatClass), the constructor (floatConstructor) and the arguments of the constructor is 5.0.
Calling methods from Android methods with JNI
jclass activityClass = env->GetObjectClass(obj);
jmethodID targetDetectedMethodID = env->GetMethodID(activityClass, "targetDetected", "([Ljava/lang/Object;)V");
if (targetDetectedMethodID == 0) {
LOG("Function targetDetected() not found.");
return;
}
env->CallVoidMethod(obj, targetDetectedMethodID, arguments); //1
1) Arguments can be any objects or a var_list
No hay comentarios:
Publicar un comentario