Android support using NDK to develop C programs, about configuring the NDK environment issues should not be mentioned again, this online there are many, here through an example to tell the simple JNI development, you can refer to this article (Get Your eclipse-integrated NDK on! Build the development environment where Eclipse compiles the C language for so files.
The native method implements the following steps:
1. Declare the native () method in Java and compile (JAVAC);
2. Create an. h file with Javah;
3. Write C files that contain. h files
4. Compiling c files
5. Use a successful so file.
The first step:
1. Declaration Native method
[Java] view Plaincopyprint?
- Public class Printf_jni {
- Static {
- System.loadlibrary ("Com_nedu_jni_helloword_printf-jni");
- }
- public native void Printhello ();
- }
2. Javac Compilation
Go to the path where the Java file is located, call the Javac command,
Step two: Use the Javah command to generate the. h header file,
This to go back to the SRC directory, do not know what the reason, if the above Javac path will be an error,
The header files generated using the Javah command are as follows:
[CPP]View Plaincopyprint?
- /* Don't EDIT this file-it are machine generated */
- #include <jni.h>
- /* Header for class COM_NEDU_JNI_HELLOWORD_PRINTF_JNI */
- #ifndef _included_com_nedu_jni_helloword_printf_jni
- #define _included_com_nedu_jni_helloword_printf_jni
- #ifdef __cplusplus
- extern "C" {
- #endif
- /*
- * Class:com_nedu_jni_helloword_printf_jni
- * Method:printhello
- * Signature: () V
- */
- Jniexport void Jnicall Java_com_nedu_jni_helloword_printf_1jni_printhello
- (JNIENV *, jobject);
- #ifdef __cplusplus
- }
- #endif
- #endif
Step three: Write the C file with the following code:
[CPP] view plaincopyprint?
- #include <stdio.h>
- #include <stdlib.h>
- #include "com_nedu_jni_helloword_printf_jni.h" &NBSP;&NBSP;&NBSP;&NBSP;
- jniexport void jnicall java_com_nedu_jni_helloword_printf_1jni_ printhello
- (JNIENV&NBSP;*E,&NBSP;JOBJECT&NBSP;J)
- {
- printf ( "hello world!");
- }
Fourth step, write Android.mk file, compile C file
The Android.mk file is as follows:
[plain] view plaincopyprint?
- Local_path: = $ (call My-dir)
- Include $ (clear_vars)
- Local_module: = Com_nedu_jni_helloword_printf-jni
- Local_src_files: =PRINTF_JNI.C
- Include $ (build_shared_library)
Local_module: = Com_nedu_jni_helloword_printf-jnilocal_module: = Com_nedu_jni_helloword_printf-jnilocal_module means so Filename
Local_src_files files that need to be compiled
Following the introduction of this article (Get Your eclipse-integrated NDK on!), you can compile it in Eclipse.
Fifth step: Using the so file:
Load the so file with the following code
[HTML] view Plaincopyprint?
- System.loadlibrary ("Com_nedu_jni_helloword_printf-jni");
Load the so file with the following code to load the so file with the following code
The call is as follows:
[Java]View Plaincopyprint?
- Printf_jni print=New Printf_jni ();
[Java]View Plaincopyprint?
- Print.printhello ();
/**
* @author Zhang Xingye
* Email: xy-zhang#163.com
* Android Development Advanced Group: 278401545
*
*/
Android JNI Introduction First article--hellojni