Build an Android Application Development Environment Using C ++ in Windows

Source: Internet
Author: User

 

Introduction: although the mainstream development languages of Android applications are Java, it is also feasible to use C/C ++ for development, although it is still limited to developing binary dynamic libraries in C/C ++ language. so; so our basic idea is to create a standard Java Android Application and then link the C/C ++ dynamic library into the application. The two work closely together to complete the final task.

The problem is coming soon:

First, the C ++ code must be compiled into a. so dynamic library to be packaged together with the Java code. How does our C ++ code be compiled into a. so dynamic library?

The answer is to use Android NDK for compilation.

How does Java code interact with C/C ++ code? To put it bluntly, how does one call each other?

The answer is that Android uses Java Native Interface (JNI) to communicate with native code in standard Java mode. It defines the norms and mechanisms for interaction between Java code and C/C ++ code.

Token is another problem;

The answer is still Android NDK.

Here, we certainly see that Android NDK is the key to our cross-Development of Java and C ++. You must be sure to know where this Android NDK is sacred and not in a hurry, listen to me;

Android NDK is the abbreviation of Native Development Kit running on the Android platform. Android app developers can call C or C ++ local code through NDK.Ndk provides a series of tools to help developers quickly develop C (or C ++) dynamic libraries, and can automatically package so and Java applications into APK together. These tools are of great help to developers. Ndk integrates the cross compiler and provides MK files to isolate differences such as CPU, platform, and abi. Developers only need to modify mk
File (indicating "which files need to be compiled" and "Compilation feature requirements"), so can be created. 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 compile your code written in C or C ++. so (similar to. DLL) format file, so that you can use Java language (JNI) in your android program to call these code.

(Source code for android development on windows
C ++ environment setup)

 

After solving these problems, we began to build the environment. Ready? GO!

Step 1: Download Eclipse;

For Java IDE, we select Eclipse, so we need to download Eclipse first. I download eclipse-java-indigo-sr1-win32.zip. The website is http://www.eclipse.org/downloads/. After downloads/;is downloaded, we will not install Eclipse;

 

Step 2: Install JDK;

In order to conduct normal Java development, we must install JDK; download and install JDK, URL: (http://www.oracle.com/technetwork/java/javase/downloads/index.html); after the download is complete, you need to set the environment variable settings: Example:

JAVA_HOME = C: \ Program Files \ Java \ jdk1.7.0 _ 01

PATH = % JAVA_HOME % \ bin

CLASSPATH = % JAVA_HOME % \ lib \ dt. jar; % JAVA_HOME % \ lib \ tool. jar; xxxxxx/lib/dt. jar

After the settings are complete, restart your computer;

 

Step 3: Install ADT;

With Eclipse and JDK, there is no problem in developing general Java projects. However, to develop Android applications, we must have the ADT (Android Development Tool) plug-in; after installing this plug-in, we can directly create an Android project in Eclipse. Installation Method: Under Eclipse, use the menu Help-> Install new Software. In the displayed interface, click the ADD button after Work with, enter the adt url (http://dl-ssl.google.com/android/eclipse/) in the pop-up dialog box, click "OK", such:

Remove the check box before "Contact all update sites during install to find required software", such as (Remember, otherwise it will be slow );

Step 4: Install the Android SDK;

Android SDK is indispensable for Android Application Development. Download android-sdk_r15-windows.zip at http://developer.android.com/sdk/index.htmlto decompress the SDK.
Manager.exe downloads API 14, for example:

 

Step 5: Install CDT;

To develop C/C ++ in Eclipse, you must install CDT, that is, the C/C ++ environment of Eclipse.
Http://download.eclipse.org/tools/cdt/releases#install the SDK in step 3. If online installation is slow, you can use offline installation. I am using offline installation. Installation Method: release;

Enable eclipse to verify that the CDT is correctly installed. If the C/C ++ project option is found during project creation, it indicates that the CDT is successfully installed. For example:

In, we can also see the android project option, proving that our ADT has been installed successfully.

 

Step 6: start playing and download ndk;

URL: Workshop;

 

Step 7: Install cygwin;

The most difficult step is to install cygwin slowly. It usually takes two days to complete. make and gcc in Cygwin are required for NDK compilation, So download and install Cygwin first. Android is based on Linux, so if you want to write native code for it, you need some Unix tools. In Windows, NDK supports Cygwin1.7.x or a later version. What is Cygwin? Cygwin is a unix simulation environment running on windows platform. 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.

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://cygwin.petsads.us, 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!

 

Step 8: configure the NDK environment variable;

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.

Note: Use UltraEdit to open. bash_profile, and ensure that there is no space in the last line of the original content of the file;

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.

 

Step 9: 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, 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!

 

Step 10: integrate NDK in Eclipse;

So far, the C ++ compiling environment has been installed and configured. However, the entire compilation process is still complex because it is not integrated with the Java compiling environment, the simplest method is to integrate NDK into Eclipse. Method:

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 <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. Now let's test whether it can be automatically compiled. 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!

 

Step 11: Install the android4.0 simulator;

Use AVD manager.exe In the android SDK folder again, and install it as prompted;

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.