Android applications are developed in Java. Android ndk enables us to use the native code developed by C/C ++ on Android.
There are two reasons to use ndk: one is to rationally reuse existing code, and the other is to improve the execution efficiency in some key parts of the program.
For Android ndk development, in addition to the general Android development environment, you also need to download the ndk package and install the cygwin tool (required in Windows ).
Cygwin
Cygwin is a Windows environment similar to Linux. It includes a DLL that provides a basic subset of UNIX functionality and a set of tools above it. (This is not required in Linux)
To install cygwin, follow these steps:
1.download setup.exe
Go to www.cygwin.com and click "Install" on the left.
Cygwin ". Click "“setup.exe" in the middle to download a small program setup.exe (250kb ).
This installer will guide the cygwin environment installation or update process on your machine.
2.run setup.exe to installInstallation, such:
Figure 1. Installation Wizard of cygwin Environment
ClickNextButton.
Select Download location
On this page, select "Install from Internet ".
Figure 2. Select the installation type
Keep clickingNextButton until the page appears:
Select a download site. Because these sites are obligated images, it is risky to select a download point (usually they are available but sometimes unavailable ). Generally, try the site that is closer to you. (There is one http://mirrors.163.com fastest)
ClickNextButton.
Select a package for Installation
On this page, you will select the package to install. By default, GCC is not installed in the cygwin basic package. Therefore, you must modify the default settings.
Move the mouse overDevelOn the plus sign (+) of the edge, click it to expandDevelClass.
In this step, we select the component package to be downloaded. To enable cygwin to compile the program, we need to install the GCC compiler. By default, GCC will not be installed, we need to select it for installation. To install GCC, click the "devel" branch in the component list. There are many components under this branch. What we need is:
Binutils
Gcc
Gcc-mingw
GDB
Make
Click the cycle button in front of the component to display the build version date. Select the latest version for installation. The four types of components are selected:
Binutils component
GCC Components
Gcc-mingw component
GDB Components
Make component
After the selection, select next to go to the installation process, as shown in,
After the download is complete, select create a shortcut on the desktop!
3. test whether the installation is successful.
Run cygwin and enter make-V and GCC-V. If you can find them, the installation is successful.
Install ndk
It is easy to install ndk. You just need to search for the downloaded ndk file on the Internet and decompress it to the specified directory. (For example, my path is E: \ android-ndk-r6b)
Configure ndk
Modify the. bash_profile file under/home/administrator under the cygwin directory (which is installed in c: \ cygwin by default) and add the following code at the end of the file:
Ndk_root =/cygdrive/ndk file path export ndk_root
For example, mine is as follows:
NDK_ROOT=/cygdrive/e/android-ndk-r6bexport NDK_ROOT
(Note: The ndkroot name can be used by yourself. The path of the ndk file is the path of the ndk after decompression. Note that you should change the Slash to a backslash and remove the path difference between Windows and Linux ))
5. Use ndk to compile the program
A. now let's use the installed ndk to compile a simple program. Let's choose the built-in ndk example hello-JNI, my location is E: \ android-ndk-r6b \ samples \ hello-JNI (depending on your location ),
B. Run cygwin, enter the command CD/cygdrive/e/android-ndk-r6b/samples/Hello-JNI, enter to E: \ android-ndk-r6b \
Samples \ hello-JNI directory.
C. Enter $ ndk_root/ndk-build (Be sure to run $ ndk_root/ndk-build in the directory of the program you want to compile, because yes
Use ndk to compile the project you need. during compilation, you will find the Android. mk file in the JNI directory of your project to determine the file to be compiled.) After successful execution,
It automatically generates a libs directory and stores the compiled. So file in it. ($ Ndk_root is used to call the environment changes we configured earlier.
(Ndk-build is the Compilation Program that calls ndk)
D. Check whether the. So file is generated in the libs directory of hello-JNI. If yes, your ndk will run normally!
6. Develop an ndk program by yourself. Example:
A. Create an android project named ndktest in eclipse and create a JNI. Java file:
Package com. ZY. ndktest;
Public class JNI {
Public native int getint ();
Public native string getstring ();
}
In addition to the native method, the JNI file can also contain Java writing methods.
B. Generate a. h file using javah:
First, run the android program to compile and generate a class file.
Open the command line window and enter the bin \ Classes directory of the project.
Run javah-classpath.-JNI com. ZY. ndktest. JNI
A com_zy_ndktest_jni.h file is automatically generated in the classes directory.
C. Use the C language to implement local methods
First, create a new JNI directory in the project directory, and then create the org _ philn_hellondk_jni.c file (implement two of org _ philn_hellondk_jni.h
Method)
# Include <stdio. h>
# Include <stdlib. h>
# Include <JNI. h>
Int Add2 (){
Int X, Y;
X = 1000;
Y = 8989;
X + = y;
Return X;
}
Jniexport jint jnicall java_com_zy_ndktest_jni_g etint
(Jnienv * ENV, jobject thiz ){
Return Add2 ();
}
Jniexport jstring jnicall java_com_zy_ndktest_jni_g etstring
(Jnienv * ENV, jobject thiz ){
Return (* env)-> newstring ut F (ENV, "hellondk ---- philn ");
}
D. Create an android. mk file under JNI and write the file:
Local_path: = $ (call my-DIR)
Include $ (clear_vars)
Local_module: = ndktest
Local_src_files: = com_zy_ndktest_jni.c
Include $ (build_shared_library)
E. Use cygwin to enter the project folder:
$ CD/cygdrive/D/Android/workspace/ndktest (My ndktest project folder is: D/Android/workspace/ndktest)
Run the following command:
$ Ndk_root/ndk-build.cmd
The libs \ armeabi \ libndktest. So file is automatically generated under this project.
F. mainactivity calls:
Public class mainactivity extends activity {
Static {
System. loadlibrary ("ndktest ");
}
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
JNI = new JNI ();
Textview = new textview (this );
Textview. settext (JNI. getstring () + ":" + JNI. getint ());
Setcontentview (textview );
}
}
6. Run the application and see the following results:
Hellondk ---- philn: 9989
Category: Android ndk