Lab notes on Android ndk

Source: Internet
Author: User

I always wanted to study ndk, but it was always because it could not be used in the project. Today, I am idle, and hello World.

 

As an example, Google was used before doing anything. As a result, there are a lot of articles about ndk. Therefore, it is very easy to do this helloworld, but you should note that due to some changes to ndk R4, never use previous articles to draw images from the gourd.

 

 

Installation steps:

1. Download, this does not say much, flip the wall, download android-ndk-r4b-linux-x86.zip or android-ndk-r4b-windows.zip.
Note: I did an experiment on Ubuntu 10.4 32bit desktop. If you use Windows, cygwin must be used. For more information, see.

 

2. Unzip the package to install it. It is as simple as ever. I put it under/usr/android-ndk-r4b.

 

 

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 CN. sharetop. android. hello. ndk, class name hellondkactivity.

2. modify Res/layout/main. XML, add an ID for the textview without ID, such as Android: Id = "@ + ID/label", and then modify the hellondkactivity, as shown below:

 

Public native string sayhello (string name); <br/>... <br/> static {<br/> system. loadlibrary ("hello_jni"); <br/>}</P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> textview label = (textview) findviewbyid (R. id. label); <br/> label. settext (sayhello ("China"); </P> <p >}< br/>

3. Enter the project directory, for example, my/home/Yancheng/myworkspace/Hello, and create a sub-directory sudo mkdir JNI.

 

4. Enter the JNI directory and create three new files (directly create in eclipse). One is Android. mk, the other is hello_a.c, and the third is hello_a.h.

 

5. compile and run the activity in eclipse and go to the bin directory to get the JNI header file. Command: javah CN. sharetop. android. hello. ndk. hellondkactivity, which processes the native defined in this class and obtains the cn_sharetop_android_hello_ndk_hellondkactivity.h file.

 

6. Open the header file and copy the content to hello_a.h. This is automatically generated.

 

/* Do not edit this file-it is machine generated */<br/> # include <JNI. h> <br/> # include <string. h> <br/>/* Header for class cn_sharetop_android_hello_ndk_hellondkactivity */</P> <p> # ifndef _ activities <br/> # DEFINE _ activities <br/> # ifdef _ _ cplusplus <br/> extern "C" {<br/> # endif <br/>/* <br/> * class: cn_sharetop_android_hello_ndk_hellondkactivity <br/> * method: sayhello <br/> * Signature: (ljava/lang/string;) ljava/lang/string; <br/> */<br/> jniexport jstring jnicall java_cn_sharetop_android_hello_ndk_hellondkactivity_sayhello <br/> (jnienv *, jobject, jstring ); </P> <p>/* <br/> * Class: cn_sharetop_android_hello_ndk_hellondkactivity <br/> * method: plus <br/> * Signature: (ii) I <br/> */<br/> jniexport jint jnicall java_cn_sharetop_android_hello_ndk_hellondkactivity_plus <br/> (jnienv *, jobject, jint, jint ); </P> <p> # ifdef _ cplusplus <br/>}< br/> # endif </P> <p>

 

7. Edit hello_a.c and write the implementation code. It is very simple:

 

# Include "hello_a.h" </P> <p> // All JNI functions have jnienv and jobject, which can be used to do many things (For details, refer to JNI. h) <br/> jstring jnicall java_cn_sharetop_android_hello_ndk_hellondkactivity_sayhello (jnienv * ENV, jobject thiz, jstring name) {</P> <p> // convert the parameter to a familiar char * <br/> const char * Param = (* env)-> getstringutfchars (ENV, name, 0); <br/> // you are familiar with it. <br/> char * STR = (char *) malloc (32 * sizeof (char )); <br/> strcpy (STR, "hello"); <br/> strcat (STR, Param ); </P> <p> // release Param <br/> (* env)-> releasestringutfchars (ENV, name, Param ); <br/> // return result <br/> jstring ret = (* env)-> newstringutf (ENV, STR); </P> <p> free (STR ); <br/> return ret; <br/>}</P> <p>

 

8. You can edit the Android. mk file. It is 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 <br/> local_path :=$ (call my-DIR) </P> <p> # clear parameters <br/> include $ (clear_vars) </P> <p> # Set the name of the compiled module <br/> local_module: = hello_jni </P> <p> # Set the source file list <br/> local_src_files: = hello_a.c </P> <p> # execute to generate a shared library <br/> include $ (build_shared_library) </P> <p>

 

9. Okay, still under Terminal, enter the JNI directory, execute the compilation:/usr/android-ndk-r4b/ndk-build, the output is as follows:

 

Compile thumb: hello_jni <=/home/Yancheng/workspace/hellondk/JNI/hello_a.c <br/> sharedlibrary: libhello_jni.so <br/> install: libhello_jni.so =>/home/Yancheng/workspace/hellondk/libs/armeabi </P> <p>

10. Return to eclipse, refresh, build, run, and you can see the result on your phone.

 

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.