Read an article in a blog post, feeling that the description can also be:
In the previous blog, the use of Java to develop Android applications, this blog will introduce Java by using JNI call C language to do development
To introduce JNI in a more graphic form, first look at the following image, which represents the entire Android system architecture
The structure from top to bottom in the figure is:
Application: represents the application layer, in fact, refers to the Android phone application
Application Framework: represents the application framework layer, we usually use Java Development app is to use the API provided by the application framework layer to do development
Libraries:Android Class Library, Android class library developed with C + +
androidRuntime: Android Run Environment
Linux Kernel: Linux kernel
The Android system is divided into five parts, namely application (Application layer), Application framework (application framework), Librarise (Android class library), Android runtime (Android Operating environment), Linux Kernel (Linux kernel), and the Android application framework developed using Java, the Android class library was developed in C + +, in order for the Java Development Application framework to invoke the Android class library developed by C + +, The introduction of a JNI in Android allows Java calls to be implemented via JNI c/c++,c/c++ call Java
Tools to prepare for the development of JNI:
Reference: Android ndk download-File too large
Android ndk,android NDK is: http://blog.csdn.net/DJY1992/article/details/51364430
After the download is completed, the NDK decompression of an arbitrary directory, I unzipped in D:\develop, the recommended file directory do not take spaces, extract the path of the NDK is added to the environment variable
After you open the Android NDK, you can see the following directory structure for the Android NDK:
Introduction to the Android NDK directory structure:
docs: Help documentation
build/tools:batch file for Linux
platforms: header files and class libraries to be used for compiling C code
prebuilt: precompiled binary executable file used
Sample:Examples of use of JNI
Source:NDK Source code
toolchains: tool Chain
Ndk-build.cmd: compiling an instruction for packing C code
Using the NDK to implement a simple Hello World program
First step: Create an Android project with Eclipse and name the project Hellojni
The second step: Modify the Activity_main.xml file, add a button on the layout to click the button and then call the C language write code
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity "> <button android:layout_width=" wrap_content " android:layout_height=" Wrap_ Content " android:text=" calls the C language code " android:onclick=" click "/> </RelativeLayout>
Step Three:Create a new JNI folder under the created Android project
Fourth Step: Create a new hello.c file under the Jni folder and add the following code to the hello.c file
#include <stdio.h> #include <stdlib.h> #include <jni.h> /*** * First parameter: Memory address of the Java Virtual machine * Second parameter: The Java object that calls the Hellofromc method * /JAVA_COM_EXAMPLE_HELLOJNI_MAINACTIVITY_HELLOFROMC ( jnienv* env, Jobject obj) { //Create a C-language string, and initialize the string char* arr = "Hello World jni!"; Converts a C string into a Java string jstring jstr = (*env)->newstringutf (env, arr); Returns the string return jstr; }
Fifth Step : Create a new android.mk file under the Jni folder and add the following code to the ANDROID.MK
Local_path: = $ (call My-dir) include $ (clear_vars) #编译生成的文件的类库叫什么名字 local_module : = Hello # c file to compile local_src_files: = hello.c include $ (build_shared_library)
Sixth step:compiling Android NDK program
1 . Open the command line and go to the directory where the JNI is located
2. Enter NDK-BUILD.CML command to compile Android NDK
3 . See the hints shown to indicate that the Android NDK was successfully compiled
4. when you open the Armeabi folder under the Libs folder, you can see that a libhello.so file is generated under the Armeabi folder
This is the Android NDK generated after the file, you can directly use the libhello.so file in the following
final step: use the NDK to compile, modify the Mainactivity.java file
Package com.example.hellojni; Import android.app.Activity; Import Android.os.Bundle; Import Android.view.View; Import Android.widget.Toast; public class Mainactivity extends Activity { static { //load packed so class library system.loadlibrary ("Hello"); } @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); } Call the C Code button response function public void Click (View view) { ///Use the Toast dialog box to display the return value of the function Hellofromc () in the C language code Toast.maketext ( This, HELLOFROMC (), Toast.length_short). Show (); Defines a local method, the method body is implemented by the C language public native String Hellofromc (); }
-------------------------------------the effect after the run------------------------------------------------
The interface shown on the phone will be displayed after running the Android program
Click on the button to eject the toast and show "Hello World jni!" in the toast. Indicates that Android successfully called the C language development of the JNI program
Android JNI. So library calls