Android NDK Development (vii)--Modern development mode

Source: Internet
Author: User

Some of the previous blogs in this column have written some basic knowledge of the NDK development through JNI under Android, and the main steps are clearly written in it, but it looks more troublesome. Perhaps careful friends will find me every time when the cross-compilation, is through the Cygwin simulation Linux environment with the Ndk-build command to compile, and each new JNI project, will be in the project directory manually create a new JNI directory, In this directory to create new C source files and android.mk configuration files, this series of operations is particularly cumbersome, especially Cygwin, first of all its download installation is a very time-consuming operation, followed by the use of Cygwin also need to understand some Linux commands.


1,ndk-build.cmd Environment Configuration

In addition to these cumbersome, there are other "inconvenient" places, the following will say, the first point is that I learned the NDK development, is based on the NDK_R7 version, that will develop the NDK may have to complete cross-compilation in the Linux environment, but after the NDK_R8 version , Google also realizes that many Android programmers are developing in the Windows environment, so after the R8 version, the NDK development kit starts with some Windows-based configuration files and DOS command-line batch files. Here is the latest version of ANDROID-NDK-R10D's Extract directory section:


The Ndk-build.cmd in the Red box is the batch command of the NDK developed by the Windows Developer on the DOS command line after the R8 version, and with this, we don't have to install annoying Cygwin when the NDK is developed, and we don't have to cross-compile under Cygwin. , instead, you can compile the. So file directly from the Windows command line, using Ndk_build.cmd to configure the Windows environment variables, configure the Ndk_build.cmd in the path variable, which is similar to configuring the JDK, and the steps are no longer detailed.

Right-click on "Computer" on the desktop--Properties--Advanced system settings--Environment variables--Select path to edit--copy and paste the NDK decompression path after the variable value


Open the Windows command line, type Ndk-build, enter, if it appears as shown in the environment configuration, you can use the Ndk-build command under Windows to develop.



2,eclipse Environment Configuration

After we've finished configuring Windows, we'd better configure the NDK path under Eclipse, so that when we compile the. So file, we don't even need the Windows command line to compile directly under Eclipse.

Specify the directory where the NDK is located in Eclipse---------------only once is required



3, using Android Tools->add Native support

To do this, let's start with a new Android project and automatically add JNI support for this Android project.

On the project right--and Android Tools--- ADD NativeSupport, enter the edited C + + file name in the popup dialog box, and then you can see that the project automatically creates a JNI and obj directory, and a. cpp C + + file is automatically generated under the JNI directory, we need to change to. c files, and a android.mk is automatically generated, and we also need to change the C file to Local_src_files. C.



4, Associated source

In the previous several blog, as long as the source of the C file in Eclipse, you will find a lot of small yellow lines, make people more annoyed, and C file class library source code is not related, we press the CTRL key with the mouse click on the source code when there is no source, then after the release of the high version of the NDK, These actions are just a simple setup under Eclipse.

Right click on the project-------------Paths and symbols to the right of the Add button--click File System--Select Android -ndk-r9b\platforms\android-19\arch-arm\usr\include, until the Include directory--OK


Open C Source view, find annoying yellow line is not, and then in the method or the head file Ctrl + Right click, the source code also bounced out, more convenient.


5, Write Java native method, write C to realize the source code

To do this is to achieve the functions we need, the specific steps are similar to the previous introduction, here is no longer detailed, interested in understanding, please check our previous blog Android NDK development-from Hello World, this code is the same as the code of the previous blog is identical, No need to re-write it again. Note that, after the above configuration, this time to write C code when the code hints, haha, is not suddenly become more advanced ah?



6, compiling the dynamic link library

Through the above steps, our code has been written, this time it is necessary to compile the code compiled into. So files, how to do? Recall the previous practice, open Cygwin, enter the Linux command to switch to the project directory, input ndk-build to compile? or open the cmd Command window, switch to the project directory, output ndk-build? None of the above, why? Because we have just set up the NDK build environment in the properties, we can do cross-compilation work directly on Eclipse.

In the upper-right corner of Eclipse, find open perspective----Select C + + view and click the Small Hammer button in the upper right corner of the view to cross-compile under Eclipse .



After compilation



Found under the engineering


Well, this step means that we have successfully completed the cross-compilation of the native code, and the rest is to write Java code to call it.


How about, the above NDK development step is not more than the manual development steps introduced in the previous several blog is more simple AH?! and also omitted the download and installation of Cygwin and use, the last code in the small yellow Line did not, write C code also has code hints, can also be associated with the source, it is simply convenient to the pole! Then we in the development of the JNI program should be taken in such a simple way, this blog introduction to this end, hope to read the blog friends to propose amendments, thank you!


Android NDK Development (vii)--Modern development mode

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.