1, need to prepare tools, ECLIPSE,CDT (c + +) plug-ins, Cygwin (Unix) and Android NDK.
In Cygwin's etc directory, the NDK path is introduced into the profile file, which can be accessed from any directory in Cygwin to the NDK, and different ndk path paths are written differently.
: ${original_path=${path}} if"addwinpath" ]; Then PATH="/usr/local/bin:/cygdrive/d/cgwin/android-ndk-r7b:/usr/bin${path:+:${path}} " else PATH="/usr/local/bin:/usr/bin:/ cygdrive/d/cgwin/android-ndk-r7b" fi
2. Development steps:
Public native String Hellofromjni (); generates a method signature for JNI by Javah the name of the package. C. Create a JNI directory, write C code, and the method name corresponds. or copy the generated signature file to the JNI directory and then introduce it in C code. D. Write the Android.mk file E.ndk compile the Build Dynamic library (ndk-build) F.java code load Dynamic Library. Calling native code
2.1 Create an Android project and create the native method.
Public class extends Activity { static{ // load binary file system.loadlibrary (" Hello "); // libhello.so remove lib and so } publicnative String helloformc (); @Override protectedvoid onCreate (Bundle savedinstancestate) { Super . OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); System.out.println ("------------" +HELLOFORMC ());} }
2.2 Javah Command successfully signed: Enter the project's bin/classes command, execute the command javah com.example.jni.MainActivity, copy the generated header file to the JNI directory, and introduce the header file in the C file #include " Com_example_jni_mainactivity.h ". The following are the parameters of the Javah command and the method of producing the header file.
2.3 Creating a C file
#include <stdio.h><jni.h><malloc.h>"com_example_jni_ MainActivity.h"; Jniexport jstring jnicall java_com_example_jni_mainactivity_helloformc * env, Jobject obj) { return (*env)->newstringutf (env,"");}
2.4 Compile the C file through Cygwin, go to the project directory, my project name is JNI, enter after execute "ndk-build" command, appear as shown to indicate execution success.
2.5 Create the Android.mk file, you need to replace HELLO-JNI with your own C file module name, in the NDK location file:///D:/cgwin/android-ndk-r7b/docs/ANDROID-MK.html:
Local_path: = $ (call my-dir) include $ (clear_vars) local_module := Hellolocal_src_files:= Hello . Cinclude $ (build_shared_library)
2.6 A Java file in an Android project introduces a compiled generated binary file.
Static { // load binary file system.loadlibrary ("Hello"); libhello.so Remove lib andso}
2.7 The last deployment project, run to see the call succeeds.
----------------------------------------
Ready to be sorted
android.mk File Additions Local_ldlibs + =- Llog
C Add in Code
#include <android/ log.h >
#define Log_tag " System.out "
#define logd (...) __ Android_log_print (Android_log_debug, Log_tag, __va_args__)
#define Logi (...) __ Android_log_print (Android_log_info, Log_tag, __va_args__)
logi ("info\n");
logd ("debug\n");
jstring Java_cn_itcast_ndk_demoactivity_hellofromjni (jnienv *env, jobjectjavathis) {
return (*env)-newstringutf(env, "Hello from native code!");
}
jnienv type represents the Java environment through jnienv * Pointers , will be able to Java end of the code to operate .
Create Java object of the class , called Java methods of the object
Get Java object Properties, and so on. .
Jobject What is it?
Jobject obj is the object represented by the class in which the current method resides .