Install and use Android ndk R5 on Windows

Source: Internet
Author: User

WindowsInstall and use android on the System Ndk R5 

I have heard of Android's ndk application for a long time, but I have never had time to study it. Today I spent some time building an ndk environment on the Windows platform and successfully ran the first simple Android Application.

1. What is ndk?

Ndk provides a series of tools to help developers quickly develop C (or C ++) dynamic libraries, and can automatically

The Java application is packaged into an APK. These tools are of great help to developers.

Ndk integrates with the cross compiler and provides MK files to isolate differences such as CPU, platform, and Abi, developers can create so simply by modifying the MK file (indicating "which files need to be compiled" and "Compilation feature requirements. Ndk can automatically package so and Java applications, greatly reducing developers' packaging work. In short, ndk is a set of cross-compilation tools that help you write data in C or C ++.Code, Compiled into. So (similar to. dll in Win) format, so that you canProgramJava language (JNI) is used to call the code.

 

 

Ii. Download and install cygwin

Since make and GCC must be used to compile the ndk code, you must first build a Linux environment. cygwin is a UNIX simulation environment running on Windows, it is very useful for learning Unix/Linux operating environments or porting applications from UNIX to Windows. With this feature, you can use ndk to compile C and C ++ code without installing Linux. Next we will install cygwin step by step.

(I forgot it during installation)

First, you have to go to the http://www.cygwin.com to download setup.exe

1. Double-click "run". After running, you will see the Installation Wizard interface:

2. click Next.

In this case, select the installation method:

1) install from Internet: Download the package directly from the Internet and install it immediately (after the installation is complete, the downloaded package will not be deleted, but will still be retained so that the package can be installed again next time ).

2) download without installing: Download the installation file to your local computer, but it is not installed yet.

3) install from local directory: without downloading the installation file, install it directly from a local directory containing the installation file.

3. Select the first item and click Next:

4. Select the directory to be installed. Note that it is best not to place it in a directory with Chinese characters and spaces, which may cause installation problems. Other options do not need to be changed. Next, click:

Step 5: select the cygwininstallation directory. This is the directory of the installation package you downloaded. Click Next To Run setup.exe:

6. You have three Connection Methods:

1) Direct Connection: a direct connection.

2) use ie5 settings: Use the connection parameter settings of IE to connect.

3) Use http/FTP Proxy: Use http or FTP Proxy Server for connection (you need to enter the server address and port number ).

You can select based on your network connection. Generally, you can select the first method, that is, direct connection. Then click "Next ",

7, this is the choice to download the site, I use a http://mirrors.kernel.org, the speed is quite fast, after the selection of the next step

8. The installation package list will be downloaded.

9. Search allows you to enter the name of the package you want to download and quickly filter out the package you want to download. The four single-choice buttons select the style of the bottom tree. By default, they do not need to be moved. The default value of view is category. We recommend that you change it to "full" to show all packages and check them again. Save some of the packages to be hidden. The check box in the lower-left corner shows whether to hide the expired package. By default, you just need to check it. Download the package we want to install below to avoid downloading it all, the following packages used for ndk development are listed: autoconf2.1, automake1.10, binutils, GCC-core, GCC-G ++, gcc4-core, gcc4-g ++, GDB, PCRE, PCRE-devel, gawk, make a total of 12 packages

10. Choose to install these packages, click skip, and convert it to the digital version format. Make sure that the bin item is converted to the cross sign, and the SRC item is the source code. This is unnecessary.

11. Test whether cygwin has been installed.

Run cygwin. In the displayed command line window, enter the cygcheck-C cygwin command to print the current cygwin version and running status. If the status is OK, cygwin runs normally.

Then input gcc-version, G ++ -- version, make-version, and GDB-version in sequence for testing. If both print the published version information and some description information, I am very happy to tell you, your cygwin installation is complete!

3. Configure ndk Environment Variables

1. First find the installation directory of cygwin and find a home \ <your username> \. bash_profile file. Mine is: e: \ cygwin \ home \ Administrator \. bash_profile, (note: I didn't have any Trojans in my home folder during installation. Solution: First open the environment variable and delete the home variable from the user variable. In E: in the \ cygwin \ home folder, create a folder named "Administrator" (user name), and then set E: \ cygwin \ etc \ skel \. bash_profile ).

2. Open the bash_profile file and add ndk =/cygdrive/<your drive letter>/<Android ndk directory> For example: ndk =/cygdrive/e/android-ndk-r5

Export ndk

The ndk name is random. For future convenience, select a short name and save it.

3. Open cygwin and enter CD $ ndk. If the/cygdrive/e/android-ndk-r5 information configured above is output, the environment variable is set successfully.

4. Use ndk to compile the program

1. Now let's compile a simple program with the installed ndk. Let's choose the built-in ndk example hello-JNI, my location is E: \ android-ndk-r5 \ samples \ hello-JNI (depending on your location ),

2. Run cygwin and enter the command CD/cygdrive/e/android-ndk-r5/samples/Hello-JNI to enter the E: \ android-ndk-r5 \ samples \ hello-JNI directory.

3. Enter $ ndk/ndk-build. After successful execution, it will automatically generate a libs directory and put the compiled. So file in it. ($ Ndk is used to call the environment variable we configured previously, and ndk-build is used to call the Compilation Program of ndk)

4. Go to the libs directory of hello-JNI to check whether the. So file is generated. If yes, your ndk will run normally!

5. Integrate the C/C ++ development environment in eclipse

1. Install the C/C ++ environment plug-in of Eclipse: CDT. Here, install it online.

First, log on to http://www.eclipse.org/cdt/downloads.php and find the on-line installation address of your own cdtplug.

2. Click the Help menu and find the install new software menu.

3. Click the Add button and enter the obtained address. After the plug-in list is displayed, select all. Then, select next to complete the installation.

4. After the installation is complete, right-click a new project in eclispe. If the C/C ++ project appears, your CDT plug-in is successfully installed!

6. Configure the C/C ++ Compiler

1. Open eclipse, import the hello-JNI example that comes with ndk, right-click the project name, and click Properties to bring up the configuration interface, and then click builders to bring up the compilation tool list of the project, click new to add a new compiler. The add page is displayed. Select Program and click OK.

2. The add interface appears. First, name the compilation configuration, for example, c_builder.

Set location to <your cygwin installation path> \ bin \ bash.exe program, for example, E: \ cygwin \ bin \ bash.exe, and set working directory to the <your cygwin installation path> \ bin directory, example: e: \ cygwin \ bin

Set arguments

-- Login-c "CD/cygdrive/e/android-ndk-r5/samples/Hello-JNI & $ ndk/ndk-build"

In the above configuration,/cygdrive/e/android-ndk-r5/samples/Hello-JNI is the directory of the program you are currently compiling, $ ndk is the environment variable of the previously configured ndk, these two parameters are configured based on your specific installation directory. Other parameters do not need to be changed. The parameters of arguments are actually passed to the bash.exe command line program to enter the program directory to be compiled, then run the ndk-build Compilation Program

3. Switch to the refresh tab and hook refresh resources upon completion

4. Switch to the build options tab and check the last three items.

5. Click specify resources, select the Resource Directory, and select your project directory.

6. Click "finish" and click "OK" to save the configuration. Note: If the compiler you configured is under another compiler, remember to set it to the first place by clicking the "up" button, otherwise, the compilation of C code is later than that of Java code, which may cause your C code to be compiled twice before you can see the latest modification.

7. You must be tired after this process. Congratulations again, the compilation configuration is complete. Now, let's test whether automatic compilation can be performed, open the hello-jni.c file in the project JNI directory and prompt hello from JNI! Change it to another text, such as hello, my name is Alex., and then run your program in the simulator. If the simulator displays your latest modified text, then congratulations! You have all configured successfully!

VII. Summary

the above process is the whole process of my ndk configuration and succeeded in running on my machine. I have written some good materials on www.eoeandroid.com, thank you for your understanding and memory.

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.