0
我該如何正確構建並通過jni傳遞一組不同的對象?我似乎無法將jint轉換爲jobject。 我最初的功能是:如何將jint插入jobjectArray?
extern "C" JNIEXPORT jint ... Func(...)
{
int res = CallNativeFunc();
return res;
}
現在,我修改的功能,因此它會返回對象的數組。第一個對象將是原始的res
,第二個對象將是jbyteArray。
我曾嘗試:
extern "C" JNIEXPORT jobjectArray ... Func(...)
{
std::vector<unsigned char> outData;
int res = CallNativeFunc(&outData);
//construct the returning object array of size 2
jclass objectClass = env->FindClass("java/lang/Object");
jobjectArray results = env->NewObjectArray(2, objectClass, 0);
//construct the 2nd object (a jbyteArray)
jbyteArray SecondObject = env->NewByteArray(outData.size());
//transfer data into the 2nd object
env->SetByteArrayRegion(SecondObject , 0, outData.size(), reinterpret_cast<const signed char*>(outData.data()));
//transfer the 1st and 2nd objects into the return array
env->SetObjectArrayElement(results, 0, static_cast<jint>(res));
env->SetObjectArrayElement(results, 1, SecondObject);
return results;
}
編譯器失敗在下面的行,說它不能轉換成jint jobject:
env->SetObjectArrayElement(results, 0, static_cast<jint>(res));
我在做什麼錯?這是正確的方法來做我想做的事嗎? (將數組「結構」返回給java)