The Jni_android project calls the. So dynamic library implementation in detail "go"

Source: Internet
Author: User

Transfer from http://www.cnblogs.com/sevenyuan/p/4202759.html

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. 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:
/* 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 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 (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 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: = 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 (+);            Textview.settext ("You are really a" + sum);        } else {            Textview.settext ("You are 250 more than 250");        }        Testjni.destory ();    }}

Run the project as follows:

In the Jni_android project. So dynamic library implementation in detail "go"

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.