Android JNI Development under Eclipse

Source: Internet
Author: User

1CreateAndroidEngineering

First build an Android project called androidjnitest , Baunimer thinks com.example.androidjnitest , src automatically created under directory Mainactivity.java .

2DesignJNIInterface

Create a new package Com.example.jni, and a new Testjni.java class under the change package .

Open Testjni.java, we will create a JNI interface class in this file that provides an interface for an addition operation:

public class Testjni {public native Boolean init ();p ublic native int Add (int x, int y);p ublic native void Destory ();}

Note that the function declaration here is to addnativekey word.

3 compilingJNI

Copy the Testjni.java file into the project bin directory, enter the project's bin directory in the terminal , enter the javac Testjni.java, a testjni.class file is generated.

Under the bin folder, if not, create a directory:/com/example/jni, and copy testjni.class to /bin/com/ the Example/jni directory. Then in the terminal into the project bin directory, enter javah-jni com. Example.jni.TestJNI, a com_example_jni_testjni.h file is generated .

The com_example_jni_testjni.h. C + + header file corresponds to the Java interface defined above . To open this file, you can see that the system has automatically completed the declaration of the interface function for us:

These three functions correspond to The three interface functions of JNI, which are simply preceded by the Java package name.

4withC + +ImplementJNI

     with jni c/c++ header file, you can c Layer implementation jni interface. First create a jni c/c++ code. Put com_ example_jni_testjni.h file copied to jni directory and create a here com_ example_jni_testjni.cpp file.

because I want to use C + + to achieve JNI , so the above two files I just used as a dynamic link library interface, specific implementation I want to put in a class to complete, so I add two more files: Add.h and the Add.cpp .

Let's implement the CADD class and the JNI interface. First implement the CAdd class:

CAdd.h

#ifndef jni_test_add#define Jni_test_addclass cadd{public:cadd (); ~cadd (); int ADD (int x, int y);}; #endif


CAdd.cpp

#include "Add.h" Cadd::cadd () {}cadd::~cadd () {}int cadd::add (int x, int y) {return x+y;}

then we'll write Com_. Example _jni_testjni.cpp, Implementing JNI:

#include <stdlib.h> #include <stdio.h> #include "Add.h" CAdd *pcadd = NULL; Jniexport Jboolean jnicall java_com_example_jni_testjni_init  (jnienv *env, Jobject obj) {if (pcadd==null) {pCAdd = New CAdd;} return pcadd!=null;} Jniexport jint jnicall java_com_example_jni_testjni_add  (jnienv *env, Jobject obj, Jint x, Jint y) {jint res = -1;if (PC Add!=null) {res = Pcadd->add (x, y);} return res;} jniexport void Jnicall java_com_example_jni_testjni_destory  (jnienv * env, Jobject obj) {if (pcadd!=null) {Delete Pcadd;pcadd=null;}}


To this, ourC + +part of it is all realized.。

5CreateMkfile

after the JNI is implemented, C + + code is compiled into a dynamic-link library . so file so that the Java program can call JNI the interface. To compile so files, you need to write android.mk and application.mk two files.  Let's write android.mkfirst.

first in the project catalog JNI under Create a android.mk File:

Then open the file and enter the following:

Local_path: = $ (call My-dir) include $ (clear_vars)  local_module    : = testjnilocal_src_files: = Com_example_jni_ Testjni.cpplocal_src_files + = Add.cpp include $ (build_shared_library)


whichLocal_pathis aC + +the directory where the code resides, which is ourJNIdirectory.

Local_module is the name of the library to compile. The compiler automatically adds Libto the front, followed by .so.

Local_src_files is a C + + file to compile.

Now we create a application.mk file under the project's root directory and enter the following:

6compiling a dynamic-link library

Once you have finished writing the Mk file, you can start compiling the C + + code.

The NDK Development environment is configured by default under Windows7, open Cygwin, and go to the engineering catalog.

Enter the project's root directory in the terminal and compile with the command "$NDK/ndk-build" command

Successful compilation generates a libtestjni.so file in the Libs/armeabi directory of the project directory .

7in theJavacalled inJNI

Now our Android app can invoke the code for the JNI compute addition, as follows:

static {System.load ("Testjni");} TextView TvX = null; TextView tvy = null; TextView tvsum = null; Button   btnadd = null, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); TvX = (TextView) Findviewbyid (r.id.et_x); TvY = (TextView ) Findviewbyid (r.id.et_y); tvsum = (TextView) Findviewbyid (r.id.et_sum); btnadd = (Button) Findviewbyid (R.id.btn_add); Btnadd.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {int x = integer.valueof (Tvx.gett Ext (). ToString ()); int y = integer.valueof (Tvy.gettext (). ToString ()); int sum = 0; Testjni JNI = new Testjni (); Boolean flag = Jni.init (); if (flag) {sum = Jni.add (x, y);} Btnadd.settext (string.valueof (sum));});}

Program Run Result:

Android JNI Development under Eclipse

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.