Android-jni Build and call

Source: Internet
Author: User

Reprint please indicate the source, thank you ~

First to create a new project, named Hellojni. Then create a new folder in the project directory JNI.

I'm using Eclipse.


Then we use the shortcut key Alt+enter to enter the project Properties option, select Builder in the left column, and then click New:


After that we select program and then there is a popup window:


Set two paths in the main option, the first one is the path of the ndk-build.cmd in the NDK, I use NDK-R9, the second is the engineering path, our Hellojni project, and then the Refresh option:


Check the box and look at options:


After the tick will be reported a mistake, at this time OK can not point, then we point specify Resources ... option to enter another interface:


Here we select the JNI directory of the HELLOJNI project and click Finish. Then click OK to complete the setup.

After we write our JNI header file:

We're going to create a new class in SRC that says the native method we're going to use in the class:

Package Com.ocean.hellojni.jni;public class Jniclient {public static native string getString (string a,string b);p Ublic St atic native int getInt (int a,int b);}

Then, if you use Eclipse, the classes file will be generated automatically, others may need to be generated manually, and then we can use the cmd command line to generate the header file:


The method is to first enter into the HELLOJNI project directory, and then enter the bin classes directory, then execute the statement javah path. Path is the whole path of the class that you wrote native local method, that is, the package name, the path plus the method name, no suffix name, and then build the. h header file, then copy it to the JNI directory and open:

/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class Com_ocean_hellojni_jni_jnic Lient */#ifndef _included_com_ocean_hellojni_jni_jniclient#define _included_com_ocean_hellojni_jni_jniclient# ifdef __cplusplusextern "C" {#endif/* * Class:     com_ocean_hellojni_jni_jniclient * Method:    getString * Signature : (ljava/lang/string; ljava/lang/string;) ljava/lang/string; */jniexport jstring jnicall java_com_ocean_hellojni_jni_jniclient_getstring  (jnienv *, Jclass, jstring, jstring) ;/* Class:     com_ocean_hellojni_jni_jniclient * Method:    getInt * Signature: (II) I */jniexport jint Jnicall java_c Om_ocean_hellojni_jni_jniclient_getint  (jnienv *, Jclass, Jint, jint); #ifdef __cplusplus} #endif #endif
According to it, write our C + + file:

#include "com_ocean_hellojni_jni_jniclient.h" #include <stdio.h> #include <stdlib.h> #ifdef __ Cplusplusextern "C" {#endif */* Class:     com_ocean_hellojni_jni_jniclient * Method:    getString * Signature: (Ljava /lang/string; ljava/lang/string;) ljava/lang/string; */jniexport jstring jnicall java_com_ocean_hellojni_jni_jniclient_getstring  (jnienv *env, Jclass arg, jstring StrA , jstring StrB) {jstring str = (*env)->newstringutf (env, "Hello jni!"); return str;} /* Class:     com_ocean_hellojni_jni_jniclient * Method:    getInt * Signature: (II) I */jniexport jint Jnicall Java_co M_ocean_hellojni_jni_jniclient_getint  (jnienv *env, Jclass Arg, jint A, Jint b) {return a+b;} #ifdef __cplusplus} #endif

It should be noted here that the method name is consistent with the header file.

Then we write the. mk File:

Local_path: = $ (call My-dir) include $ (clear_vars) Local_module: = hellojnilocal_src_files: = Com_ocean_hellojni_jni_ Jniclient.cinclude $ (build_shared_library)
C and C + + 's. mk files may be slightly different, but that's roughly the case, as may be mentioned later in the blog post.

Then call the local method we wrote in the Java class:

Package Com.ocean.hellojni;import Android.app.activity;import Android.os.bundle;import android.view.Menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.edittext;import Android.widget.textview;import Com.ocean.hellojni.jni.jniclient;public class Mainactivity extends Activity {private TextView show;private TextView answer;private EditText one;private EditText two;static{system.loadlibrary ("Hellojni") ;} @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); show = (TextView) Findviewbyid (r.id.show); answer = (TextView) Findviewbyid (r.id.answer); one = (EditText) Findviewbyid (R.id.et_one); (EditText) Findviewbyid (r.id.et_two); String string = Jniclient.getstring ("1", "2"); Show.settext (string); Answer.setonclicklistener (new Onclicklistener () { @Overridepublic void OnClick (View arg0) {if (One.gettext (). ToString () = null && two.gettext (). ToString () = null &&!one.gettext (). ToString (). Equals ("") &&!two.gettext (). ToString (). Equals ("")) {int i = Jniclient.getint ( Integer.valueof (One.gettext (). ToString ()), integer.valueof (Two.gettext (). ToString ()); Answer.settext ("Result:" + i);}});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
Execution See results:




ok! There should be applause here ~ ~


Android-jni Build and call

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.