简体   繁体   English

JNI使用Android 5+将参数传递回Java

[英]JNI passing of arguments back to Java with Android 5+

I am having this native code to get back an array of shorts :) 我正在使用此本地代码来获取短裤数组:)

extern "C" 
JNIEXPORT jshortArray Java_my_application_CallerClazz_getShortArray(JNIEnv *jenv, jobject self)
{
    jshortArray shortArray = jenv->NewShortArray(size);
    jshort* p2ji = jenv->GetShortArrayElements(shortArray, JNI_FALSE);
    for(int idx = 0; idx < size; idx++)
    {
        p2ji[idx] = srcArray[idx];
    }
    return shortArray;
}

It works fine with all Android versions < 5. Since Android 5 the native code itself works fine, but the short[] on the java side is filled just with 0 . 它适用于所有<5版本的Android。自Android 5起,本机代码本身即可正常工作,但Java端的short[]仅填充0

Anybody has a tip why this happens? 有人提示为什么会这样吗?

Quoting from the JNI documentation for Get<PrimitiveType>ArrayElements() : JNI文档中引用Get<PrimitiveType>ArrayElements()

Since the returned array may be a copy of the Java array, changes made to the returned array will not necessarily be reflected in the original array until Release<PrimitiveType>ArrayElements() is called. 由于返回的数组可能是Java数组的副本,因此在调用Release<PrimitiveType>ArrayElements()之前,对返回的数组所做的更改不一定会反映在原始数组中。

So before returning from your C++ function you should call RelaseShortArrayElements with mode == 0 ( 0 means "copy back the content and free the elems buffer" ). 因此,在从C ++函数返回之前,应使用mode == 0调用RelaseShortArrayElements0表示“复制内容并释放elems缓冲区” )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM