Initial use of the NDK under Linux

Source: Internet
Author: User

Resources:

1, the copyright notice: reproduced in the form of hyperlinks to the original source of the article and the author's information and this statement

Http://eshock.blogbus.com/logs/61459223.html

2, http://www.cnblogs.com/anyanran/archive/2010/11/22/ndk1.html

1. Platform and Software information

View by uname-a :

3.13. 0--generic #:$ i686  i686 i686 Gnu/linux

To verify that the environment variables are configured correctly, you can:

java-version View:

" 1.7.0_67 "  1.7. 0_67-24.65-b04, Mixed mode)

ndk-build View:

Find Application project directory!     Android ndk:please define the Ndk_project_path variable to the it.     /HOME/ANNA/DOWNLOADS/ANDROID-NDK-R10B/BUILD/CORE/BUILD-LOCAL.MK:148: * * * android ndk:aborting    .  Stop.

The ADT and CDT plugins are also installed in eclipse to run the Java-written application properly.

2. Write Java code

New Android project, the package name is set to "Com.anna", the class name is Jnitest, in which implement native method plus and SayHello, respectively, used to pass back the string and the number to Java. The code is as follows:

 PackageCom.anna;ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity; Public classJnitestextendsactionbaractivity { Public nativeString SayHello ();  Public native intPlusintXinty); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (r.layout.activity_jni_test); intx = Plus (3,2); System.out.println ("The result in Java is:" +x); //sets the returned string to the title of the app       This. Settitle ("" + This. SayHello ()); }Static{system.loadlibrary ("Mylib"); }}

3. Generate Com_anna_jnitest.h Header file

The implementation of the native method requires the Javah command to generate the corresponding header file, using the form defined by the native method in the header file to be implemented in the C + + language, and the current Java code is not properly compiled with the Javac command. Therefore, a Java file containing only the native method is extracted and compiled to obtain relevant information.

 Public class  jnitest{  publicnative  String SayHello ();  public nativeint plus (int x,int  y);}
To generate a JniTest.h file using the javah-jni jnitest command
/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class Jnitest*/#ifndef _included_jnitest#define_included_jnitest#ifdef __cplusplusextern "C" {#endif/** class:jnitest * Method:sayhello * Signature: () ljava/lang/string;*/jniexport jstring jnicall Java_jnitest_sayhello (jnienv*, Jobject); /** class:jnitest * method:plus * Signature: (II) I*/jniexport jint jnicall java_jnitest_plus (jnienv*, Jobject, Jint, Jint); #ifdef __cplusplus}#endif#endif

two of them Jniexport sentence is the desired native method description, remember to add the package name before the function name, that is java_jnitest_sayhello--> Java_com_anna_jnitest_ SayHello

4. Write A. c file

Under Path, create a new folder named Jni, in which you create a new MYLIB.C (according to System.loadlibrary ("Mylib"); name) with the contents:

#include <string.h><jni.h>*env, Jobject obj, Jint x, Jint y) {  return x+*env,  jobject obj)
{ return"");}

In which the return of type int and string data is implemented, the data types in C and Java do not correspond exactly, so it is easy to program using the Jint, jstring style naming method.

5. Writing android.mk and Application.mk files

Under the jni folder , create a new file android.mk with the following content:

Local_path: = $ (call my-dir) include $ (clear_vars) local_module    := mylib local_src_files: = Mylib.cinclude $ (build_shared_library)

Where Local_module represents the file name of the dynamic-link library. So file to be generated (the generated file automatically adds the Lib header, that is, the resulting so file is named "Libmylib.so"), and Local_src_files represents the path where the C file is located.

Create a new file Application.mk under the total project directory (that is, equivalent to the SRC directory), with the following content:

App_project_path: = $ (call my-dir) app_modules      := mylib

The app_modules is the same as the content of Local_src_files.

6. Generate Libmylib.so File

Toggle the command-line tool into the JNI directory , using the -ls command to view the folder containing:

Android.mk   MYLIB.C

After using the ndk-build command, the post-run command line output is as follows:

[Armeabi] Install        : libmylib.so = libs/armeabi/libmylib.so

According to the output, the output of so file is stored in the entire project ibs/armeabi/path , if not, right-click Project "Refresh" appears.

7, run the project, view the output results

The output in Logcat is:

The emulator interface output is:

  

(Note: Hello World is an engineering default established by TextView ...)

Initial use of the NDK under Linux

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.