Android-Android calls JNI method and code
Android calls JNI method and code
JNI: Java Native Interface to implement intercommunication between Java and C/C ++.
InAndroidUse onJNIMethod.Time: 2014.9.3
Environment: RequiredUseStandard Eclipse,Install the Android EnvironmentTo useNDT 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 an interface class:
CreateJNI Interface Class, IncludingStatic Method. Location:Project-> src-> [package]-> JniClient. java
/** * */package com.example.hellomyjni;/** * @author Administrator * */public class JniClient {static public native String sayName();}
2. Compile the interface class:
Go to the project folder and generateHeader file of JNI, Run the following command:
Javah-classpath bin/classes-d jni com. example. hellomyjni. JniClient
Command Parsing:
JavahGenerate header files;
-ClasspathThe class position (bin/classes) is a. class file;
-D jniThe JNI class to be generated (Com. example. hellomyjni. JniClient), Including [package]. [classname].
PressF5 refreshProject, project meetingAutomatic jni folder generationAnd containsHeader file com_example_hellomyjni_JniClient.h.
/* DO NOT EDIT THIS FILE - it is machine generated */#include
/* Header for class com_example_hellomyjni_JniClient */#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 the Android project.
Right-clickProject inAndroid ToolsClickAdd NativeSupport.Automatic Generation:HelloMyJni. cpp and Android. mk.
Android. mkNoModify:
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 function
ModifyHelloMyJni. cpp.
First,Add header fileThat is, the header file generated by JNI,# Include com_example_hellomyjni_JniClient.hIn this caseError disappears.
Second,Copy JNIEXPORT Function, AndEnter ParametersName.
Write the program in the function, andCall other C ++ programs. Note thatThe compiling environment is automatically C ++.
#include
#include com_example_hellomyjni_JniClient.hJNIEXPORT jstring JNICALL Java_com_example_hellomyjni_JniClient_sayName (JNIEnv *env, jclass) {return env->NewStringUTF(I'm Spike!);}
5. Modify the main program.
DefaultIs outputHelloWorld.
Go to res-> layout->Activity_main.xml, IsTextView add idCan be called, such as android: id = @ + id/text_view.Others are not modified.
In the main program, src-> [package]->
MainActivity. java,
FirstModify output characters, That is, re-specify the character:
@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());}
Second,
Add database referenceThe referenced name is the class name entered when adding NativeSupport,
DefaultSame as the project name.
static {System.loadLibrary(HelloMyJni);}
The rest do 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 present.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 AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}static {System.loadLibrary(HelloMyJni);}}
6. compile.
In this case, the project shouldNo errors or warnings. Output: