Android NDK compiles c/A + + for so shared objects __c++

Source: Internet
Author: User
Concept

jni(Java Native Interface,java local interface) that enables Java and other languages to interact (mainly C/s + +), such as: Java programs invoke C + + through JNI A DLL dynamic link library that is written to run on Windows.

SO (Shared object), a dynamic library in a Linux system, similar to a DLL in a Windows system.. So is sometimes called directly and sometimes participates in the compilation. Android is developed by the Linux kernel, so it is also used in the Android system. So.

The android NDK(Android Native Development Kit) is a series of tools provided by Google to simplify the process of compiling a C + + dynamic library into a. So library through JNI. NDK integrates the crossover compiler and provides the corresponding. mk file isolation CPU, platform, ABI, and so on, developers simply need to modify the Mk file, execute the compilation script can create. So.

the relationship between NDK and JNI:

JNI is a mechanism for Java to interact with other languages and is a feature of the Java language itself, regardless of Android.

Android is divided into application application layer, application Framework application frame layer, Libraries class library, Linux kernel kernel. In the application framework layer and above, the Java language is used for development, under which Android's own class libraries, drivers are written with C + +, and then through JNI provide interfaces to the upper-level Java calls. So, the Android framework uses a lot of JNI technology to let application-tier developers use Java to manipulate C + +.

The usual Android development is done on the application framework layer and above, but sometimes it needs to be implemented on the ground floor. Obviously, the bottom of the development is much more complex, NDK is Google's launch to help developers through C + + writing application of the development package, including some of the android bottom of the common C/s + + header files, library files, documentation and sample code. installation

To configure NDK under Windows:
1, Download: android-ndk-r13b-windows-x86_64.zip
2, Decompression: D:\sdk\android-ndk-r13b
3. Environment variable:

android_ndk:d:\sdk\android-ndk-r13b 
path:%android_ndk%

4, verify: cmd command input ndk-build if the "Android ndk:could not find Application project directory" said NDK installed correctly, but not to compile the project just. the Hello-jni in the NDK

Google's NDK Samples can be downloaded, taking the HELLO-JNI project as an example to illustrate the use of JNI in NDK:

HELLO-JNI Engineering directory Structure: HELLO-JNI
Jni
ANDROID.MK (Compilation configuration file) Hello-jni.c (. C code) Res src
Com/example/hellojni/hellojni.java (. Java code) test ndk-build

JNI is independent of the existence of NDK, and understanding JNI only has a better understanding of NDK. [Reference 1].

When using JNI independently, it is necessary to compile the C + + code into a dynamic library by using the Cygwin and other compiling tools, while NDKR7 starts with the integration of the cross compiler and the Ndk-build.cmd script, which the developer can execute directly to complete the compilation work.

Run the following command to compile:

D:\>CD Sdk\android-ndk-r13b\samples\hello-jni    //positioning to Hello-jni
D:\sdk\android-ndk-r13b\samples\ Hello-jni>ndk-build    //compilation

P.S.
When the Ndk-build command prompt cannot find the project, you can view the path relationship in the project's Android.mk file, navigate to the correct path, and use the Ndk-build command. Alternatively, you can set the engineering path to the NDK_PROJECT_PATH environment variable.

If compiled successfully, \HELLO-JNI will have two more folders \libs and \obj. which
The \libs directory is compiled with a different CPU type. So file, which is selected as needed when it is actually used
\obj are other files generated during compilation (such as. O intermediate files, or debug files)
Call

Import into the Hello-jni project in Eclipse-adt, establish a folder \libs with \src siblings, and put the compiled. So in the folder. (Here I import directly into the compiled project folder)

Run the results directly:
android.mk

The Ndk-build command actually executes the%ndk_project_path%/jni/android.mk makefile file, and if the%ndk_project_path% environment variable is not configured, you need to navigate to the engineering directory. Also, the. c and compile configuration file android.mk to be compiled will need to be located in a directory named JNI.

ANDROID.MK is a NDK compiled configuration file that defines the. c files that need to be compiled, the dependent. h header files, the compiled so library name, and so on, which is the key to compiling with NDK.

Android.mk
Local_path: = $ (call My-dir)

include $ (clear_vars)

local_module    : = Hello-jni    // The compiled library name is libhello-jni.so
local_src_files: = hello-jni.c  //compile source file

include $ (build_shared_library)
android.mk Common Configuration

Common configurations will accumulate updates at work ... application.mk

Different CPUs compile the. So different, Ndk-build compiles all CPUs by default. So. The App_abi specified CPU, such as armeabi-v7a, can be specified through the Application.mk file.

Application.mk
App_abi: =  All//compile all CPU's. So
app_abi: = armeabi-v7a  //compile armeabi-v7a. So
Eclipse Integrated NDK compilation Operations

The previous operation of the NDK using the cmd command, obviously in the work is very inconvenient, fortunately, these operations can be integrated into the Eclipse button, a key to complete the task.
Reference: Eclipse integrated JNI and ANDROIDNDK operations Eclipse add NDK to carry JNI code hints

NDK, which has a different CPU's JNI operating source code, can be added to eclipse to help write JNI's C + + codes in Eclipse.

1, right-click Engineering-> Android Tools-> Add Native Support, then click the pop-up window finish can
2. Right-click Engineering-> Properties->/C + + general-> Paths and symbols, select C,cpp language-> Add, use file system to select an Android version of the NDK in specific CP U implementation of include, OK.

Include Path
D:\sdk\android-ndk-r13b\platforms\android-19\arch-arm\usr\include

Tip Effect:
Reference What is the JNI entry--hellojni so format file? Android JNI Development data –NDK Environment build Android framework introduction of NDK and JNI Eclipse integrated JNI and ANDROIDNDK operations

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.