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.
Here we will talk about several symbol conventions:
<Project>-directory of your Android Application Project
<Ndk>-directory of your ndk Installation
Shortcuts
Here is an aside -- if you do not need to use ndk development, but you only need to use a third-party library developed with ndk, you only need to do this:
Put the libxxx. so provided by the third party under your <project>/libs/armeabi/, and add it to any place of the program.
Static {
System. loadlibray ("XXX ");
}
You can use this library.
Install ndk
The installation of ndk is simple:
1. First, upgrade the SDK to the latest version, and then download the ndk (which may have to go over the wall, enun) to unzip it to a directory <ndk>.
2. Run: <ndk>/build/host-setup.sh
(Note: you do not need to perform this step for version R4 or later. For details, see ndk/doc/install.html)
3. If it succeeds, it will be OK. If it fails, check whether you have downloaded the correct ndk version (for example, if your operating system is Linux and Windows ndk is downloaded ).
Appendix:
Android SDK: 3.0
Windows
Http://dl.google.com/android/installer_r10-windows.exe
Http://dl.google.com/android/android-sdk_r10-windows.zip
Mac OS X (Intel)
Http://dl.google.com/android/android-sdk_r10-mac_x86.zip
Linux (i386)
Http://dl.google.com/android/android-sdk_r10-linux_x86.tgz
Android ndk r5b
Windows
Http://dl.google.com/android/ndk/android-ndk-r5b-windows.zip
Mac OS X (Intel)
Http://dl.google.com/android/ndk/android-ndk-r5b-darwin-x86.tar.bz2
Linux
Http://dl.google.com/android/ndk/android-ndk-r5b-linux-x86.tar.bz2
Android SDK: 2.3
Windows
Http://dl.google.com/android/android-sdk_r08-windows.zip
Http://dl.google.com/android/installer_r08-windows.exe
Linux (i386)
Http://dl.google.com/android/android-sdk_r08-linux_86.tgz
Mac OS X (Intel)
Http://dl.google.com/android/android-sdk_r08-mac_86.zip
Android ndk R5
Windows
Http://dl.google.com/android/ndk/android-ndk-r5-windows.zip
Mac OS X (Intel) R5
Http://dl.google.com/android/ndk/android-ndk-r5-darwin-x86.tar.bz2
Linux 32/64-bit (x86) R5
Http://dl.google.com/android/ndk/android-ndk-r5-linux-x86.tar.bz2
Java
Now we use a simple example to illustrate the use of ndk. Create an android project in eclipse, where:
Project name: jnitest
Build target: Android 1.6
Application name: JNI Test
Package name: org. eshock. jnitest
Create activity: jnitest
Jnitest. Java:
Package org. eshock. jnitest;
Import Android. App. activity;
Import Android. OS. Bundle;
Public class jnitest extends activity {
Public native int plus (int x, int y );
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Int x = plus (1, 2 );
Android. util. log. D ("JNI", String. valueof (x ));
}
Static {
System. loadlibrary ("mylib ");
}
}
We only demonstrate ndk, so we don't need the interface. In this program, we call a plus (INT, INT) function written in C language to calculate the value of 1 + 2 and print it in log. The Library name is mylib.
To use a C-language function, you must declare in Java:
Public native int plus (int x, int y );
In this way, the Java compiler will know that this function is implemented in the external library.
Part C
Next we will use the C language to implement this plus function.
Create <project>/JNI/mylib. C:
# Include <string. h>
# Include <JNI. h>
Jniexport jint jnicall
Java_org_eshock_jnitest_jnitest_plus (jnienv * ENV,
Jobject thiz,
Jint X,
Jint y)
{
Return X + Y;
}
This
We can see that the plus function in mylib. C is much more complicated than the signature of the plus function in Java. The main reason is that the package name is added before the plus function. The function has two more parameters, and
All the int values are converted into jint values. For the ing between types in C and types in JNI, see JNI documents, such as core Java
The last chapter in section II. If you do not want to view the document, you can use the following method:
First, create a file jnitest. Java in a directory:
Public class jnitest {
Public native int plus (int x, int y );
}
Then execute the following command line:
Javac jnitest. Java & javah jnitest
A file jnitest. H will be generated:
/* Do not edit this file-it is machine generated */
# Include <JNI. h>
/* Header for class jnitest */
# Ifndef _ included_jnitest
# DEFINE _ included_jnitest
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: jnitest
* Method: plus
* Signature: (ii) I
*/
Jniexport jint jnicall java_jnitest_plus
(Jnienv *, jobject, jint, jint );
# Ifdef _ cplusplus
}
# Endif
# Endif
The signature corresponding to the native function in Java is automatically generated here. You can use it in C code. Xiao Jin: Remember to change the package name
If java_org_eshock_jnitest_jnitest_plus is inconsistent with the package name in Java code, compilation will not be wrong,
Java. Lang. unsatisfiedlinkerror
Android. mk and application. mk
Android. mk is a makefile used to tell ndk which files need to be compiled and which modules are generated. We create the <JNI>/Android. mk file:
Local_path: = $ (call my-DIR)
Include $ (clear_vars)
Local_module: = mylib
Local_src_files: = mylib. c
Include $ (build_shared_library)
Local_path indicates the location of the C source code file, local_module indicates the name of the generated shared library, and local_src_files indicates the file of the C code. You do not need to include the header file. The dependency of the header file is automatically calculated by ndk.
Application. mk is used to tell Android SDK which library files are needed. With it, ndk can place the database in the correct position. We create <project>/application. mk:
App_project_path: = $ (call my-DIR)
App_modules: = mylib
App_project_path indicates the directory where the android project is located. In this example, it is placed in <project>. app_modules indicates the library required by the project. If multiple libraries are separated by spaces.
Compile the Shared Library
Because ndk requires that application. mk be placed in <ndk>/apps/project_name, we create a symbolic connection under <ndk>:
Ln-S <project> <ndk>/apps/jnitest
In this way, we can start to compile the Shared Library:
Run the following command in the <ndk> directory:
Make APP = jnitest
Android ndk: Building for application 'jnitest'
Compile thumb: mylib <= apps/jnitest/JNI/mylib. c
Sharedlibrary: libmylib. So
Install: libmylib. So => apps/jnitest/libs/armeabi
You can see that the compilation is successful and libmylib. So is generated under <project>/libs/armeabi.
Xiao Jin: After the ndk R4 version, you can directly execute the ndk-build command under <project> without creating a connection. The command is located in the ndk directory.
Test
Before running the program, make sure that all the files in your project are complete. It should be similar to the following:
Then test. Normally, we can see that the log has output 3.
From above: http://blog.163.com/hancker_31/blog/static/355873612011376517108/
《
Android ndk for beginners