How to return "int array" from native method in java in NDK

I have a built-in function in nativeLib

 public native int [] getArrayNative();

which I use as follows

 private static int[] DEMO_NODES;

 DEMO_NODES =  nativeLib.getArrayNative();

in c code it has

JNIEXPORT jintArray JNICALL Java_com_testing_NativeLib_getArrayNative
  (JNIEnv *env, jobject obj) {

    int array[] = { 0, 1, 0, 1, 2, 1, 2, 3, 2, 3, 1, 2, 1, 2, 3, 2, 3, 1, 2 };
    jintArray temp = (*env)->NewIntArray(env,20);
    temp[0] = array[0];   // gives error
    return temp;    
}

here I want to return the whole guy [], but I can’t figure out how to do this. here I took a new temp array inside, which tried to copy the value of arry [], but it shows an error. so how to do it

+3
source share
1 answer

Use SetIntArrayRegionto fill an array, jintArrayis is just a magic is an internal structure that you can access using indexes.

Prototype void SetArrayRegion (JNIEnv * env, array, jsize start, jsize len, * buf);

+4
source

All Articles