1. Installation
After downloading the ndk package, put it in the directory of the same level as the SDK and configure the environment variables. After the configuration is completed, enter the ndk-build command on the terminal to test the installation. If the following occurs, the installation is successful.
Android NDK: Could not find application project directory ! Android NDK: Please define the NDK_PROJECT_PATH variable to point to it. /home/braincol/workspace/android/android-ndk-r5/build/core/build-local.mk:85: *** Android NDK: Aborting . Stop.
2. Engineering and code
A. Create an android project, write Java code, write the required local method, and load the c file name.
B. Generate the. h c file based on the written Java code. The steps for generating a. h file are as follows:
1. Go to the project directory
Braincol @ Ubuntu :~ $ CD workspace/Android/ndk/Hello-JNI/
2. ls viewing project files
Braincol @ Ubuntu :~ /Workspace/Android/ndk/Hello-JNI $ ls
Androidmanifest. xml assets bin default. properties Gen res SRC
3. Create a JNI folder under the project directory:
Braincol @ Ubuntu :~ /Workspace/Android/ndk/Hello-JNI $ mkdir JNI
Braincol @ Ubuntu :~ /Workspace/Android/ndk/Hello-JNI $ ls
Androidmanifest. xml assets bin default. properties Gen JNI res SRC
4. Execute the following statement to generate the corresponding. h file:
Braincol @ Ubuntu :~ /Workspace/Android/ndk/Hello-JNI $ javah-classpath bin-d jni com. example. hellojni. hellojni
C. Compile the corresponding. c file based on the. h file generated in B, and implement the method in the. h file according to your own needs.
D. Compile the Android. mk file in the following format:
Local_path: = $ (call my-DIR) include $ (clear_vars) local_module: = hello-JNI // the target object to be compiled. The system will generate 'libello-JNI. so 'file for the Java file to call local_src_files: = hello-jni.c // compiled source file, the system will generate the target object include $ (build_shared_library) based on the file)
E. generate. so shared library file, enter the directory of the project in the terminal, enter the ndk-build command, you can generate in the lib directory. to run the project.
3. debug statement output
In ndk, printf () cannot be output, so we need to use the log library to output the content in our C code library through the Java console. Call function _ android_log_print () to view the related output information in eclipse.
Therefore, in the compiling of the C file, Android/log. h file must be introduced:
# Include <Android/log. h>
At the same time, declare the printing method:
# DEFINE _ android_log_print (...); for example:
#include <string.h>#include <stdio.h>#include <jni.h>#include <android/log.h>#define LOG_TAG "MYJNI"#define LOGI(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)static char s_string[] = "My god, I did it!";jstringJava_com_jpf_myjni_MyJNI_stringFromJNI( JNIEnv* env,jobject thiz ){ LOGI("MyJNI is called!"); return (*env)->NewStringUTF(env, s_string);}
Because printing is a linked log library, add the following sentence to the Android. mk file:
Local_ldlibs + =-llog
Re-run ndk-build and run the project. You can find the print statement in the C file in logcat.
Refer:
Http://www.cnblogs.com/hibraincol/archive/2011/05/30/2063847.html
Http://blog.csdn.net/xuxinyl/article/details/6409030