Android JNI-learning notes, androidjni
Getting started with JNI-learning notes
Executable File Windows -*. exe | linux -*. elfc function library file Windows -*. dll | linux -*. so batch file Windows -*. bat | linux -*. sh
Tools
NDK -- native developer kits
Cygwin-Linux System Simulator
- Installation Process key points: -- can be installed online during installation or through a local file (if there is a local file) -- Select Packages: No need to install all, install Devel and Shells. After installation, double-click the desktop shortcut icon to go to The Cygwin command console. Command: cd .. | cd .. | ls | cd cygdrive
- To facilitate the execution of the ndk-build command in any directory, You need to configure environment variables: to the installation directory of Cygwin .. /cygwin/etc/profile: Open the profile file and modify the PATH value. Multiple environment variables are separated by colons. For example: PATH = "...:/cygdrive/c/android-ndk-r7b:...", if not, you can also configure the system environment variables in Windows to run with cmd
CDT-to facilitate c code development in eclipse, highlight c code
- Full name: CDT: c/c ++ developer tools ADT: android developer tools
- Installation Procedure: eclipse --> help --> install new software --> add --> Name: cdt | Location: adt.zip --> next. There are two plug-ins for you to select CDT Main Features and cdt Optional Features. You only need to select the former and Contact all update sites... this option is for online updates. You can skip this option. next. After installation, there will be a C/C ++ view in eclipse.
HelloWorld for JNI Development
Step 1:Create an Android project and define a local method
Public class MainActivity extends Activity {// defines a local method, which is equivalent to a c interface. You need to use c code to implement public native String helloFromC (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main );}...}
Step 2:Create a new folder named jni under the project, which must be the file name, and then create a new file under the folder, such as Hello. c. write c code in the C file. The method name is long and Quick Generation Method: cmd -- cd to the src directory of the project, enter the command javah com. example. helloworldformc. mainActivity (full class name) will generate. h header file, copy the file to the jni directory, open the file copy method, remove the semicolon and add {}.
# Include <stdio. h> # include <jni. h> // Java + full class name + method name. Use _ to connect each word jstring encode (JNIEnv * env, jObject obj) {return (* env)-> NewStringUTF (env, "hellofromc ");}
Step 3:Create an Android. mk file in the jni folder
LOCAL_PATH: = $ (call my-dir) include $ (CLEAR_VARS) # package the name of the function library. What file name do you want to package into? What is it written here? LOCAL_MODULE: = hello # name of the file corresponding to C code: LOCAL_SRC_FILES: = Hello. cinclude $ (BUILD_SHARED_LIBRARY)
Step 4:
Package C code into a function library: cmd --> cd to the directory where the Android project is located --> ndk-build (cross-compilation command), refresh the project, and there will be one in libs/armeabi. so file, the file name is lib + hello (the Library name defined in mk), and an obj folder will be added in the project, mainly storing intermediate files
Step 5:Introduce function libraries in Java code
// Use a static code block to introduce the function library static {System. loadLibrary ("hello");} to Java code ");}
Step 6:
Public void method () {// call String str = helloFromC (); Toast. makeText (getApplicationContext (), str, 1). show ();}
** Note **
- When referencing other's. so function libraries, you need to use your native method to match the package name of the corresponding class with that of the. so function library.
To print logs in C, you need to introduce the header file.
Eg: # include <android/log. h>
However, the log method name is too long to be remembered. We need to handle it.
Eg: # define LOGTAG "clog" # define LOGD (...)AndroidLogPrint (ANDROIDLOGDEBUG, LOGTAG, _ VAARGS) # define LOGI (...)AndroidLogPrint (ANDROIDLOGINFO, LOGTAG ,_VAARGS)
Add LOCAL_LDLIBS + =-llog to the mk file.
Call LOGD ("x = % d", x) like this in C code );
C. Call Java
Principle: C calls the Java method through the reflection method. C has similar methods to get the class object, get the methodId, and call the Five return value method CallVoidMethod. The above methods can be used in the header file jni. find in h, jni. h Directory: android-ndk-r7b \ platforms \ android-8 \ arch-arm \ usr \ include through these methods can realize C code call Java code to obtain the method signature: the full Class Name of the class where the javap-s method is located under the classes folder under the cd to bin directory
C
Header file *. h library file *. so/*. o a metaphor for the relationship between the two: header file is equivalent to the interface file in Java, library file is equivalent to Java Implementation class file tool download: http://pan.baidu.com/s/1mg2zvN6
Other highlights
Android learning notes (41) android option menu and SubMenu android learning notes (40) Functions and usage of Notification android learning notes (42) android uses listeners to listen to menu events android learning notes (43) android creates single-choice menus and check menus android learning notes (44) android sets Activityandroid learning notes associated with menu items (45) android context menu jQuery tutorial (12)-ajax operations-loading data based on requests jQuery tutorial (13)-ajax operations append HTML