Android-jni invoking a simple instance resolution
Last Update:2015-11-13
Source: Internet
Author: User
<span id="Label3"></p><p><p>Turn from: http://www.cnblogs.com/sevenyuan/p/4202759.html thanks to the original Author's careful finishing!</p></p><p style="TEXT-TRANSFORM: none; BACKGROUND-COLOR: rgb(255,255,255); TEXT-INDENT: 0px; MARGIN: 10px auto; FONT: 13px/23px Verdana, Geneva, Arial, Helvetica, sans-serif; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-text-stroke-width: 0px"><p style="TEXT-TRANSFORM: none; BACKGROUND-COLOR: rgb(255,255,255); TEXT-INDENT: 0px; MARGIN: 10px auto; FONT: 13px/23px Verdana, Geneva, Arial, Helvetica, sans-serif; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-text-stroke-width: 0px">1. Create a project in Eclipse: Testjni</p></p><p style="TEXT-TRANSFORM: none; BACKGROUND-COLOR: rgb(255,255,255); TEXT-INDENT: 0px; MARGIN: 10px auto; FONT: 13px/23px Verdana, Geneva, Arial, Helvetica, sans-serif; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-text-stroke-width: 0px"><p style="TEXT-TRANSFORM: none; BACKGROUND-COLOR: rgb(255,255,255); TEXT-INDENT: 0px; MARGIN: 10px auto; FONT: 13px/23px Verdana, Geneva, Arial, Helvetica, sans-serif; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-text-stroke-width: 0px"></p></p><p style="TEXT-TRANSFORM: none; BACKGROUND-COLOR: rgb(255,255,255); TEXT-INDENT: 0px; MARGIN: 10px auto; FONT: 13px/23px Verdana, Geneva, Arial, Helvetica, sans-serif; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-text-stroke-width: 0px"><p style="TEXT-TRANSFORM: none; BACKGROUND-COLOR: rgb(255,255,255); TEXT-INDENT: 0px; MARGIN: 10px auto; FONT: 13px/23px Verdana, Geneva, Arial, Helvetica, sans-serif; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-text-stroke-width: 0px">2. Create a new Class:TestJNI.java</p></p><pre class="brush:java;toolbar: true; auto-links: false;"><pre class="brush:java;toolbar: true; auto-links: false;">Package com.wwj.jni; public class Testjni {public native Boolean Init (); public native int Add (int x, int y); Public native void Destory ();}</pre></pre><p><p><span style="LINE-HEIGHT: 1.5 !important; PADDING-RIGHT: 5px; FONT-FAMILY: ‘Courier New‘ !important; FONT-SIZE: 12px !important"></span></p></p><p><p>The above code declares three local methods.</p></p><p><p>3. Compiling JNI</p></p><p><p>Find an Android project in the bin directory, there will be classes folder, eclipse automatically generated for us the bytecode file is in this directory.</p></p><p><p>We use the Javah command under this path to generate The. h header file we want to get, as shown in:</p></p><p><p></p></p><p><p>After the Javah-jni Com.wwj.jni.TestJNI command is executed, the header file is generated in the classes directory: com_wwj_jni_testjni.h</p></p><p><p>Copy it to the JNI folder and open the Following:</p></p><pre class="brush:cpp;toolbar: true; auto-links: false;">/* do not edit this file - it is 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 */ JNIEXPORT void JNICALL Java_com_wwj_jni_TestJNI_Destory (jnienv *, jobject); #ifdef __cplusplus} #endif #endif</pre><p><p><span style="LINE-HEIGHT: 1.5 !important; PADDING-RIGHT: 5px; FONT-FAMILY: ‘Courier New‘ !important; FONT-SIZE: 12px !important"></span></p></p><p><p><span style="LINE-HEIGHT: 1.5 !important; PADDING-RIGHT: 5px; FONT-FAMILY: ‘Courier New‘ !important; FONT-SIZE: 12px !important"></span></p></p><p><p>The code above is to <span style="WIDTH: auto; FLOAT: none; HEIGHT: auto">generate JNI layer code from the <span style="WIDTH: auto; FLOAT: none; HEIGHT: auto; COLOR: rgb(0,0,255); FONT-SIZE: 16px">Java</span>H command.</span></p></p><p><p></p></p><p><p>4. Implementation of JNI using C + +</p></p><p><p>Under the JNI <span style="WIDTH: auto; FLOAT: none; HEIGHT: auto"> <span style="WIDTH: auto; FLOAT: none; HEIGHT: auto; COLOR: rgb(0,0,255); FONT-SIZE: 16px">folder</span> , create the com_wwj_jni_testjni.h corresponding CPP file: com_wwj_jni_testjni.cpp</span></p></p><p><p></p></p><p><p>We add two more file add.h,add.cpp, the implementation is placed in these two files to Complete.</p></p><p><p>Add.h</p></p><pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">#ifndef _test_jni_add_h_ #define _TEST_JNI_ADD_H_ class CAdd {public:cadd (); ~cadd (); int ADD (int x, int y); }; #endif</pre></pre><p><p><span style="LINE-HEIGHT: 1.5 !important; PADDING-RIGHT: 5px; FONT-FAMILY: ‘Courier New‘ !important; FONT-SIZE: 12px !important"></span></p></p><p><p>Add.cpp</p></p><pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">#include "Add.h" cadd::cadd () {} cadd::~cadd () {} int cadd::add (int x, int y) {return x + y;}</pre></pre><p><p><span style="LINE-HEIGHT: 1.5 !important; PADDING-RIGHT: 5px; FONT-FAMILY: ‘Courier New‘ !important; FONT-SIZE: 12px !important"></span></p></p><p style="TEXT-INDENT: 0px; MARGIN: 10px auto"><p style="TEXT-INDENT: 0px; MARGIN: 10px auto">Implementation of Com_wwj_jni_testjni.cpp:</p></p><p><p><span style="LINE-HEIGHT: 1.5 !important; PADDING-RIGHT: 5px; FONT-FAMILY: ‘Courier New‘ !important; FONT-SIZE: 12px !important"></span></p></p><pre>#include <stdio.h> #include <stdlib.h> #include "com_wwj_jni_testjni.h" #include "Add.h" add *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; }}</pre><p><p><span style="LINE-HEIGHT: 1.5 !important; PADDING-RIGHT: 5px; FONT-FAMILY: ‘Courier New‘ !important; FONT-SIZE: 12px !important"></span></p></p><p><p>5. Create the Mk file and use the Ndk-build command to generate the. so dynamic link library file</p></p><p><p>Create the Android.mk file under the JNI directory as Follows:</p></p><p><p><span style="LINE-HEIGHT: 1.5 !important; PADDING-RIGHT: 5px; FONT-FAMILY: ‘Courier New‘ !important; FONT-SIZE: 12px !important"></span></p></p><pre><pre>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)</pre></pre><p><p><span style="LINE-HEIGHT: 1.5 !important; PADDING-RIGHT: 5px; FONT-FAMILY: ‘Courier New‘ !important; FONT-SIZE: 12px !important"></span></p></p><p><p><strong>which</strong></p></p><p><p><strong><span style="COLOR: rgb(255,0,0)">Local_path</span> is the directory in which C + + code is located, which is our JNI directory.</strong></p></p><p><p><strong><span style="COLOR: rgb(255,0,0)">Local_module</span> is the name of the library to Compile. The compiler automatically adds Lib to the front, followed By. So.</strong></p></p><p><p><strong><span style="COLOR: rgb(255,0,0)">Local_src_files</span> is a C + + file to Compile.</strong></p></p><p><p></p></p><p><p>Then I also need to create the Application.mk file in the root directory of the Android Project:</p></p><pre><pre>App_project_path: = $ (call My-dir) app_modules: = Testjni</pre></pre><p><p>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\<span style="WIDTH: auto; FLOAT: none; HEIGHT: auto"><span style="WIDTH: auto; FLOAT: none; HEIGHT: auto; COLOR: rgb(0,0,255); FONT-SIZE: 16px">android</span>-ndk-r9d, depending on the individual Situation.</span></p></p><p><p>Enter the directory where the Application.mk file is located and use Ndk-build on the command line to generate The. so file</p></p><p><p></p></p><p><p><strong>Successful compilation generates a libtestjni.so file in the Libs/armeabi directory of the project Directory.</strong></p></p><p><p></p></p><p><p>The project structure will become as Follows:</p></p><p><p></p></p><p><p>6. Invoking JNI in Java</p></p><p><p> <span style="line-height:1.5!important; padding-right:5px; Font-family: ' Courier New '!important; Font-size:12px!important "> </span> </p></p><pre>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 (); // calls 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 (); }}</pre><p><p><span style="LINE-HEIGHT: 1.5 !important; PADDING-RIGHT: 5px; FONT-FAMILY: ‘Courier New‘ !important; FONT-SIZE: 12px !important"></span></p></p><p style="TEXT-INDENT: 0px; MARGIN: 10px auto"><p style="TEXT-INDENT: 0px; MARGIN: 10px auto">Run the project as Follows:</p></p><p style="TEXT-INDENT: 0px; MARGIN: 10px auto"><p style="TEXT-INDENT: 0px; MARGIN: 10px auto"></p></p><p><p>Follow the steps above to generate A. So file of your own, which can be called and executed correctly, and add two points here:</p></p><p><p>Note one: the NDK environment configuration:</p></p><p><p>1,:</p></p><p><p><span style="FONT-SIZE: 14px"><strong><span style="FONT-FAMILY: Helvetica; COLOR: #222222">Android NDK r10e:</span></strong></span></p></p><p><p><span style="FONT-FAMILY: 微软雅黑,Microsoft YaHei; COLOR: #222222; FONT-SIZE: 12px"><strong>32-bit: Http://dl.google.com/android/ndk/android-ndk-r10e-windows-x86.exe</strong></span></p></p><p><p><span style="FONT-FAMILY: 微软雅黑,Microsoft YaHei; COLOR: #222222; FONT-SIZE: 12px"><strong>64-bit: Http://dl.google.com/android/ndk/android-ndk-r10e-windows-x86_64.exe</strong></span></p></p><p><p><span style="FONT-SIZE: 14px"><strong><span style="FONT-FAMILY: Helvetica; COLOR: #222222">Android NDK r9d:</span></strong></span></p></p><p><p><span style="FONT-FAMILY: 微软雅黑,Microsoft YaHei; COLOR: #222222; FONT-SIZE: 12px"><strong><span style="FONT-FAMILY: 微软雅黑,Microsoft YaHei; COLOR: #222222; FONT-SIZE: 12px"><strong>32-bit: Https://dl.google.com/android/ndk/android-ndk-r9d-windows-x86.zip</strong></span></strong></span></p></p><p><p><span style="FONT-FAMILY: 微软雅黑,Microsoft YaHei; COLOR: #222222; FONT-SIZE: 12px"><strong>64-bit: Https://dl.google.com/android/ndk/android-ndk-r9d-windows-x86_64.zip</strong></span></p></p><p><p>I use the ndk-r9d version, the above address needs to turn over the wall, the corresponding version of the need to leave the mailbox is Good.</p></p><p><p>2, Configure the PATH environment variable, cmd command Line input ndk-build, appear the content prompt indicates the configuration is Correct.</p></p><p><p>Android ndk:could not find application project directory!<br>Android ndk:please define the Ndk_project_path variable to the it.<br>D:\java\android-ndk-r9d\build/core/build-local.mk:148: * * * Android ndk:aborting<br>. Stop.</p></p><p><p>Note Two: refer to the above article in the last input Ndk-build will prompt the following error:</p></p><p><p></p></p><p><p>At this point, modify the following in the Application.mk file:</p></p><p><p>App_project_path: = $ (call My-dir)/into app_project_path: = $ (call my-dir)/...</p></p><p><p>Android-jni invoking a simple instance resolution</p></p></span>