Using rust in android through jni, jarray has non-array type issue

I was using rust in android through a jni bridge jni-rs package.
In this case I am not able to convert my arrays/values into rust ones.
This error pops up when I am converting JByteArray into rust vectors or even trying to get the length of it:

JNI DETECTED ERROR IN APPLICATION: jarray argument has non-array type: java.lang.Class<com.example.calc.HomeKt> in call to GetArrayLength

My rust code is like this:

#[no_mangle]
pub unsafe extern "C" fn Java_com_example_calc_HomeKt_infer(
    env: JNIEnv,
    inputImage: JByteArray,
) -> i64 {
        let size = env.get_array_length(&inputImage).unwrap();
    }

And I am calling it in android (kotlin) like

external fun infer(inputImage: ByteArray, imageSize: String): Int;

infer(byteArray)

and the bytearray is formed using this function in kotlin :

fun uriToByteArray(context: Context, uri: Uri): ByteArray {
    val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, uri)
    val outputStream = ByteArrayOutputStream()
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
    return outputStream.toByteArray()
}

Found the issue... The second parameter is a reference to the jclass object, not the arguments passed... Fixed it by changing the definition:

pub unsafe extern "C" fn Java_com_example_calc_HomeKt_infer(
    env: JNIEnv,
    thiz: jobject,
    inputImage: JByteArray)

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.