In the first half, a company project, itself I do not like the development of Android, but the learning of things always need to tidy up.
Going from iOS to Android is a bit of a real habit. As far as the IDE is concerned, Eclipse is worse than Xcode at 1:30. The Android emulator still has to boot! I'll take a walk.
today write the simplest Android NDK for the little chestnuts. first, what is the Android NDK? The NDK full name is: Native Development Kit, others, shift Baidu Encyclopedia: Android Ndk_ Baidu Encyclopedia1, create a new Android application Project,fuck. 2. New class fuck, declare local static method: Numfromfuck (). The code is as follows:
1 PackageCom.example.fuck;2 Public classFuck {3 4 Public native Static intNumfromfuck ();5 6 Static{7 8System.loadlibrary ("fuck");//Fuck represents the dynamic link library libfuck.so file9 Ten } One A}
3. Add a button to the program, when you click on the button, by calling the local method in class fuck to change the text of TextView, the code is as follows
1 button.setonclicklistener (new Span style= "color: #000000;" > Onclicklistener () { @Override 4 void OnClick (View arg0) { Span style= "color: #008080;" > 5 6 // TODO auto-generated Method stub 7 8 Textview.settext (Fuck.numfromfuck () + " " 10 });
4 , buildthe project, generate the Fuck.class class file (in the bin directory), this step is more important.
5, in the terminal CD to the project directory, create a new folder: "MkDir jni" (terminal input commands are in brackets, the same below), and then enter the command: "Javah-classpath bin/classes-d JNI Com.example.fuck.Fuck ",a file is then found under the Jni folder: Com_example_fuck_fuck.h, where the code is as follows:
1 /*Do not EDIT this file-it are machine generated*/2#include <jni.h>3 4 /*Header for Class Com_example_fuck_fuck*/5 6 #ifndef _included_com_example_fuck_fuck7 8 #define _included_com_example_fuck_fuck9 Ten #ifdef __cplusplus One Aextern "C" { - - #endif the - /* - - * Class:com_example_fuck_fuck + - * Method:numfromfuck + A * Signature: () I at - */ - - jniexport jint jnicall java_com_example_fuck_fuck_numfromfuck - -(JNIENV *, jobject); in - #ifdef __cplusplus to + } - the #endif * $#endif
6. Create a new fuck.c in the JNI directory to implement the method in the. h file generated in 5:
1 #include "com_example_fuck_fuck.h"2 jniexport jint jnicall java_com_example_fuck_fuck_ Numfromfuck (JNIENV *env, jobject obj)34{56 return ; 7 8 }
If you copy the code directly from the. h file, be sure to remember to change the parameters.
7. Create a new file Android.mk file in the Jni file directory:
1 Local_path: = $ (call my-dir)2include $ (clear_vars)34 Local_ MODULE : = fuck56 local_src_files: = fuck.c78 Include $ (build_shared_library)
Local_module:=fuck indicates that the so file to be generated is named: Libfuck.so, which is the dynamic-link library that needs to be loaded in step 2.
8, back to the terminal, CD to the project directory, enter the command: "Ndk-build", yes, will be in the project directory Libs/armeabi generated files: libfuck.so. Last Run Program:Click the button button,textview the content will become "42". over~
2014-03-08