android jni 返回多个变量

2022-09-05 00:04:11

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


答案 1

我可以想出三种不同的方法来做到这一点。

回调

从 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 类型 ,因为 JNI 查找操作速度很慢,实际上只需要执行一次。jclass, jmethodID, jfieldID


缓存

要在回调方法中缓存 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;
}

答案 2

还有一种方法 - jobjectArray

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

可以使用 env->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];

推荐