1. First write the class library containing the native method
Package com.miles.libs; Public class Hello { // load local library static { system.loadlibrary ("Hello" ); } // Methods in the local library Public native String hello ();}
2, using Javah to generate A/C + + header file
Javah-classpath Compiling directory class names
Javah-classpath ./bin/classes Com.miles.libs.Hello
Generated header file: com_miles_libs_hello.h
/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class Com_miles_libs_hello*/#ifndef _included_com_miles_libs_hello#define_included_com_miles_libs_hello#ifdef __cplusplusextern "C" {#endif/** Class:com_miles_libs_hello * Method:hello * Signature: () ljava/lang/string;*/jniexport jstring jnicall Java_com_miles_libs_hello_hello (jnienv*, jobject); #ifdef __cplusplus}#endif#endif
PS: This file is in the directory where you call the Javah command
3. Create a new JNI file directory to copy the header files, and write C + + files in the directory
code file: com_miles_libs_hello.c
#include <jni.h>"com_miles_libs_hellolibs.h"*env, Jobject Thiz); { return"Hello JNI! " );}
4. Writing android.mk files
Local_path : = $ (call my-dir) include $ (clear_vars) local_module := hellolocal_src_files: = com_miles_libs_hello.cinclude $ (build_shared_library)
For the preparation of the Mk file, please check it yourself.
5, compiled in the configuration of the NDK environment to compile
I am using android-ndk-r8c this version compiled, in the high version of the compilation will be error, no in-depth study!
Simple JNI Programming for Android