Even though our Java layer functions have no parameters, the native method has two parameters, the first of which is jnienv.
As follows:
Native method:
publicnativestringFromC(); publicnativestringFromCpp();
Native methods:
jstring Java_com_example_jni_MainActivity_stringFromC(JNIEnv* env,jobject thiz){ return (*env)->NewStringUTF(env,"I am from C""C" jstring Java_com_example_jni_MainActivity_stringFromCpp(JNIEnv* env,jobject thiz){ return env->NewStringUTF("I am from C++");}
JNIENV is an interface pointer to the available JNI function table that uses the functions of the virtual machine through the various functions provided by the JNIEnv interface pointers. JNIEnv is a pointer to the thread-local data, and the thread-local data contains pointers to the thread table. The functions that implement the native methods will jnienv the interface pointers as their first arguments.
The native code is c and the native code is C + + its syntax for invoking the JNI function is different , in C code, JNIENV is a pointer to the JNINATIVEINTERFACE structure, in order to access any of the JNI functions, the pointer needs to be dereferenced first. Because the JNI function in C code does not understand the current JNI environment, the jnienv instance should be passed as the first parameter to each JNI function caller.
The correct wording should be the following:
jstring Java_com_example_jni_MainActivity_stringFromC(JNIEnv* env,jobject thiz){ return(*env)->NewStringUTF(env,"I am from C");}
However, in C + + code, JNIENV is actually a C + + class instance, and the JNI function exists as a member function because the JNI method has already accessed the current JNI environment, so the JNI method call does not require the JNIENV instance as a parameter, in C + +, The same function code should be done as follows:
"C" jstring Java_com_example_jni_MainActivity_stringFromCpp(JNIEnv* env,jobject thiz){ return env->NewStringUTF("I am from C++");}
This project source code download
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.
JNIEnv Parameters of NDK development