3

I have an android project that uses a c++ library and i need to send an object array to the c++ code.

This is my Java native bind code

class MyLib {
    public static native void testArray(String[] array);
}

This is how i call it

...
MyLib.testArray(new Object[20]);
...

And this is my c++ jni code:

JNIEXPORT void JNICALL Java_com_android_mypackage_MyLib_testArray(JNIEnv * env, jobjectArray arr) {
    __android_log_print(ANDROID_LOG_INFO,"test","test array!!!!!!!!");
    int count = env->GetArrayLength(arr);
    __android_log_print(ANDROID_LOG_INFO,"test","array length %d", count);
     for (int i=0; i<count; i++) {
        __android_log_print(ANDROID_LOG_INFO,"test","--- get item %d", i);
        jstring jkey    = (jstring) env->GetObjectArrayElement(arr, i);
        __android_log_print(ANDROID_LOG_INFO,"test","item %d", i);    
        env->DeleteLocalRef(jkey);
    }
}

When i execute that, logcat shows this and execution ends with sigsev 11

JNI WARNING: jarray arg has wrong type (expected array, got Ljava/lang/Class;) in Lcom/android/mypackage/MyLib;.testArray:([Ljava/lang/Object;)V (GetArrayLength)

Any ideas why my array reference appears to be a class reference? I have no problems with arrays of native types, only with objects.

1 Answer 1

5

I believe your function signature

JNIEXPORT void JNICALL Java_com_android_mypackage_MyLib_testArray(JNIEnv * env, jobjectArray arr)

should look like this:

JNIEXPORT void JNICALL Java_com_android_mypackage_MyLib_testArray(JNIEnv * env, jclass clazz, jobjectArray arr)

You're missing the second parameter - which in static methods is always the Class object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.