The role of JNI in Android is to allow Java to invoke code implemented by C/s + + in order to implement this function. Need to use the NDK Toolkit provided by anrdoid, here do not say how to configure, good trouble, configuration for a long time.
。
。
In essence, Java's code to call C + + is actually to invoke the methods provided by C + +. So, in the first step, we're going to create a class and define a native method. For example, the following:
Jnitest class:
public class Jnitest {public native String getteststring ();}
Can see, in the front of this method, used to Nativekeyword.
Next, we will compile this Java file on the command line. Get a class file. For example, the following:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbglubwlhbnnozw5n/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
Then we can use the Javah command file. Generate a header file for C. In fact Javah This step is not necessary, because the creation of this header file, just to facilitate the replication of this jni in the corresponding method name, because these names are too complex.
One thing to note here is that the Javah command is to be lowered in the root folder of the package, and the corresponding class file must be the complete class name, as seen. Will go back to the SRC folder first. Call the Javah command again.
This way we will generate a header file in the SRC directory. For example, as seen in:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbglubwlhbnnozw5n/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
We can see that its name is com_lms_jni_jnitest.h, in fact it is the package name + class name, we can look inside the content:
/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class Com_lms_jni_jnitest */#ifnd EF _included_com_lms_jni_jnitest#define _included_com_lms_jni_jnitest#ifdef __cplusplusextern "C" {#endif/* * Class: com_lms_jni_jnitest * Method: getteststring * Signature: () ljava/lang/string; */jniexport jstring Jnicall Java _com_lms_jni_jnitest_getteststring (jnienv *, jobject); #ifdef __cplusplus} #endif #endif
We can see. In this there is a method, the name is java_com_lms_jni_jnitest_getteststring, complex enough. In fact, assuming that we know the name rules and know how to implement such a method, we are completely able to not generate this header file. We can write the corresponding C file directly.
Next, create a corresponding C file in the Jni file. The name is worth it and doesn't matter, but for unification. Let's call it jnitest.c. For example, the following:
Here, we also put com_lms_jni_jnitest.h here, this is actually not related, just for the content of the coordination and unification, in general, we will put so C + + implementation of files are placed in the project folder under a folder called JNI below.
The following is the implementation of the native method in Jnitest.c, getteststring. For example, the following:
#include <stdio.h> #include <stdlib.h> #include <jni.h>jniexport jstring jnicall java_com_lms_jni_ Jnitest_getteststring (jnienv *e, Jobject obj) {return (**e). Newstringutf (E, "Hello from Jnitest Function");}
In this c file, we see. Does not refer to the header file Com_lms_jni_jnitest.h, but only refers to the general C/s library files, such as stido.h and stdlib.h files, and so on, note here. We will also refer to the jni.h file, the jni.h file is very important in JNI programming a header file, about the data type in Java and the data type in JNI is defined in this file, perhaps will look at this jni.h file.
After implementing the method in the Jnitest.c file above. The implementation of the C + + side is actually achieved. Then the next step is to compile the C file into so files called by Android.
Why is the so file, which is because Android is essentially a Linux system. So the JNI library file that it calls is the so form.
The NDK library provided by Android provides ndk-build commands to implement this compilation process. But before we do, we need to create a android.mk file first. This is a simple, small make file with the following content such as:
Local_path: = $ (call My-dir) include $ (clear_vars) Local_module: = com_lms_jni_hwdemolocal_src_files: = hwdemo.c Jnitest.c include $ (build_shared_library)
Over here. We will define several variables:
Local_path: The value is call My-dir, and My-dir is a macro function that returns the path where the android.mk is located, in this case, the JNI directory.
Include $ (clear_vars), this command clears all variables from the local start, such as Local_module, but with one exception, the Local_path above.
Local_module: The so package name to generate is also the name of the Java code loaded in Android.
Local_src_files: The source file to be compiled. As in here. There are hwdemo.c and jnitest.c and so on.
Include $ (build_shared_library): Indicates that a dynamic link library is generated.
After defining such a android.mk file, call the Ndk-build command at the command line, such as the following:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbglubwlhbnnozw5n/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
After the command was implemented, we were able to see an so library in the Libs under the project folder. For example, the following:
Here, the implementation of the JNI is over, and the next step is how to use the native method in Android.
We have created an activity that simply places a TextView control in it, with its layout such as the following:
<?XML version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:orientation=" Vertical "> <textview android:id=" @+id/tvjni " android:layout_width=" Wrap_content " Android : layout_height= "wrap_content" android:text= "test"/></linearlayout>
And then in the activity, we're going to load this so library. For example, the following:
public class Hwdemo extends Activity {static {system.loadlibrary ("Com_lms_jni_hwdemo");//load so library}public native String Printhello (); @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); TextView TV = (TextView) Findviewbyid (R.ID.TVJNI); Jnitest jnitest = new Jnitest ();//method Tv.settext to invoke Jnitest file (jnitest.getteststring ());}}
1) load the so library file with a static block of code. Can see here, this name is the Local_module value defined in ANRDOID.MK.
2) Create the Jnitest object, call its getteststring () method, and finally display the results such as the following:
Here, in a simple example, we've identified how to use JNI in Android to invoke the C + + method.
Finally, let's summarize these steps:
1) Create a Java class file. and define the native method. such as the Jnitest class.
2) Use Javac to generate class files, and then go back to the SRC folder, using Javah to generate C + + header files, note here. Javah command to be lowered in the root folder of the package, the corresponding class file must be the full class name. For example, the following:
In the src folder: Javah com.lms.jni.JniTest. In the above, can also see Javac, is back to the SRC folder, and then call Javah.
3) write the corresponding C files, such as jnitest.c, in the implementation of the C + + method, remember to put in the JNI directory below.
4) write the Android.mk file and use the Ndk-build command to generate the so file.
5) in Android, using static block code, call the System.loadlibrary method to load the so library file.
6) Call the method of the previously defined Jnitest class in the Java logic.
End.
Source Download !
Learn about JNI in Android (0) Simple sample, easy to get started