The Mystery of Android NDK Development is often exciting for many programmers, but it is not known what it is, due to the recent development of applications, In order to be developed. apk file is not interpreted by others (anti-compilation), consulted a lot of information, including the use of the NDK development, with a curious mind, through online video education website, I initially understand the NDK's mystery, good things to share, then we will come together to meet the Android NDK development.
I. Background of the NDK generation
The Android platform has supported C and C + + development since its inception. As we all know, the Android SDK is based on Java implementations, which means that third-party apps that are developed based on the Android SDK must use the Java language. But this is not the same as "third-party apps can only use Java." When the Android SDK was first released, Google announced that its virtual machine Dalvik supported JNI programming, which means that third-party applications could call their own C dynamic library through JNI, i.e., on Android, the "java+c" programming is always possible.
However, Google also said that the use of Native SDK programming compared to the Dalvik virtual machine has some disadvantages, the Android SDK documentation, no JNI help found. Even if third-party application developers use JNI to complete their own C dynamic-link library (SO) development, how so is packaged with the app as an APK and released? There are also technical barriers in this. For example, the program is more complex, compatibility is difficult to protect, unable to access the framework api,debug more difficult and so on. Developers need to use them at their own discretion.
So the NDK was born. The NDK full name is native development Kit.
The release of the NDK enabled the development of "Java+c" to become officially supported by the official development approach. The NDK will be the beginning of the Android platform to support c development.
Second, why use the NDK
1. Protection of the code. Because the Java layer Code of the APK is very easy to decompile, the C + + library is more difficult to reverse.
2. The existing open Source library is easy to use. Most of the existing open source libraries are written in C + + code.
3. Improve the efficiency of the execution of procedures. Use c development for application logic that requires high performance to improve application execution efficiency.
4. Easy to transplant. The library can be used in other embedded platforms easily.
Iii. introduction of NDK
1.NDK is a collection of a range of tools
The NDK provides a range of tools to help developers quickly develop C (or C + +) dynamic libraries and automatically package so and Java applications together as APK. These tools are great for developers.
The NDK integrates the cross compiler and provides the corresponding MK file isolation CPU, platform, ABI and other differences, developers simply need to modify the Mk file (stating "which files need to compile", "compilation characteristics Requirements", etc.), you can create a so.
The NDK can automatically package so with Java applications, greatly reducing the developer's packaging effort.
2.NDK provides a stable, feature-limited API header file declaration
Google expressly declares that the API is stable and supports the currently published API in all subsequent releases. As seen from this version of the NDK, these APIs support a very limited number of features, including: C standard library (LIBC), Standard math Library (LIBM), compression library (LIBZ), log Library (Liblog).
Iv. Building the NDK development environment
1. Download install android NDK
Address: http://developer.android.com/sdk/ndk/index.html
2, Installation DNK
There are many ways to build DNK development on the Linux environment, but today we have to make it clear that the NDK is now very simple to install and support many platforms. Today we will use win platform to introduce how to configure the development environment and Design and development DNK program. First is the installation, the above is provided by the official website address, we can choose to download according to their own needs, but because in the domestic restrictions on access to foreign sites, so download must FQ,FQ for some programmers is a difficult, here I provide you with a: ANDROID-NDK-R9
Once downloaded, installing DNK is very simple, we unzip the downloaded. zip file, and then copy the file to one of our disks.
3. Configure NDK Environment variables
Learn Java children's shoes, for the configuration environment variables must be not unfamiliar, here I will briefly explain how to configure the DNK development environment, right-click My Computer--Properties--Advanced system settings--advanced--environment variables
In the open environment variable, locate the path in the system variable, double-click and add our DNK installation directory at the end.
Here our win environment configuration Android NDK development environment has been completed, finally we verify our configuration through the command line: Click Start-and-enter CMD carriage return-Input ndk-build Enter, if the printed information indicates that our configuration is complete.
V. Running the NDK-provided case demo
Run the case demo before we need to ensure that our Android development environment has been configured, for the Android development environment configuration, I will not repeat, if you have any questions can be separate message discussion. Let's start by importing the case demo, open the Eclipse Android development environment, then right-click on an empty area and select Import-->android Import:
After importing the project, let's look at the directory structure:
The files in the JNI directory are our. c files, and the. so file under the obj directory is our ndk compiled file, which is our library file. Of course there is no. so file in the project when it was imported, we need to compile the build, generate the method: Start-up input cmd enter-->CD our project catalog enter, see Operation decomposition:
Here we will be able to allow the test if the demo is finished.
Android NDK Development First Knowledge