Installation steps:
1. Download Android ndk, this does not say much, My is ndk-r6b.
2. Unzip the package to install it. I put it in ~ Below.
Write a hello World
1. use Eclipse IDE to generate a default simplest project, that is, the Hello world with only one textview. note the package name, such as my com. easou. abo. hellojni, class name hellojniactivity.
2. modify Res/layout/main. XML: add an ID for the textview without ID, for example, Android: Id = "@ + ID/label", and then modify the hellojniactivity, as shown below:
Package COM. easou. abo. hellojni; import android. app. activity; import android. OS. bundle; import android. widget. textview; public class hellojniactivity extends activity {public native string sayhello (string name); static {system. loadlibrary ("hello_jni");}/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); textview label = (textview) findviewbyid (R. id. label); label. settext (sayhello ("word "));}}
3. Use javaslse to create a JNI directory under the root directory of the project and three files under the JNI directory. One is Android. mk, the other is hello_a.c, and the other is hello_a.h.
4,Use the terminal to locate the project directory bin/classes, My is (/home/Abo/workspace/hellojni/bin/classes)
5. Execute the command javah COM. easou. abo. hellojni. after the hellojniactivity is executed, a com_easou_abo_hellojni_hellojni_hellojniactivity.h file is generated in the bin/classes directory. Open it with gedit and copy the content to hello_a.h.
6. Now edit hello_a.c and write the implementation.Code, Very simple:
# Include "hello_a.h" // All JNI functions have jnienv and jobject, which can be used to do many things (For details, refer to JNI. h) jstring jnicall encode (jnienv * ENV, jobject thiz, jstring name) {// convert the parameter to the familiar char * const char * Param = (* env) -> getstringutfchars (ENV, name, 0); // you are familiar with char * STR = (char *) malloc (32 * sizeof (char); strcpy (STR, "Hello"); strcat (STR, Param); // release param (* env)-> releasestringutfchars (ENV, name, Param ); // return result jstring ret = (* env)-> newstringutf (ENV, STR); free (STR); return ret ;}
7. You can edit the Android. mk file. It is also quite simple, because we are just a hello World. I don't know how complicated it is to think about it:
# Set the working directory to the current directory local_path :=$ (call my-DIR) # Run the clear parameters include $ (clear_vars) # Set the name of the compiled module local_module: = hello_jni # Set the source file list local_src_files: = hello_a.c # execute the generated Shared Library include $ (build_shared_library)
8. Locate the JNI directory on the terminal and run ~ /Android-ndk-r6b/ndk-build, after success, a libs directory appears under the project directory. Libs/armeabi/libhello_jni.so file.
9. Return to eclipse and run the project.