I have .so (shared library) written in C++, lets call it functionality.so in which I implement different functions, here is list of some fu
I consider @pron answer the best one, so far.
About automatic JNI code generation, consider jnigen It is very simple to use and powerful.
Get the Javadocs, and look for NativeCodeGenerator class. It has the applied mapping between Java types and native CPP types, as String to char*, int[] to int*, FloatBuffer to float*, etc.
Jnigen as described on GitHub
jnigen is a small library that can be used with or without libgdx which allows C/C++ code to be written inline with Java source code.
jnigen has two parts:
Inspect Java source files in a specific folder, detect native methods and the attached C++ implementation, and spit out a C++ source file and header, similar to what you'd create manually with JNI.
Provide a generator for Ant build scripts that build the native source for every platform.
Example: This are your Java native methods, with desired cpp code defined as comment just after the method declaration :
private static native ByteBuffer newDisposableByteBuffer (int numBytes); /*
char* ptr = (char*)malloc(numBytes);
return env->NewDirectByteBuffer(ptr, numBytes);
*/
private native static void copyJni (float[] src, Buffer dst, int numFloats, int offset); /*
memcpy(dst, src + offset, numFloats << 2 );
*/
After running jnigen generator, you will have the *.cpp file with your C code and bindings. The *.h is also created automagically.
The cpp will looks like this:
JNIEXPORT jobject JNICALL
Java_com_badlogic_gdx_utils_BufferUtils_newDisposableByteBuffer
(JNIEnv* env, jclass clazz, jint numBytes) {
//@line:334
char* ptr = (char*)malloc(numBytes);
return env->NewDirectByteBuffer(ptr, numBytes);
}
JNIEXPORT void JNICALL
Java_com_badlogic_gdx_utils_BufferUtils_copyJni___3FLjava_nio_Buffer_2II
(JNIEnv* env, jclass clazz, jfloatArray obj_src, jobject obj_dst, jint numFloats, jint offset) {
unsigned char* dst = (unsigned char*)env->GetDirectBufferAddress(obj_dst);
float* src = (float*)env->GetPrimitiveArrayCritical(obj_src, 0);
//@line:348
memcpy(dst, src + offset, numFloats << 2 );
env->ReleasePrimitiveArrayCritical(obj_src, src, 0);
}
This is how would look your methods, with jnigen:
/**
* @param userData - a pointer. Assumed position() is Zero.
**/
public native static long initialize(Buffer userData);/*
return (jlong) Initialize( (void*) userData);
*/
public native static long uninitialize();/*
return (jlong) Uninitialize();
*/
/**
* Assumptions : id points to a unique device
* @param id - id
* @param device - a long[] with length 1, to return device pointer.
*/
public native static long deviceOpen(long id, long[] device);/*
return (jlong) DeviceOpen( (unsigned long) id, (unsigned long*) device);
*/
public native static long deviceClose(long device);/*
return (jlong) DeviceClose( (unsigned long) device);
*/