C + + developing Android NDK development environment in Android and Windows

Source: Internet
Author: User

1. NDK (Native development Kit)

1.1 NDK Introduction

The Android NDK is a set of tools that allow developers to develop their Android apps using native code, such as C + +, and through this tool, we can compile native code that can be run directly on the Android platform, These native codes exist in the form of a dynamic-link library (*.so), and because of this, we can reuse the local code by reusing these dynamic link libraries.

So, with the NDK development Kit, can we write an apk completely in C + +?

The answer is no. As is known to all, Android applications are running on Dalvik virtual machines. While the NDK has developed only a subset of the interfaces, the NDK just allows developers to implement some of the features of the Android app via C + +. Since the Android system itself is the Linux core, the NDK is only responsible for writing these native code into a dynamic link library that can run on Linux, bypassing the Dalvik virtual machine and running directly on the Android platform.

1.2 NDK Download

1.2.1 Download Instructions

: http://www.cr173.com/soft/66623.html
Http://developer.android.com/tools/sdk/ndk/index.html#Downloads

As of 2013-12-9, when I wrote this blog, the latest version is: R9

In the same platform, the download package has two, we choose to download android-ndk-r9b-windows-x86

1.2.2 Download Item Differences

The NDK supports three platforms (Windows, MAC, Linux), and here I take windows as an example to explain the difference between the package in the download,

, we can see that each platform has two corresponding download packages, namely Android-ndk-r9b-windows-x86.zip and Android-ndk-r9b-windows-x86-legacy-toolchains.zip, So what's the difference between these two packages?

The official website explains this: the first download package, the Android-ndk-r9b-windows-x86.zip in the diagram, contains the default toolchain for all platforms, and if you do not want to use the current default recommended toolchain, you can download the second one. It might be a little short to say so, let's take a look at the following picture:

We know that the legacy package has only one toolchain package, and not the legacy package contains a variety of default already-developed tools, including examples, documentation, and so on.

1.3 NDK Installation Instructions

1) Directly unzip the downloaded installation package to the path you want to put;

2) Configure Environment variables

I. Create a new system environment variable named Android_ndk_root, and the value of the variable is the path where your NDK is located, such as mine is D:\DevTools\JAVA\adt-bundle-windows\android-ndk-r9b

II. Add%android_ndk_root%\ at the front of the system variable Path;

 

1.4 NDK Installation Verification

After completing the above steps, the NDK installation is complete, so let's verify that the NDK installation is successful or not.

1) Open Cmd.exe, enter ndk-build-version inside, output the following content, indicating the installation is successful.

Instructions for using the 1.5 NDK

According to the official documentation, from the R7 version, there is an extra ndk-build.cmd in the NDK, allowing us to run directly under Windows, without the need to spend a lot of time downloading and installing Cygwin ...

Let's use the official project Hello-jni as an example to explain it.

1) enter into the Hello-jni directory or any of its parent directory, I will now go directly into the HELLO-JNI directory;

2) Direct input command ndk-build for local code compilation, output the following content, indicating the successful compilation into a dynamic link library, and we can see the compiled libhello-jni.so under the Hello-jni\obj\local\armeabi path;

 

Once the compilation is successful, let's try to see if the dynamic link library that was successfully compiled is available.

1) Perform the ADB Update Project command we explained in the previous blog post to upgrade the project, add files such as Build.xml to the project, and you can view the previous blog post "Android learning the next day-android common Commands" Below I execute the following command directly on the machine:

Android Update project-p. -T 1

Output the following results indicate that the update was successful.

2) directly execute the command ant debug to compile package into apk (here need to pre-build ant environment, if not set up, can refer to blog: http://my.oschina.net/xiahuawuyu/blog/71296)

3) Open Simulator

4) Execute command directly install the APK in the bin directory into the simulator in adb install hellojni-debug.apk the following are the results of the execution:

 

2. Using the NDK in eclipse

2.1 Eclipse CDT (development tooling)

2.1.1 CDT Introduction

CDT is a development tool for C/s + + under the Eclipse platform. It provides features such as: A/C + + editor (some basic features: syntax highlighting, code editing, etc.), A/C + + debugger, A/C + + Launcher, a parser, a content hint, a makefile generator, and more.

2.1.2 CDT Download and install

2.1.2.1 CDT Pre-installation instructions

If you download the ADT binding version provided by Google, then it has been installed by default the corresponding CDT, you can ignore the chapter does not read, we can also through help-> about ADT to view the relevant information,

2.1.2.2 CDT Download

: http://www.eclipse.org/cdt/downloads.php

This address is downloaded from an offline installation package, which is also recommended because of the large installation package.

Note: Different versions of eclipse should download the corresponding packages or they will not be installed

So how do we know what our eclipse version is?

Also, via help-about Eclipse, open the relevant information,

, we can know our version of Eclipse, such as my is Indigo, and then on to the download according to the version of the corresponding, such as I downloaded is cdt-master-8.0.2.zip

2.1.2.3 CDT Installation

1) Open a dialog box by using the Install New software Help, click the button Add.

2) in the Pop-up dialog box, the recommended value for name is: Cdt_ version number, then click the Archive button to navigate to the CDT Offline installation package we downloaded to

3) After the installation is complete, restart eclipse will take effect

4) After the installation is complete reboot, create a new project in Eclispe, if a C + + project appears, the CDT plug-in installation is successful

2.1.2.4 CDT Installation Problem Resolution

1) If the "Duplicate location" error appears below the location, go to avaliable software, install/update, window, preferences Remove removed from the site.

2) If a missing eclipse package indicates that the version you downloaded and the Eclipse version do not match, re-download the matching reinstall.

2.2 Sequoyah

2.2.1 Sequpyah Introduction

The Sequoyah plugin is used to set up Android engineering support for native development, and if you are installing an ADT-bound version of your classmate, you can read the section 2.2.4 NDK configuration directly

2.2.2 Sequoyah Download

: Http://www.eclipse.org/sequoyah/downloads/index.php#latest

We can download the offline installation package from the above address, the specific installation method is the same as the CDT offline installation method above, again do not repeat the instructions.

2.2.3 Sequoyah Online Installation

As the Sequoyah itself is not too large, the online installation speed is relatively fast, so it is recommended to install online, let us explain the online installation method

1) Open a dialog box with install New software via Help, click the button Add, no, just like the first step in the CDT installation.

2) in the popup window, Name input sequoyah,location Direct input http://download.eclipse.org/sequoyah/updates/2.0/,

Note: Do not tick the "Group Items by category" checkbox on the installation screen, which is checked by default and there is a case where the list is empty (there is no categorized items).

Configuration of the 2.2.4 NDK

We explain this in the example given in the NDK HELLO-GL2:

1) Click the Browser button to navigate to the root directory of our NDK installation via the preference, Android--, NDK

2.3 Eclipse in the NDK usage notes

1) First we should import the project, right-click, select Import

2) Since the example is not an Eclipse project, we must choose to let Eclipse build the project based on the existing Android code and click Next

3) Navigate to the item we want to import and copy it to the workspace, let the example take care of the code to keep it as it was, and we're working on the code that was copied into the work area.

4) Right-click on the project we have introduced, select Android Tools, ADD Native support

5) because the demo has already written for us the CPP file, so directly with his name, click OK.

6), click on the button in the upper right corner to enter perspective/C + +

If you can't find the Change button, we can also open it via Windows---open perspective.

7) Click the Hammer button in the toolbar to compile

8) Wait for the console to output the following, indicating that the dynamic link library has been compiled, and we can also see the compiled dynamic link library in Obj\local\armeabi.

9) Note: Change the project want to directly run the words will be wrong, the specific solution can be consulted: http://hi.baidu.com/huareal/item/f00ad3cf8d35ebd7ee183b83, according to the article modified, we found that we can work properly , which also shows that our NDK-related settings in Eclipse have been configured successfully

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.