Search code examples
javaandroidcjava-native-interface

How to use integers and booleans as input parameters in a jni function?


I want to call in my java code a jni function with some integer parameter

JAVA Code

invokeNativeFunction(integer1, integer2, boolean1);

C code

jstring Java_com_any_dom_Eservice_invokeNativeFunction(JNIEnv* env, jobject obj,
                                                    jint integer1, // is it correct to put jint?
                                                    jint integer2,
                                                    jbool boolean1) {
}

How to use integers and booleans as input parameter in a jni function?


Solution

  • This the way you write native functions

    Java

    private native void func(int length, boolean flag)
    

    Now using the javah, generate .h file. It will have auto generated function declaration for you

    JNIEXPORT void JNICALL Java_com_lgc_gstream_client_client_func (JNIEnv *, jobject, jint, jboolean);
    

    Include this header file in some file and write the definition of the function. You can use jint and jboolean in code like normal int and bool in C++