Build an ndk development environment in Windows
1. What is ndk?
Ndk
Provides a series of tools to help developers quickly develop C (or C ++) dynamic libraries, and can automatically
Package it into an APK together with a Java application. These tools are of great help to developers.
Ndk
Integrates with the cross compiler and provides MK files to isolate CPU, platform, and abi
Developers only need to modify the MK file (indicating "which files need to be compiled" and "Compilation feature requirements") to create so files. Ndk
So and Java
Application packaging greatly reduces 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 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 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.
Cygwin: http://cygwin.com/setup.execlick cygwin.exefile
The following page is displayed:
Click Next
If you have not downloaded the package, select the second one to download the installation file, or use the first online installation, but it is very slow.
Because everything is downloaded in advance, the third item is selected.
Click Next
Find the downloaded Installation File directory and click Next.
Change the two marked in the red box to the install status, and then click Next.
Finally, the installation is successful.
There is a shortcut on the desktop
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.
Input gcc-V, G ++ -- version, make-version, and GDB in sequence.
-Version is used for testing. (note the space and-) if both print the current information and some description information, we are very happy to tell you that your cygwin installation is complete!
3. Download and install androidndk
Google
The latest output ndk is
Http://developer.android.com/sdk/ndk/index.html
Iv. 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> example: ndk =/cygdrive/e/android-ndk-r6b
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-r6b information configured above is output, the environment variable is set successfully.
5. Compile the demo project
Now you can compile the relevant ndk Project (some programs written in C/C ++). The compilation method is as follows:
1. Run the cygwin command line to go To the android project working directory:
CD $ ndk
2. Go to the samples \ hello-JNI directory in sequence (full path example: e: \ andriod \ develop \ android-ndk-r4 \ samples \ hello-JNI)
3. Run
$ Ndk/ndk-build
Note: This command $ ndk and/ndk-build are connected together with no spaces in the middle. In actual use, the complete $ ndk/ndk-build is entered, do not create ndk-build only.
4. After compilation is complete, the corresponding. So file will be generated in the libs/armeabi directory of your project.
6. Import the static target Library to the android Project
After compilation is complete, you will get *. after the so static target library is used, it can be imported into the eclipse Android project of the engineering environment. Note that ndk programming does not necessarily require importing this target library into the android project, in addition to the usage described in this step, the android source code is also directly modified, compiled, and then burned into the development mode of the test machine, in this way, the default installation of the application, permission boot upgrade, and other "thorough" functions can be implemented. Overall:
This step demonstrates the ndk target library + andriodapk style. In addition, there are Android source code direct development and direct compilation methods (to be exact, the so-called Android engineering concept has been deprecated, of course, source code development does not seem to be available in windows at present. It seems that it must be carried out in the Linux family system, which will be covered in a later article)
The usage is as follows (example ):
1. Create a new project hellojni and hellojni in eclipse. for the code in Java, refer to the Demo code (android-ndk-r4 \ samples \ hello-JNI \ tests \ SRC \ com \ example \ hellojni) under the sample corresponding to ndk to call the method.
2. Copy the JNI and libs folders under the ndk compilation project directory to the new project directory.
Note that the two folders must be in the same directory as the SRC and res files in the project. Then go to eclipse to refresh the project (two more folders will be displayed ).
3. Run the Eclipse project, you can see the output string in the hello-jni.c function on the virtual machine.
4. Later, you can try to modify the Library source program or project Java program to familiarize yourself with the entire ndk development process.