Jni_android in the project call. So dynamic Library implementation

Source: Internet
Author: User

Transferred from: http://www.yxkfw.com/?p=7223

1. Create a project in eclipse: Testjni

2. Create a new Class:TestJNI.java

Package Com.wwj.jni;  Public class Testjni {    public  native Boolean Init ();      Public int ADD (intint  y);      Public void destory ();}
The above code declares three local methods. 3. Compile jni found in the Android project in the bin directory, there will be a classes folder, and Eclipse automatically generates a bytecode file for us in this directory. In this path, we use the Javah command to generate the. h header file we want to get, as shown in: After executing the javah-jni com.wwj.jni.TestJNI command, the header file is generated in the classes directory: Com_wwj_jni_ TestJNI.h copy it to the JNI folder and open the following:
/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class Com_wwj_jni_testjni*/#ifndef _included_com_wwj_jni_testjni#define_included_com_wwj_jni_testjni#ifdef __cplusplusextern "C" {#endif/** Class:com_wwj_jni_testjni * method:init * Signature: () Z*/jniexport Jboolean jnicall java_com_wwj_jni_testjni_init (jnienv*, jobject);/** Class:com_wwj_jni_testjni * method:add * Signature: (II) I*/jniexport jint jnicall java_com_wwj_jni_testjni_add (jnienv*, Jobject, Jint, jint);/** Class:com_wwj_jni_testjni * method:destory * Signature: () V*/Jniexportvoidjnicall java_com_wwj_jni_testjni_destory (jnienv*, jobject); #ifdef __cplusplus}#endif#endif
The above code is throughThe JavaH command generates JNI layer code. 4. Implementation of JNI in JNI using C + + folder , create the com_wwj_jni_testjni.h corresponding CPP file: Com_wwj_jni_testjni.cppWe add two more file Add.h,add.cpp, the implementation is placed in these two files to complete. Add.h
#ifndef _test_jni_add_h_ #define _test_jni_add_h_class  CAdd {public:    CAdd ();     ~CAdd ();     int ADD (intint  y);}; #endif

Add.cpp

" Add.h " Cadd::cadd () {}cadd::~CAdd () {}int cadd::add (intint  y) {     return x + y;}

Implementation of Com_wwj_jni_testjni.cpp:

#include <stdio.h>#include<stdlib.h>#include"com_wwj_jni_testjni.h"#include"Add.h"CADD*pcadd =NULL; Jniexport Jboolean jnicall java_com_wwj_jni_testjni_init (jnienv*env, Jobject obj) {    if(Pcadd = =NULL) {Pcadd=NewCAdd; }    returnPcadd! =NULL;} Jniexport jint jnicall java_com_wwj_jni_testjni_add (jnienv*env, Jobject obj, Jint x, Jint y) {    intres =-1; if(Pcadd! =NULL) {Res= pcadd->Add (x, y); }    returnRes;} JniexportvoidJnicall Java_com_wwj_jni_testjni_destory (JNIENV *env, Jobject obj) {    if(Pcadd! =NULL) {Pcadd=NULL; }}
5. Create the Mk file and use the Ndk-build command to generate the. So dynamic link library file in the JNI directory, create the Android.mk file as follows:
Local_path: = $ (call my-dir) include $ (clear_vars) Local_module:= testjnilocal_src_files:=  + = Add.cppinclude $ (build_shared_library)
which Local_path is the directory in which C + + code is located, which is our JNI directory. Local_module is the name of the library to compile. The compiler automatically adds Lib to the front, followed by. So. Local_src_files is a C + + file to compile. Then I also need to create the Application.mk file in the root directory of the Android project:
App_project_path: = $ (call my-dir) app_modules:= Testjni
After writing these two MK files, we can use the NDK to generate the corresponding dynamic link library for us. If you need to download the NDK and configure the NDK path to the PATH environment variable, the path I configured is: D:\Cocos2dx\ android-ndk-r9d, depending on individual circumstances. Enter the directory where the Application.mk file is located and use Ndk-build on the command line to generate the. so file successful compilation generates a libtestjni.so file in the Libs/armeabi directory of the project directory. The project structure will become as follows: 6. Invoking JNI in Java
Package Com.wwj.jni;import android.os.bundle;import android.widget.textview;import android.app.Activity; Public classTestjniactivity extends Activity {PrivateTextView TextView; Static {        //loading the dynamic librarySystem.loadlibrary ("Testjni"); } @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); TextView=(TextView) Findviewbyid (R.id.textview); Testjni Testjni=NewTestjni (); //calling the native methodBoolean init =Testjni.init (); if(init = =true) {            //Call the Add function            intsum = Testjni.add ( -, Max); Textview.settext ("you're such a"+sum); } Else{Textview.settext ("you're 250 more than 250 .");    } testjni.destory (); }}

Run the project as follows:

Jni_android in the project call. So dynamic Library implementation

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.