Android NDK Development (7)-modern development method, androidndk

Source: Internet
Author: User

Android NDK Development (7)-modern development method, androidndk

In the previous blogs of this column, I wrote some basic knowledge about NDK development through JNI in Android. The main steps are also clearly written, but it looks troublesome. Why? I may find that every time I perform cross-compilation, cygwin is used to simulate the Linux environment and use the ndk-build command for compilation. Every time I create a JNI project, A jni directory is created manually under the project directory, where C source code files and Android are created. mk configuration file, this series of operations is particularly cumbersome, especially cygwin, first of all, its download and installation is a very time-consuming and labor-consuming operation, and the use of cygwin also needs to understand some Linux commands.


1, ndk-build.cmd environment Configuration

In addition to these cumbersome features, there are other "inconveniences". The first thing I will note is that I used the ndk_r7 version when I was studying NDK development, in that case, the development of NDK may have to be completed in the Linux environment, but after the ndk_r8 version, Google also realized that many Android programmers are developing in the Windows environment, so after the r8 version, ndk Development Kit began to provide some Windows-based development of configuration files and the doscommand line of the batch files, the following is the latest version of the android-ndk-r10d decompression directory section:


The ndk-build.cmd in the red box circle is some of the NDK batch processing commands developed under the doscommand line provided by Windows developers after r8 version, with this, during NDK development, we do not need to install annoying cygwin, nor do we need to perform cross-compilation under cygwin. Instead, we can compile it directly under the Windows command line. so file. To use ndk_build.cmd, You need to configure the windows environment variable and configure ndk_build.cmd in the Path variable. This process is similar to configuring JDK. The specific steps are not described in detail.

Right-click "computer" on the desktop --> properties --> advanced system settings --> environment variables --> select Path and click Edit --> copy and paste the ndk decompression Path to the variable value


Open the Windows command line, enter ndk-build, and press Enter. if the environment is configured as shown in, you can use the ndk-build command for development in Windows.



2. Eclipse environment Configuration

After completing the Windows configuration, we 'd better configure the NDK path under Eclipse, so that we can compile. so files do not even use the Windows command line to compile them directly in Eclipse.

In the eclipse --> window --> perference --> Android --> NDK tag, specify the directory where the NDK is located (only one execution is required)



3. Use Android Tools-> Add Native Support

To achieve this, we first create an Android project and automatically add JNI support for this Android project.

InRight-click the project --> Android Tools --> Add Native SupportIn the displayed dialog box, enter the edited C/C ++ file name. After confirming, you can see that a jni and obj directory is automatically created under the project, in addition, a jni directory is automatically generated. we need to change the C ++ file of cpp. c file, and an Android. mk is automatically generated. We also need to change LOCAL_SRC_FILES. c file.



4. Associate source code

In the previous blogs, as long as you open the source code of the C file in Eclipse, you will find a lot of yellow lines, which are annoying, in addition, the Class Library source code of the C file is not associated. When we press Ctrl and click the source code with the mouse, the source code does not appear. After the release of the High-version ndk, these operations are just easy to set in Eclipse.

InRight-click the project --> properties --> C/C ++ General --> Paths and Symbols click the add button on the right --> click File System --> select android-ndk-r9b \ platforms \ android-19 \ arch -arm \ usr \ include, until the include directory --> OK


Open the C source code and check out the annoying yellow line. Then, press Ctrl + Right-click the method or header file, and the source code is displayed, which is much more convenient.


5. Compile the Native method of Java and the C implementation source code.

To do this, we need to implement the functions we need. The specific steps are similar to those described earlier. We will not detail them here. If you are interested in understanding them, please refer to the Android NDk development of our previous blog-from Hello World, the code written this time is exactly the same as the code of the previous blog, and you don't have to write it again. It should be noted that, after the above configuration, the code prompt is displayed when C code is compiled this time. Haha, is it too advanced?



6. Compile the dynamic link library

Through the above steps, our code has been written. At this time, we need to cross-compile the compiled code into a. so file. What should we do? In retrospect, open cygwin, enter the Linux Command to switch to the project directory, and enter ndk-build to compile? Open the cmd command window, switch to the project directory, and output ndk-build? None of the above calls are needed. Why? Because we have just set up the NDK compiling environment in properties, this time we can directly complete cross-compilation on Eclipse.

InIn the upper-right corner of Eclipse, find Open Perspective --> select C/C ++ View --> click the "Hammer" button in the upper-right corner of the view to perform cross-compilation under Eclipse..



After compilation



Found under the project


Now, this step indicates that we have successfully completed the Native code cross-compilation, and the rest is to compile the Java code for calling.


How are the above NDK development steps much simpler than the manual development steps described in the previous blogs ?! The download, installation, and use of cygwin are also omitted. At last, the small yellow line in the Code is absent, and the code prompt is also provided for compiling C code, which can also be associated with the source code, which is very convenient! In the future, we should adopt this simple method when developing the JNI program. This blog has been introduced so far. I hope that my friends who have read this blog will provide comments for correction. Thank you!


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.