Android NDK R9 environment configuration, development tutorial
NDK is a tool set that allows your applications to use languages such as C and C ++ of local code. Android NDK is added with the Native Development Kit in front of the SDK. Therefore, it is also called "NDK" by Google ".
Previously, third-party applications on the Android platform were developed by Java-based Dalvik virtual machines. S.
S. The announcement of NDK allows developers who originally engaged in C language to participate in the Android platform, and also allows java developers to conveniently call underlying operations, especially game developers who have high requirements on engines and speed, A new tool is provided to implement the required functions and make more perfect programs.
This can be helpful for some types of applications, so you can reuse existing code libraries written in these languages, but most applications do not need NDK.
Android NDK R9 update:
Android NDK R9 is mainly prepared for Android 4.3 Development.
With the release of the Android 4.3 platform, Google also released an updated version of Android NDK (r9. The new NDK allows you to locally access OpenGL ES 4.3 APIs in Android 3.0 and other stable APIs. This version can be used if you use the high-performance graphics function in a game or application.
What is Android NDK?
Android NDK is short for Native Development Kit, which is a local Development Kit.
1. NDK is a collection of tools.
NDK provides a series of tools that greatly help developers. They can help developers quickly develop C (or C ++) dynamic libraries and automatically package so libraries and java applications into apk.
NDK integrates with the cross compiler and provides mk files to isolate differences such as CPU, platform, and ABI, developers can create the so library by simply 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.
2. NDK provides a stable and functional API header file statement. Google explicitly states that this API is stable and supports the currently released API in all subsequent versions. From the NDK version, we can see that these Apis support very limited functions, including: C standard library (libc), standard Math Library (libm), and compression library (libz), Log Library (liblog ).
Android NDK configuration Tutorial: tool environment (Android Developer Tools, android-ndk-r9d, Android4.3 API18)
For the development of NDK in Windows, if the NDK is earlier than r7, Cygwin must be installed to use NDK; from r7, Google provides a ndk-build.cmd script, you can compile it directly without using Cygwin. You can configure the path variable and run the ndk-build command under cmd to compile the project in the android project.
1, JAVA_HOME: C: Program Files (x86) Javajdk1.7.0 _ 17 (jdk installation directory)
2, CLASSPATH:.; % JAVA_HOME % lib ools. jar (fixed)
3, ANDROID_NDK_HOME: D: android-ndk-r9d (ndk extract directory)
4. PATH: Add "% JAVA_HOME % in; % ANDROID_NDK_HOME %;" to the original directory.
5, test
Enter java-version and ndk-build-h in cmd.
"Xxx is successful if it is not an internal or external command"
Android NDK development tutorial (please follow the tutorial sequence and test it yourself)
Tool environment (Android Developer Tools, android-ndk-r9d, Android4.3 API18)
1. Create an android Project (NDKhi) in ant (eclipse ).
2. Create a jniClient. java file in com. example. ndkhi under src.
The role of this file:
1. Load the local C library file (. so ).
2. Declare the method (static public native String sayHi () in the local C library ()).
JniClient. java source code:
Package com. example. ndkhi;
Public class jniClient {
Static {
System. loadLibrary (Ndkhi );
}
Static public native String sayHi ();
}
3. Compile the android File
Eclipse automatically compiles by default. In the workspaceNDKhiinclassescomexample dkhi directory, check whether the jniClient. class file exists. If it does not exist, manually compile the android project.
4. Use javah to generate the. h header file in C Language
Create a folder named jni in the NDKhi project and open window cmd and cd to the jni directory,
InputJavah-Classpath ../bin/classes/com. example. ndkhiIf the command is successfully executed, the com_example_ndkhi_jniClient.h file is generated in the jni directory.
Refresh the NDKhi project in eclipse, that is, the com_example_ndkhi_jniClient.h file is visible.
Com_example_ndkhi_jniClient.h source code (this file is automatically generated and generally does not need to be modified ):
/* Do not edit this file-it is machine generated */
# Include
/* Header for class com_example_ndkhi_jniClient */
# Ifndef _ Included_com_example_ndkhi_jniClient
# Define _ Included_com_example_ndkhi_jniClient
# Ifdef _ cplusplus
Extern C {
# Endif
/*
* Class: com_example_ndkhi_jniClient
* Method: sayHi
* Signature: () Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_ndkhi_jniClient_sayHi
(JNIEnv *, jclass );
# Ifdef _ cplusplus
}
# Endif
# Endif
5. Write c language programs
In the jni directory, create a file named com_example_ndkhi_jniClient.. c to implement the function declared in com_example_ndkhi_jniClient.h.
Com_example_ndkhi_jniClient.. c source code:
# Include
JNIEXPORT jstring JNICALL Java_com_example_ndkhi_jniClient_sayHi
(JNIEnv * env, jclass arg)
{
Return (* env)-> NewStringUTF (env, android ndk hi !!!);
}
6. Android. mk file (makefile)
In the jni directory, create a file named Android. mk.
The content is as follows:
LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)
LOCAL_MODULE: = Ndkhi
LOCAL_SRC_FILES: = com_example_ndkhi_jniClient.c
Include $ (BUILD_SHARED_LIBRARY)
Modify the xxx. c file and the. so file to be generated for the self-created. c file by Ndkhi.
7. Compile and link the C program
Open windows cmd, cd to the workspaceNDKhi directory, and enter Ndk-buildCommand
If successful, for example:
After the link is compiled, libNdkhi. so will be generated under libs/armeabi/of the NDKhi project,
8. Compile MainActivity. java.
Source code:
Package com. example. ndkhi;
Import android. OS. Bundle;
Import android. R. string;
Import android. app. Activity;
Import android. view. Menu;
Import android. widget. TextView;
Public class MainActivity extends Activity {
Private TextView tView;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
TView = (TextView) findViewById (R. id. TV );
String strHi = jniClient. sayHi ();
TView. setText (strHi );
}
}
9. Run the android project NDKhi