When Java and C are invoked, a key-value mapping table is created in the VIRTUAL machine.

Value is a pointer to a function;

Key consists of a function name and a parameter to form a unique value;


Function signature:

// Student[] Xxx(Student[]) ([Ljava/lang/String; )[Ljava/lang/Object // Equivalent Object[] Xxx(String[] s)Copy the code

Another way to write it, mainly the way functions are registered;

// app/src/main/cpp/native-lib.cpp #include <jni.h> #include <string> #define JNI_CLASS_PATH "Com/example/superlea/firstjni/MainActivity" / / define a C + + function extern "C" JNIEXPORT jstring JNICALL fun_in_c_register(JNIEnv *env, jobject instance){ return env->NewStringUTF("this is a test for register!" ); } static JNINativeMethod g_methods[] = {// CPP function name {"_fun_in_java", "()Ljava/lang/String; , (void*)fun_in_c_register}, }; JNI_OnLoad(JavaVM * VM, void *reserved){JNIEnv *env = NULL; vm->GetEnv((void**)&env, JNI_VERSION_1_6); Clazz = env->FindClass(JNI_CLASS_PATH); env->RegisterNatives(clazz, g_methods, sizeof(g_methods)/sizeof(g_methods[0])); Return JNI_VERSION_1_6; return JNI_VERSION_1_6; }Copy the code
Public native String _fun_in_java();Copy the code