These days in learning the development of the Android NDK, so first let's look at what the Android NDK development is all about.
The NDK (Native Development Kit), the local development tool, simply means that when developing an Android application, the Native function is called in the Java class, and the interface of the Native function is defined in the Java class, However, the native function is eventually implemented by the local C + + code. To put it simply, it is to call C + + functions in Java. As for why to use the NDK, I summed up a bit, for the following reasons:
1. Improve the efficiency of the program operation: it is well known that in the processing of complex operations or the speed, memory requirements of the program, C + + More than Java efficiency is much higher, such as face recognition, OCR character recognition and other image processing programs, with C/s implementation is undoubtedly the first choice.
2. Dealing with hardware: Java is cross-platform because all of the code is interpreted by the JVM (the Java Virtual machine), which masks the differences in the underlying hardware architecture. But if our program needs to deal with the underlying hardware, or interact with the driver, then we need to write the code in C + +.
3. Re-use existing programs: For example, you have already implemented the program in C + +, you want to migrate to the Android platform, you can directly through the NDK to use, no longer be written in Java.
Now, let's talk about the general steps of the Android NDK development.
First, the preparation work: Build the Android development environment, including JDK, ADT (the new version already contains the Eclipse IDE), the SDK and so on the download installs, will not self-Baidu, related articles too many, I do not repeat here.
Second, download the NDK on the official web site: http://developer.android.com/tools/sdk/ndk/index.html. Download the appropriate version of the NDK based on your operating system platform (WINDOWS32 or WINDOWS64 or Linux or Mac), and then double-click Unzip to the appropriate folder.
III. create a new generic Android application (note the minimum supported API version to >=14). If it is novice, do not know this, can Baidu, Google.
Iv. Setting the NDK path in the ADT development environment
Click on the Window menu, select Preference, tap Android to expand, click Ndk, set the NDK path, click Apply as shown:
V. Add native support
Right-click Engineering,->android tools->add Native support. Enter the name of the. So file that will be generated, point to finish, such as:
Vi. setting up paths and Symbols for C + +
Right-click on the project,->properties, C + + general expansion, click on the path and Symbols, on the right includes, click Add, the Ndk folder under the JNI header file and the corresponding C + + header file to add in. This step is very important, otherwise it will be an error, see my other article: http://blog.csdn.net/u013149325/article/details/40371873
Vii. add a class to the package of the project source file that defines the native function interface, such as:
Viii. generate A/C + + header file based on the interface defined in the previous step
You can use the Javah command to generate a header file, as detailed in my other article: http://blog.csdn.net/u013149325/article/details/40263413, I started with the Javah command under the cmd window, It was later discovered that the header file could be generated by configuring the relevant options directly in ADT. I have to say that ADT is too powerful now, if your ADT does not support the relevant features, you can download the latest version.
In the menu bar run->external tools->external Tools configurations, right-program->new, on the right of the main sub-window configuration such as, name can take a name.
Then click Refresh, as checked:
Point Common, tick the following:
Under the menu bar button, click Javah to find the. h header file generated under the Jni folder, as shown on the right
Nine, write C + + source files, compile and build. so files
The. So file is actually a dynamic link library under the Linux system, which is equivalent to our DLL under Windows. The Android system is Linux-based, so it will be used. There are a lot of people on the internet who suggest using Cygwin to generate. So files, and I've done it myself, but it's more convenient to automatically generate it in an ADT integrated development environment.
After the project is compiled, a. cpp file with the same name as the project is generated under the Jni folder, open it, and write C + + code here. Such as:
This is to include the. h header file that was generated in the previous step, and then implement the native function in this area. If you do not understand these types of jnienv *, Jclass, Jintarry, you can learn about JNI programming in depth. If you need multiple. h and. cpp files, just put them all in the Jni folder.
Right-click Project,properties->builders->new, create a new builder. Click Program on the Pop-up dialog box, click OK, and in the popup dialog "Edit Configuration", Configure the Tab "Main", The location needs to fill in the path of the Nkd-build.cmd (NDK installation directory), Workingdiretcoty in the project root directory. Such as:
Configuration Tab "Refresh": tick "Refresh resources upon completion", tick "the entire workspace", tick "recuresively include sub-folders", such as:
Configuration tab "Build Options": Tick "after a" and "clean", tick "During manual builds", tick "During Auto builds", tick "specify working set of Relev" Ant Resources ". Such as:
In the, click "Specify Resources ..." tick the "JNI" Directory of the test project, click Finish, then click OK to save. Such as:
Create a new makefile type file under the Jni folder for the project with the file name application. (Note: If you don't know how to create a new makefile-type file, you can copy the Android.mk file under the Jni folder in place and rename it to application.) You can change the contents of the application.mk after opening it).
Such as:
Edit the Application.mk file to add the following two lines based on your emulator processor type and SDK level, where App_abi specifies the processor type and can specify multiple.
App_abi: = armeabi-v7a
App_platform: = android-19
As we tick the "During Auto builds", so when the project has changed, so file will be automatically compiled, the correct generation can be found in the project directory under a Libs folder, such as:
Well, the environment of the Android NDK program is set up and the detailed process of compiling is finished here. I am trying to set breakpoints in the source code of the Android NDK to follow-up debugging, and so I debug the success of the blog to share with you.
http://blog.csdn.net/u013149325/article/details/40375455
Detailed tutorials for ANDROIDNDK development with ADT under Windows (from environment configuration to compilation process)