Android JNI 提供了很強大的支持,不僅可以采用基本類型做為參數和返回值,同時也支持自定義對象做為參數和返回值,以下舉例說明。
一、定義作為輸入和返回的自定義類 (僅提供兩個簡單類型和一個打印函數)
- package com.example.jniexample;
-
- import android.util.Log;
-
- public class JNIParam {
- public int mInt;
- public String mString;
-
- JNIParam(){
- mInt = 0;
- mString = "0";
- }
-
- public void print(String tag){
- Log.d(tag, String.format("print: mInt=%d, mString=%s", mInt, mString));
- }
- }
二. 定義和JNI通信的java封裝
功能:測試輸入參數和返回值都為自定義對象的本地方法,同時提供必要的調試打印
- package com.example.jniexample;
-
- public class JNIParamTest {
- private final String TAG = "JNIParamTest";
-
- static {
- System.loadLibrary("JNIParamTest");
- };
-
- public void runParamTest(){
- JNIParam paramIn = new JNIParam();
- paramIn.print(TAG);
-
- JNIParam paramOut = doTest(paramIn);
- if( paramOut != null ) paramOut.print(TAG);
- }
-
- //JNI
- private native JNIParam doTest(JNIParam paramIn);
- }
三、實現JNI的本地實現
a. 頭文件 (JNIParamTest.h)
- /* DO NOT EDIT THIS FILE - it is machine generated */
- #include <jni.h>
- /* Header for class com_example_jniexample_JNIParamTest */
-
- #ifndef _Included_com_example_jniexample_JNIParamTest
- #define _Included_com_example_jniexample_JNIParamTest
- #ifdef __cplusplus
- extern "C" {
- #endif
- /*
- * Class: com_example_jniexample_JNIParamTest
- * Method: doTest
- * Signature: (Lcom/example/jniexample/JNIParam;)Lcom/example/jniexample/JNIParam;
- */
- JNIEXPORT jobject JNICALL Java_com_example_jniexample_JNIParamTest_doTest
- (JNIEnv *, jobject, jobject);
-
- #ifdef __cplusplus
- }
- #endif
- #endif
b. c文件 (JNIParamTest.c)
- #include "JNIParamTest.h"
-
- #include "android/log.h"
- #define TAG "JNI_ParamTest"
- #define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args)
-
- /*
- * Class: com_example_jniexample_JNIParamTest
- * Method: doTest
- * Signature: (Lcom/example/jniexample/JNIParam;)Lcom/example/jniexample/JNIParam;
- */
- JNIEXPORT jobject JNICALL Java_com_example_jniexample_JNIParamTest_doTest
- (JNIEnv *env, jobject thiz, jobject paramIn)
- {
- jclass paramInClass = (*env)->GetObjectClass(env, paramIn);
- if( paramInClass){
- jboolean iscopy;
- jfieldID intId = (*env)->GetFieldID(env, paramInClass, "mInt", "I");
- jint num = (int)(*env)->GetIntField(env, paramIn, intId);
- LOGD("num = %d", num);
-
- jfieldID strId = (*env)->GetFieldID(env, paramInClass, "mString", "Ljava/lang/String;");
- jstring str = (jstring)(*env)->GetObjectField(env, paramIn, strId);
- const char *locstr = (*env)->GetStringUTFChars(env, str, &iscopy);
- LOGD("str = %s", locstr);
-
- (*env)->ReleaseStringUTFChars(env, str, locstr);
- }
-
- jclass cls = (*env)->FindClass(env, "com/example/jniexample/JNIParam");
- jmethodID id = (*env)->GetMethodID(env, cls, "<init>", "()V");
-
- jobject paramOut = (*env)->NewObjectA(env, cls, id, 0);
-
- jfieldID intId = (*env)->GetFieldID(env, cls, "mInt", "I");
- (*env)->SetIntField(env, paramOut, intId, 1);
-
- jfieldID strId = (*env)->GetFieldID(env, cls, "mString", "Ljava/lang/String;");
- (*env)->SetObjectField(env, paramOut, strId, (jstring)(*env)->NewStringUTF(env, "1"));
-
- return paramOut;
- }
四、提供一個測試環境
- package com.example.jniexample;
-
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
-
- public class JNIExample extends Activity {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_jniexample);
-
- JNIParamTest test = new JNIParamTest();
- test.runParamTest();
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_jniexample, menu);
- return true;
- }
- }
實現了一個Activity, 啟動就調用
五、測試結果: (logcat 查看)
- D/JNIParamTest( 6135): print: mInt=0, mString=0
- D/JNI_ParamTest( 6135): num = 0
- D/JNI_ParamTest( 6135): str = 0
- D/JNIParamTest( 6135): print: mInt=1, mString=1
六、其他類型轉換可以參考: http://www.linuxidc.com/Linux/2012-11/74906.htm