Android calls Jni method and code
This address: Http://blog.csdn.net/caroline_wendy
Jni:java Native Interface to achieve interoperability between Java and C + +.
The method of using JNI on Android . Time: 2014.9.3
Environment: must be Use standard eclipseto install the Android environment before you can use the NDT plugin .
Eclipse standard/sdk Version:luna Release (4.4.0);
Android:adt-23.0.3.zip; Ndt:gnu make 3.81;
Basic installation skipped.
Method:
1. Create the interface class:
First, create a new JNI interface class that contains the static methods used. Location: Project->src->[package]->jniclient.java
/** * */package com.example.hellomyjni;/** * @author Administrator * */public class Jniclient {static public native St Ring Sayname ();}
2. Compile the interface class:
Enter the project folder, generate the JNI header file , and use the command:
"Javah-classpath bin/classes-d jni com.example.hellomyjni.JniClient"
Command parsing:
javah generate header file;
-classpath uses the location of the class (Bin/classes), which are. class files;
- D JNI requires the generation of JNI classes (com.example.hellomyjni.JniClient), including [package]. [classname].
Press F5 to refresh the project, and the project will automatically generate a Jni folder with a header file "Com_example_hellomyjni_jniclient.h".
/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class Com_example_hellomyjni_jnic Lient */#ifndef _included_com_example_hellomyjni_jniclient#define _included_com_example_hellomyjni_jniclient# ifdef __cplusplusextern "C" {#endif/* * Class: com_example_hellomyjni_jniclient * Method: sayname * Signature: () ljava/lang/string; */jniexport jstring jnicall java_com_example_hellomyjni_jniclient_sayname (jnienv *, jclass); #ifdef __cplusplus} #endif #endif
3. Configure Nativesupport for Android projects.
Right click on the item in Android Tools and click "Add nativesupport", it will be generated automatically : HelloMyJni.cpp and Android.mk.
Android.mk does not need to be modified:
Local_path: = $ (call My-dir) include $ (clear_vars) local_module : = hellomyjnilocal_src_files: = Hellomyjni.cppinclude $ (build_shared_library)
HelloMyJni.cpp needs to be modified.
4. Add the required functions
That is, modify HelloMyJni.cpp.
First, add the header file , the JNI generated header file, #include "com_example_hellomyjni_jniclient.h"; The error of the header file disappears at this time.
Next, copy the Jniexport function and fill in the parameter name.
Write the program in the function and call other C + + programs . Note that the compilation environment at this point is automatically C + +.
#include <jni.h> #include "com_example_hellomyjni_jniclient.h" Jniexport jstring jnicall Java_com_example_ Hellomyjni_jniclient_sayname (jnienv *env, Jclass) {return Env->newstringutf ("I ' m spike!");}
5. Modify the main program.
The default is output as "HelloWorld".
In res->layout->activity_main.xml , the ID is added for TextView, which can be called, such as android:id= "@+id/text_view". Other modifications are not made .
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=" com.example.hellomyjni.MainActivity "> <textview Android:id= "@+id/text_view" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "@string/hello_world"/></relativelayout>
in the main program, src->[package]->
Mainactivity.java,
First, the output character is modified , that is, the character is re-specified:
@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); TextView TV = (TextView) Findviewbyid (R.id.text_view); Tv.settext (Jniclient.sayname ());}
Next,
Add a reference to the library, the name of the class that is filled in with the "Add Nativesupport", and the
default is the same as the project name.
static {system.loadlibrary ("Hellomyjni");}
The
rest does not need to be modified.
All:
Package Com.example.hellomyjni;import Android.app.activity;import Android.os.bundle;import android.view.Menu; Import Android.view.menuitem;import Android.widget.textview;public class Mainactivity extends Activity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); TextView TV = (TextView) Findviewbyid (R.id.text_view); Tv.settext (Jniclient.sayname ());} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);} static {System.loadlibrarY ("Hellomyjni");}}
6. Compile.
The project should not have any errors or warnings at this time. Output:
Android-android invoking the Jni method and code