I've made a simple native library that can store a integer and return it.
#include <string.h>
#include <jni.h>
static int a;
void Java_com_example_testnativelibs_TestClass_setA(JNIEnv* env, jobject javaThis, jint val){
a = val;
}
jint Java_com_example_testnativelibs_TestClass_getA(JNIEnv* env, jobject javaThis) {
return a;
}
This is the TestClass code:
public class TestClass {
public TestClass() {
System.loadLibrary("ndkfoo2");
}
public native void setA(int val);
public native int getA();
}
And then the code of my MainActivity:
TestClass a = new TestClass();
TestClass b = new TestClass();
a.setA(5);
b.setA(2);
Log.i("A VALUE",""+a.getA());
Log.i("B VALUE",""+b.getA());
The value 2 is shown two times in the log, this means that the library is loaded only once and it is "shared" by all the instances of the same class. Is it possible to load it multiple times, one for each class instance?