android jni返回多个变量

时间:2015-03-14 00:02:23

标签: java android java-native-interface

如何从jni函数返回多个变量,避免数组mangaling的开销?

3 个答案:

答案 0 :(得分:21)

我可以提出三种不同的方法来实现它。

<强>回调

从您的JNI代码中调用Java方法,该代码接受多个参数,在Java代码中的某处设置一个变量,从方法返回时可以检索该变量。

JNIEXPORT void JNICALL Java_my_package_name_JNIReturnExample_returnWithJavaCallback(JNIEnv *env, jobject javaThis, jfloat param1, jfloat param2)
{
    // Get the class of the current calling object
    jclass clazz = (*env)->GetObjectClass(env, javaThis);

    // Get the method id of the instance method: void javaCallback(float, float) in my.package.name.JNIReturnExample
    jmethodID callback = (*env)->GetMethodID(env, clazz, "javaCallback", "(FF)V");

    // Calls my.package.name.JNIReturnExample#javaCallback(float, float);
    (*env)->CallVoidMethod(env, javaThis, callback, param1, param2);
}

返回一个新的Java对象

在JNI中实例化Java对象(my.package.name.JNIReturnExample),并将其返回给Java。

JNIEXPORT jobject JNICALL Java_my_package_name_JNIReturnExample_returnObjectValue(JNIEnv *env, jobject javaThis, jfloat param1, jfloat param2)
{
    // Get the class we wish to return an instance of
    jclass clazz = (*env)->FindClass(env, "my/package/name/JNIReturnObject");

    // Get the method id of an empty constructor in clazz
    jmethodID constructor = (*env)->GetMethodID(env, clazz, "<init>", "()V");

    // Create an instance of clazz
    jobject obj = (*env)->NewObject(env, clazz, constructor);

    // Get Field references
    jfieldID param1Field = (*env)->GetFieldID(env, clazz, "param1", "F");
    jfieldID param2Field = (*env)->GetFieldID(env, clazz, "param2", "F");

    // Set fields for object
    (*env)->SetFloatField(env, obj, param1Field, param1);
    (*env)->SetFloatField(env, obj, param2Field, param2);

    // return object
    return obj;
}

将Java对象作为参数传递,并设置其字段

在Java代码中创建Java对象的新实例,并将该对象作为参数传递给JNI函数。

JNIEXPORT void JNICALL Java_my_package_name_JNIReturnExample_setObjectFields(JNIEnv *env, jobject javaThis, jobject obj, jfloat param1, jfloat param2)
{
    // Get the class of the input object
    jclass clazz = (*env)->GetObjectClass(env, obj);

    // Get Field references
    jfieldID param1Field = (*env)->GetFieldID(env, clazz, "param1", "F");
    jfieldID param2Field = (*env)->GetFieldID(env, clazz, "param2", "F");

    // Set fields for object
    (*env)->SetFloatField(env, obj, param1Field, param1);
    (*env)->SetFloatField(env, obj, param2Field, param2);
}

请注意,无论您决定使用哪种方法,都应该缓存各种JNI类型jclass, jmethodID, jfieldID,因为JNI查找操作很慢,只需要执行一次。


<强>缓存

要在Callback方法中缓存JNI引用,并使用以下方法调用它们:

static jclass myCallbackClass;
static jmethodID myCallbackMethod;

/**
 * Call this method in JNI_OnLoad
 */ 
void CacheCallback()
{
    // Get a reference to the Callback class
    jclass clazz = (*env)->FindClass(env, "my/package/name/JNIReturnExample");

    // Store a global reference, since the local one will be freed when returning from the function.
    myCallbackClass = (*env)->NewGlobalRef(env, clazz);

    // Get a reference to the static callback method
    jmethodID callback = (*env)->GetStaticMethodID(env, myCallbackClass, "jniCallback", "(II)V");

    // jmethodID doesn't need a NewGlobalRef call
    myCallbackMethod = callback;
}

/**
 * Call this method in JNI_OnUnload
 */
void ReleaseCallback()
{
    (*env)->DeleteGlobalRef(env, myCallbackClass);
    myCallbackClass = NULL;

    // jmethodIDs are safe to keep without an explicit global reference, for this reason, we don't need to delete the reference either.
    myCallbackMethod = NULL;
}

答案 1 :(得分:2)

还有一种方法 - jobjectArray

就个人而言,我需要返回一对字符串和int。

使用env-&gt; NewStringUTF方法可以轻松地将字符串放入此类数组中 但是对于int,必须构造一个包装器。

原生部分:

extern "C"
JNIEXPORT jobjectArray JNICALL
Java_com_example_myapp_CallingClass_writeCfgFile(
    JNIEnv *env,
    jobject obj,
    jobjectArray stringArray,
    jstring filepath){
...     
    std::pair<int,string> ret = generate_config(filePath, reqMsgTypes);

    jobjectArray retobjarr = (jobjectArray)env->NewObjectArray(2, env->FindClass("java/lang/Object"), NULL);
    env->SetObjectArrayElement(retobjarr, 0, NewInteger(env, ret.first));
    env->SetObjectArrayElement(retobjarr, 1, env->NewStringUTF(ret.second.c_str()));

    return retobjarr;
}

jobject NewInteger(JNIEnv* env, int value){
    jclass integerClass = env->FindClass("java/lang/Integer");
    jmethodID integerConstructor = env->GetMethodID(integerClass, "<init>", "(I)V");
    return env->NewObject(integerClass, integerConstructor, static_cast<jint>(value));
}

在Java方面:

Object[] resp = writeCfgFile( msgTypes,  filePath");
Integer i = (Integer)resp[0];
String str = (String)resp[1];

答案 2 :(得分:-1)

您可以使用包装。然后返回包装器。首先创建一个包含一些内部变量的类,这些变量是您想要返回的多个内部变量。然后使用此类作为返回类型。