0

I'm new to JNI and C++, here I'm going to pass an array from Java in Android to JNI, and in the JNI just need sort the array in Bubble way. However, it's seems this never get worked, I mean the array never get sorted. I don't know whether the array had never been passed or there're some problems with the functions in JNI.

And following is the demo-code.

File .h :

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_jni_jniDemo_JniInterface */

#ifndef _Included_com_jni_jniDemo_JniInterface
#define _Included_com_jni_jniDemo_JniInterface
#ifdef __cplusplus

extern "C" {
#endif

/*
 * Class:     com_jni_jniDemo_JniInterface
 * Method:    getString
 * Signature: ()Ljava/lang/String;
 */

JNIEXPORT jintArray JNICALL Java_com_jni_jniDemo_JniInterface_arraySort 
(JNIEnv *, jobject, jintArray);

#ifdef __cplusplus
}
#endif
#endif

File .cpp :

int sort(int &a, int &b){
    a = a + b;
    b = a - b;
    a = a - b;
}

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

    jint *arr = env -> GetIntArrayElements(jArr, 0);
    int len = env -> GetArrayLength(jArr);

    for(int i = 0; i < len; i++){
        for(int j = 0; j < len - i -1; j++){
            if(arr[j] > arr[j + 1]){
                sort(arr[j], arr[j + 1]);
            }
        }
    }

    env -> ReleaseIntArrayElements(jArr, arr, JNI_COMMIT);

    return jArr;
}

File .java :

private int[] array = {7, 34, 2, 44, 6, 0, 127, 9};
private int[] newArray;    

for(int i = 0; i < array.length; i++){
    System.out.println("Before sort:" + String.valueOf(array[i]));
}

jniInterface = new JniInterface();
newArray = jniInterface.arraySort(array);

for(int j = 0; j < newArray.length; j++){
    System.out.println("After sort:" + String.valueOf(newArray[j]));
}

Any ideas?

frank jorsn
  • 489
  • 1
  • 9
  • 27
  • 1
    `JNIEXPORT jstring JNICALL Java_com_jni_jniDemo_JniInterface_arraySort` - Looks like you've got the return type specified as `jstring`. – Mike M. Apr 05 '17 at 03:26
  • Plus, your sort function makes my head hurt. – MuertoExcobito Apr 05 '17 at 03:27
  • 1
    @Mike M I'm so sorry, it's really my mistake! Thanks! – frank jorsn Apr 05 '17 at 04:59
  • @Mike M I had edit my problem! This time I got another problem. – frank jorsn Apr 05 '17 at 05:29
  • 1
    _"This time I got another problem"_. **What** problem? You need to be more specific. Also, since you seem to be using `jArr` as an in/out-parameter, why does your jni function need to return anything at all? – Michael Apr 05 '17 at 05:41
  • Btw, you're leaking memory by using `JNI_COMMIT`. If you want to both copy back _and_ free `arr` you should use 0 as the `mode`. – Michael Apr 05 '17 at 08:40
  • @Michael Ok, I got it. Well, 0 is better than JNI_COMMIT for this demo. But anyway, the real problem is the code never do as I wish, the array never got sorted. That is the problem what stuck me, I have no idea about this. – frank jorsn Apr 05 '17 at 10:16
  • Use a debugger or log prints to see what's going on in your native function. – Michael Apr 05 '17 at 11:10

1 Answers1

0

Take a look here to see how to handle arrays in JNI. This sample will show you how to pass array back and forth:

http://jnicookbook.owsiak.org/recipe-No-013/

Note that you don't want to return the values as function result. When you release with JNI_COMMIT, values are available in the object you have passed as argument.

Oo.oO
  • 12,464
  • 3
  • 23
  • 45