Using JNI under Android

Source: Internet
Author: User

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:
Android ndk,android NDK is: http://blog.csdn.net/u010105970/article/details/45152629

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_ The 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: The memory address of the Java Virtual machine * The second parameter: Call the Java object of 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!"; /convert C string to Java string jstring jstr = (*env)->newstringutf (env, arr);//returns 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, at this time open the Libs folder under the Armeabi folder, you can see Armeabi folder generated a libhello.so file, this is the Android NDK compiled files generated, You can use the libhello.so file directly in the back



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");} @Overrideprotected 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 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


Using JNI under Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.