Search code examples
javaandroidcjava-native-interface

Is it possible that JNI function return integer or boolean?


JAVA Code

boolean b = invokeNativeFunction();
int i = invokeNativeFunction2();

C code

jboolean Java_com_any_dom_Eservice_invokeNativeFunction(JNIEnv* env, jobject obj) {
    bool bb = 0;
    ...
    return // how can return 'bb' at the end of the function?
}

jint Java_com_any_dom_Eservice_invokeNativeFunction2(JNIEnv* env, jobject obj) {
    int rr = 0;
    ...
    return // how can return 'rr' at the end of the function?
}

Is it possible that JNI function return integer or boolean? If yes, How I can do that?


Solution

  • Yes, just return the value directly.

    JNIEXPORT jint JNICALL Java_com_example_demojni_Sample_intMethod(JNIEnv* env, jobject obj,
        jint value) {
        return value * value;
    }
    
    JNIEXPORT jboolean JNICALL Java_com_example_demojni_Sample_booleanMethod(JNIEnv* env,
        jobject obj, jboolean unsignedChar) {
        return !unsignedChar;
    }
    

    There is a map relation between Java primitive type and native type, reference here.