Android JNI development under Eclipse, androidjni

Source: Internet
Author: User

Android JNI development under Eclipse, androidjni

 

1. Create an Android Project

First, create an Android project named AndroidJniTest. The package name defaults to com. example. androidjnitest. MainActivity. java is automatically created under the src directory.

 

 

2. design the JNI Interface

Create a new file package com. example. jni and create a new class TestJNI. java under the package.

 

Open TestJNI. java and we will create a JNI interface class in this file. This Java class provides an addition operation interface:

public class TestJNI {public native boolean init();public native int add(int x , int y);public native void destory();}

Note that the native keyword must be added to the function declaration.

3. Compile JNI

Copy the TestJNI. java file to the bin directory of the project, enter the bin directory of the project in the terminal, and enter javac TestJNI. java. A file named TestJNI. class is generated.

 

 

In the bin folder, if not, create the Directory:/com/example/jni and copy TestJNI. class to the/bin/com/example/jni directory. Enter the bin directory of the project in the terminal and enter javah-jni com. example. jni. TestJNI. A com_example_jni_TestJNI.h file is generated.

 

The com_example_jni_TestJNI.h file is the C/C ++ header file corresponding to the Java interface defined above. Open this file and you can see that the system has automatically declared the interface function for us:

 

These three functions correspond to the three interface functions of JNI respectively. The naming method only adds the Java package name.

4. Use C/C ++ to implement JNI

With the jni c/C ++ header file, you can implement the JNI interface on the C layer. First, create a jni directory under the project directory, which is specifically used to put C/C ++ code. Copy the com_example_jni_TestJNI.h file to the jni directory and create a com_example_jni_TestJNI.cpp file.

Since I want to use C ++ to implement JNI, the above two files are only used as interfaces for Dynamic Link Libraries. I hope to implement them in a class, so I will Add two more files: Add. h and Add. cpp.

 

The following describes how to implement the CAdd class and JNI interfaces. 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 will write com_example_jni_TestJNI.cpp to implement 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(pCAdd!=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;}}


At this point, all the C/C ++ sections are implemented.

5. Create an mk File

After JNI is implemented, it is necessary to compile the C/C ++ code into a dynamic link library. so that the Java program can call the JNI interface. To compile the so file, you need to write the Android. mk and Application. mk files. Let's first write Android. mk.

Create an Android. mk file under jni in the project directory:

 

Open the file and enter the following content:

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)


LOCAL_PATH is the directory where C/C ++ code is located, that is, our jni directory.

LOCAL_MODULE is the name of the library to be compiled. The compiler automatically adds lib to the front and. so to the back.

LOCAL_SRC_FILES is the C/C ++ file to be compiled.

Now, create an Application. mk file under the root directory of the project and enter the following content:

APP_PROJECT_PATH := ${call my-dir}APP_MODULES := TestJNI 

6. Compile the dynamic link library

After writing the mk file, you can start compiling C/C ++ code.

By default, the NDK development environment is configured in Windows 7. Open cygwin and go to the project directory.

 

Enter the root directory of the project in the terminal, and enter the "$ NDK/ndk-build" command to compile

 

After compilation, A libTestJNI. so file is generated in the libs/armeabi directory of the project directory.

 

 

7. Call JNI in Java

Now, our Android app can call the JNI computing addition Code 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.getText().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 running result:

 

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.