Javaでjniインタフェースでnative codeを呼び出す


               native code    so   ,      Java     JNI          native code  。
       ,               Android java code   native code。
 
1      android project,    Why
 
2    Why     Java ,class  Jni。      JNI   Java ,    Jni.java。  
  package com.yarin.android.Why;
  public class Jni {
         public native int getCInt();
         public native String getCString();
  }
 
3    Why   "src\com\yarin\android\Why"     Jni.java  copy “Why\bin\classes” .
4  Generate Jni.class file via the command below: 
    javac jni.java
    Then copy the Jni.class file generated to cover and replace the original Jni.class file in directory “Why\bin\classes\com\yarin\android\Why”.
     ------The original Jni.class file, you must build the java project first to generate it.
 
5  Generate c style header file
   javah –classpath C:\android-ndk-r6b\myproject\Why\bin\classes  com.yarin.android.Why.Jni
   com.yarin.android.Why is my package name appeared in Jni.java file.
 
com_yarin_android_Why_Jni.h is like below:  
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_yarin_android_Why_Jni */
#ifndef _Included_com_yarin_android_Why_Jni
#define _Included_com_yarin_android_Why_Jni
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_yarin_android_Why_Jni
 * Method:    getCInt
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_com_yarin_android_Why_Jni_getCInt
  (JNIEnv *env, jobject object);   ---you need to supplement the parameter name yourself
/*
 * Class:     com_yarin_android_Why_Jni
 * Method:    getCString
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_yarin_android_Why_Jni_getCString
  (JNIEnv *env, jobject object);
#ifdef __cplusplus
}
#endif
#endif
 

6     Add com_yarin_android_Why_Jni.c file corresponding to the above c style header file, then add implemented code.
#include #include #include "com_yarin_android_Why_Jni.h"
int add() {     int x,y;     x = 111;     y = 22;     x += y;
    return x; }
JNIEXPORT jint JNICALL Java_com_yarin_android_Why_Jni_getCInt   (JNIEnv *env, jobject object) {     return add(); }
JNIEXPORT jstring JNICALL Java_com_yarin_android_Why_Jni_getCString   (JNIEnv *env, jobject object) {     (*env)->NewStringUTF(env, "Why is ok ^_^ ----->> "); }
 
7その後、プロジェクトWhyの下でディレクトリjniを作成し、copy com_を実現する.yarin_android_Why_Jni.c/hはjniディレクトリの下にあります.
 
8「Whyjni」ディレクトリでAndroid.mkを作成し、「android-ndk-r 6 bjni」でApplication.mkを作成し、NDK環境でnative codeをコンパイルし、ダイナミックライブラリlibWhy.soを生成します.これはあなたがすでに前編から知っている.......
 
9 Javaエンジニアリングにnativeダイナミックライブラリを呼び出すコードを追加します.
package com.yarin.android.Why;
import android.app.Activity; import android.os.Bundle; import android.widget.TextView;
public class WhyActivity extends Activity {  static  {   System.loadLibrary("Why");  }    /** Called when the activity is first created. */    @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         Jni jni = new Jni();         TextView view = new TextView(this);         view.setText(jni.getCString() + Integer.toString(jni.getCInt()));         setContentView(view);     } }
 
10  Whyエンジニアリングをコンパイルし、run as Androidアプリケーションを使用すると、native codeの呼び出し結果が表示されます.
Tip:
Usage of the command javah.
javah -d -classpath
where:
'outputdir' is the directory where to put the generated header file
'classpath' contains an absolute path to the directory containing your root package (as mentionned by Glen)
'fully_qualified_class' is the name of the class containing native methods without .class extension
-jni option is not required (set by default)