Android ndk/JNI entry instance (Environment: ubuntu10.10 + Android ndk-r6b)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.