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 native int Add (int x, int y);    public native void Destory ();}
The above code declares three local methods.  3. Compiling JNIfind an Android project in the bin directory, there will be Classes folder, Eclipse automatically generated for us the bytecode file is in this directory. we use the Javah command under this path to generate the. h header file we want to get, as shown in:after the Javah-jni com.wwj.jni.TestJNI command is executed, the header file is generated in the classes directory: Com_wwj_jni_testjni.hcopy it to the JNI folder and open the following:
/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class Com_wwj_jni_testjni */#ifnd EF _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 */jniexport void Jnicall java_com_wwj_jni_testjni_destory  (jnienv *, jobject); #ifdef __cplusplus} #endif #endif
The code above is to generate JNI layer code through the Javah command.  4. Implementation of JNI using C + +under the JNI folder, create the com_wwj_jni_testjni.h corresponding CPP file: Com_wwj_jni_testjni.cpp  we 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 (int x, int y);}; #endif

Add.cpp

#include "Add.h" Cadd::cadd () {}cadd::~cadd () {}int cadd::add (int x, int 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 = new CAdd;    }    return pcadd! = NULL;} Jniexport jint jnicall java_com_wwj_jni_testjni_add (jnienv *env, Jobject obj,        jint x, jint y) {    int res = -1;
    
     if (Pcadd! = NULL) {        res = Pcadd->add (x, y);    }    return res;} Jniexport void Jnicall 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 fileCreate the Android.mk file under the JNI directory as follows:
Local_path: = $ (call My-dir) include $ (clear_vars) Local_module: = testjnilocal_src_files: = Com_wwj_jni_ Testjni.cpplocal_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 class TestJNIActivity extends    Activity {private TextView TextView;    static {//Load Dynamic library system.loadlibrary ("Testjni");        } @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        TextView = (TextView) Findviewbyid (R.id.textview);        Testjni Testjni = new Testjni ();        Call the Native method Boolean init = Testjni.init ();            if (init = = true) {//Call the ADD function int sum = TESTJNI.ADD (100, 150);        Textview.settext ("You are really a" + sum);        } else {Textview.settext ("You are 250 more than 250");    } testjni.destory (); }}

Run the project as follows:

Jni_android in the project call. So dynamic Library implementation

Related Article

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.