Android JNI and JAR Library JNI Library 1. JNI *.c sources file must include jni header file jni.h #include 2. In Make file CFLAGS must add -I $(NDK_INC)/ and android_evn.mak must have It in Makefile_android CFLAGS += -I $(NDK_INC)/ android_evn.mak ifeq ($(VER40), $(VER)) NDK_INC = $(ANDROID_SRC)/prebuilt/ndk/android-ndk-r6/platforms/android-9/arch- $(ARCH)/usr/include NDK_LIB = $(ANDROID_SRC)/prebuilt/ndk/android-ndk-r6/platforms/android-9/arch- $(ARCH)/usr/lib Endif ( Android 4.0 ndk/android-ndk-r6/platforms/android-9/arch-x86/usr/include/jni.h ) ( Android 2.2 ndk/android-ndk-r4/platforms/android-8/arch-x86/usr/include/jni.h )
3.How to implements C function method JNIEXPORT jboolean JNICALL Java_ com_Advantech_SUSI_SUSILIB_SusiDllInit(JNIEnv *, jobject) { return SusiDllInit(); // SusiDllInit SUSI C function and return boolean } All available types define as below., Java Language Type Native Type Description boolean jboolean unsigned 8 bits byte jbyte signed 8 bits char jchar unsigned 16 bits short jshort signed 16 bits int jint signed 32 bits long jlong signed 64 bits float jfloat 32 bits double jdouble 64 bits com_Advantech_SUSI is packages name and SUSILIB is jar class library Implements in java file as below., import com.Advantech.SUSI.SUSILIB; // For Android APP JNIEXPORT and JNICALL are macros defined at jni.h Ref The Java™ Native Interface Programmer’s Guide and Specification ( ISBN )
JNI use Get/Release ArrayElements functions to let native library code to obtain a direct pointer to the elements of primitive arrays ( In Java no pointer only array) If use Get ArrayElements and must use Get ArrayElements to release memory in Virtual Machine JNIEXPORT jboolean JNICALL Java_com_Advantech_SUISI_SUSILIB_SusiHWMGetFanSpeed (JNIEnv* env, jobject obj, jshort fanType, jshortArray retval, jshortArray typeSupport ) { jboolean jbool; jshortArray jretval; jshortArray jtypeSupport; if( retval != NULL ) jretval = (*env)->GetShortArrayElements(env, retval, NULL); // If use C++ change to env->GetShortArrayElements // Use Get ArrayElements to pointer to allow the native code to obtain a direct pointer to the elements of primitive //arrays. if( typeSupport != NULL ) jtypeSupport = (*env)->GetShortArrayElements(env, typeSupport, NULL); // To obtain a direct pointer to the elements of primitive arrays if( typeSupport != NULL ) { jbool = SusiHWMGetFanSpeed ( (WORD) fanType, (WORD *) 0, (WORD *) jtypeSupport ); // Can’t pass NULL instead use (void *) 0 or (WORD *) 0 } else { jbool = SusiHWMGetFanSpeed ( (WORD) fanType, (WORD *) jretval, (WORD *) 0 ); } if( retval != NULL ) (*env)->ReleaseShortArrayElements(env, retval, jretval, 0); if( typeSupport != NULL ) (*env)->ReleaseShortArrayElements(env, typeSupport, jtypeSupport, 0); return jbool; }
JAR Library 1. Ceate a new Java class with Eclipse for Android APP call SUSI Methods package com.Advantech.SUSI; import android.util.Log; public class SUSILIB { public static native boolean SusiHWMGetFanSpeed(short fanType, short[] retval, short[] typeSupport); static { try { Log.i("JAVA", "System.load(/system/lib/libSUSI-JNI.so)"); System.loadLibrary("SUSI-JNI"); // Load SUSI JNI library libSUSI-JNI.so from /system/lib } catch (UnsatisfiedLinkError use) { String msg = use.getMessage(); Log.e("JAVA", msg); Log.e("JAVA", "WARNING: Could not load libSUSI-JNI.so"); }
2. Use Eclipse to build it and export a jar file for SUSI Demo APP In Package Explorer first to select Export then select export destination
Select resource files to be created as JAR file and output JAR file, Press finish.