Build the NDK environment on Eclipse &&so file generation &&jni file call

Source: Internet
Author: User

JNI is the Java language provides a mechanism for mutual communication between Java and C + +, and Java can invoke local C/s code via JNI, and the local C + + code can also invoke Java code. JNI is the interface that the local programming interface, Java and C/s, pass through to each other. One of the key reasons for Java's use of native code through C + + is the high efficiency of C + + code.

The NDK is a collection of a range of tools. It provides a list of tools to help developers quickly develop a dynamic library of C + +, and to automatically package so and Java applications together as APK. It integrates the cross compiler, and provides the corresponding MK file isolation CPU, platform, ABI and other differences, developers only need to simply modify the Mk file ("which files need to compile", "compile feature requirements", etc.), you can create a so.

Specific steps for Java to communicate through the JNI mechanism and C + +

1. Write Java classes containing native local methods
2. Generate the header file of C + + language through Javah tool
3. Implement the header file using C + + language
4. Use the cross-compilation tool to compile the C + + native code and finally link to the *.so executable
5, the actual implementation of Java code to communicate with the local C + + code

NDK installation process:

Under the premise that the computer has already configured the Android environment, only need to download the NDK (R7 above version), this is a compressed package, decompression is available. Note: Prior to R7, you must install Cygwin before you can use the NDK. Starting with R7, the Windows version of the NDK provides a ndk-build.cmd script that can be compiled directly with this script. Then just add a builder to the Eclipse Android project to implement the eclipse's auto-compile NDK:

The first time we generated the so file, we first used the example of Hello-jni under the NDK's sample.

1, start eclipse, through the Create project from existing source, select Hello-jni directory, by default option to create the project;

2, observe the HELLO-JNI project directory structure, found a root directory called Jni folder, the folder below there are two files, android.mk and HELLO-JNI.C.

ANDROID.MK is the configuration file used to generate so files, let's see what's Inside:

Local_path: = $ (call My-dir)
Include $ (clear_vars)

Local_module: = Hello-jni
Local_src_files: = Hello-jni.c

Include $ (build_shared_library)

The include $ (build_shared_library) indicates that a dynamic-link library, the so file, is generated, and the resulting library file name is called lib*.so

Local_module: = Hello-jni indicates that a library file named libhello-jni.so will be generated

Local_src_files: = HELLO-JNI.C indicates that the source file that generated the library file is Hello-jni.c

Open the Hello-jni.c file and you will see a function inside it:

Jstring Java_com_example_hellojni_hellojni_stringfromjni (jnienv* env, Jobject thiz)
{
Return (*ENV)->newstringutf (env, "Hello from JNI!");
}

What principle does this function generate from? is based on the function public native String Stringfromjni () in the Hellojni.java file under SRC, generated. The naming rules are:

Java_ (fixed start) Com_example_hellojni (the package name connected with "_") _hellojni (class name) _stringfromjni (function name)

This function can be generated using the Javah command: Enter the bin\classes directory of the project directory using the command line mode, run the Javah Com.example.hellojni.HelloJni, all native functions are generated according to the rules in a header file called HelloJni.h.

3. Create and configure a new builder

1) Click Project->properties->builders->new to create a new builder. Click Program on the popup dialog, click OK;

2) in the popup dialog box "Edit Configuration", configure Tab "Main":

The path in the location where the nkd-build.cmd is required (under the NDK installation directory).

Workingdiretcoty need to fill in the Hellojni's engineering root directory.

3) in "Editconfiguration", Configure Tab "Refresh":

Tick "Refresh Resources upon completion",

Tick "The entire workspace",

Tick "recuresively include sub-folders".

4) in "Editconfiguration", Configure tab "Build Options":

Tick "After a" and "clean",

Tick "During Manual builds",

Tick "During Auto Builds",

Tick "Specify working set of relevant resources".

Click "Specify Resources ..." to check the "JNI" directory of the TESTNDK project, finish!

Save the settings and click OK.

4. Generate So File

Because we tick "During Auto builds", so when the project has changed, so file will be automatically compiled, the correct generation will be able to find more than two folders in the project directory, folder libs\ A file called libhello-jni.so is generated under the Armeabi directory. At this point, the work of generating so files with the NDK is complete.

5. Calls to so files

There is a section of code in the Hellojni.java file:

static {
System.loadlibrary ("Hello-jni");
}

Use LoadLibrary can load the so file, loading the time do not need to write libhello-jni.so, as long as the Hello-jni can be written. The above is the basic development of eclispe+ndk+android

The following recommendation of this blog is Eclispe +jdk in a simple Java project called the C program: http://blog.csdn.net/zygang1988/article/details/7896071

Build the NDK environment on Eclipse &&so file generation &&jni file call

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.