JNI function names are divided into three parts:
The first is the Java keyword for Java Virtual Machine recognition; for example, Java_com_example_hellojni_HelloJni_stringFromJNI
Java: keyword
Com_example_hellojni: package name
HelloJni: File Name (originally named hello_jni)
StringFromJNI: function name
Then the caller Class Name (fully qualified class names, with underlines instead of name delimiters );
Finally, it is the corresponding method name, which is separated by underscores.
The JNI function _ parameters are also composed of three parts:
The first is JNIEnv *, which is a pointer to the JNI runtime environment;
The second parameter varies with whether the local method is static or non-static. The second parameter of the non-static local method references the object, the second parameter of the static local method is a reference to its Java class;
Other parameters correspond to common Java method parameters. The parameter types need to be mapped according to certain rules.
Jstring // return type, String
Java_com_example_hellojni_HelloJni_stringFromJNI (JNIEnv * env, jobject thiz)
{
Return (* env)-> NewStringUTF (env, "Hello from JNI! ");
}
Compile the make file:
LOCAL_PATH: = $ (call my-dir) // source file address, which must be in the first line
Include $ (CLEAR_VARS) // clear some variables except LOCAL_PATH
LOCAL_MODULE: = hello-jni // specify the name of the current compilation Module
LOCAL_SRC_FILES: = hello-jni.c // source file
Include $ (BUILD_SHARED_LIBRARY) // compile the current module as a shared link library with the prefix lib and suffix. so
Finally, compile the file in the Cygwin cross-compilation environment and generate the file in the libs directory of the root directory.
Armeabi/libhello-jni.so files
Copy the libs directory to the root directory of Eclipse to use the compiled local method.
Code:
Public class HelloJni extends Activity
{
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
TextView TV = new TextView (this );
TV. setText (stringFromJNI ());
SetContentView (TV );
}
Public native String stringFromJNI ();
Public native String unimplementedStringFromJNI ();
Static {
System. loadLibrary ("hello-jni ");
}
}
From the xtcpcgx Column