JNI java呼叫c程式碼 (一)靜態註冊
阿新 • • 發佈:2019-01-08
今天說的程式碼是從java層呼叫c程式碼,然後再反調java程式碼的。這裡在java用的是靜態註冊,也就是jni方法名是根據java的檔案路徑生成的,不是動態註冊。
一、java呼叫jni,靜態註冊
先看:java註冊jni,以及呼叫jni函式:
package com.duicky; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * C 呼叫 Java 例子 * * @author luxiaofeng * */ public class MainActivity extends Activity { //也就是你mk配置檔案中的 LOCAL_MODULE := NDK_03 private static final String libSoName = "NDK_04"; private static final String tag = "MainActivity"; public static Context mContext = null; private Button btnClickStatic = null; private Button btnClick = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mContext = this; //初始化控制元件 initViews(); } /** * 初始化控制元件 */ private void initViews() { btnClick = (Button) this.findViewById(R.id.btn_click); btnClick.setOnClickListener(new OnClickListener() { public void onClick(View v) { sayHello(); } }); btnClickStatic = (Button) this.findViewById(R.id.btn_click_static); btnClickStatic.setOnClickListener(new OnClickListener() { public void onClick(View v) { getTime(); } }); } public native void getTime() ; public native void sayHello() ; /** * 載入JNI生成的so庫檔案 */ static { System.loadLibrary(libSoName); } }
二、JNI的Android.mk
然後我們再看JNI的mk函式:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog
LOCAL_MODULE := NDK_04
LOCAL_SRC_FILES := \
CToJava.c \
Provider.c
include $(BUILD_SHARED_LIBRARY)
是動態庫,需要在libs載入libNDK_04.so動態庫三、jni函式
jni函式,當java傳下來,就帶有了JNIEnv參量
#include <string.h> #include <android/log.h> #include <jni.h> #include "Provider.h" JNIEnv* jniEnv; /** * Java 中 宣告的native getTime 方法的實現 */ void Java_com_duicky_MainActivity_getTime(JNIEnv* env, jobject thiz) { if(jniEnv == NULL) { jniEnv = env; } GetTime(); } /** * Java 中 宣告的native sayHello 方法的實現 */ void Java_com_duicky_MainActivity_sayHello(JNIEnv* env, jobject thiz) { if (jniEnv == NULL) { jniEnv = env; } SayHello(); }
下面這個類是c中的方法,以及獲取java類,和方法的初始化
#include "Provider.h"
#include <android/log.h>
extern JNIEnv* jniEnv;
jclass TestProvider;
jobject mTestProvider;
jmethodID getTime;
jmethodID sayHello;
int GetProviderInstance(jclass obj_class);
/**
* 初始化 類、物件、方法
*/
int InitProvider() {
__android_log_print(ANDROID_LOG_INFO, "JNIMsg", "InitProvider Begin 1" );
if(jniEnv == NULL) {
return 0;
}
if(TestProvider == NULL) {
TestProvider = (*jniEnv)->FindClass(jniEnv,"com/duicky/TestProvider");
if(TestProvider == NULL){
return -1;
}
__android_log_print(ANDROID_LOG_INFO, "JNIMsg", "InitProvider Begin 2 ok" );
}
if (mTestProvider == NULL) {
if (GetProviderInstance(TestProvider) != 1) {
(*jniEnv)->DeleteLocalRef(jniEnv, TestProvider);
return -1;
}
__android_log_print(ANDROID_LOG_INFO, "JNIMsg", "InitProvider Begin 3 ok" );
}
if (getTime == NULL) {
getTime = (*jniEnv)->GetStaticMethodID(jniEnv, TestProvider, "getTime","()Ljava/lang/String;");
if (getTime == NULL) {
(*jniEnv)->DeleteLocalRef(jniEnv, TestProvider);
(*jniEnv)->DeleteLocalRef(jniEnv, mTestProvider);
return -2;
}
__android_log_print(ANDROID_LOG_INFO, "JNIMsg", "InitProvider Begin 4 ok" );
}
if (sayHello == NULL) {
sayHello = (*jniEnv)->GetMethodID(jniEnv, TestProvider, "sayHello","(Ljava/lang/String;)V");
if (sayHello == NULL) {
(*jniEnv)->DeleteLocalRef(jniEnv, TestProvider);
(*jniEnv)->DeleteLocalRef(jniEnv, mTestProvider);
(*jniEnv)->DeleteLocalRef(jniEnv, getTime);
return -3;
}
__android_log_print(ANDROID_LOG_INFO, "JNIMsg", "InitProvider Begin 5 ok" );
}
__android_log_print(ANDROID_LOG_INFO, "JNIMsg", "InitProvider Begin 6" );
return 1;
}
int GetProviderInstance(jclass obj_class) {
if(obj_class == NULL) {
return 0;
}
jmethodID construction_id = (*jniEnv)->GetMethodID(jniEnv, obj_class,
"<init>", "()V");
if (construction_id == 0) {
return -1;
}
mTestProvider = (*jniEnv)->NewObject(jniEnv, obj_class,
construction_id);
if (mTestProvider == NULL) {
return -2;
}
return 1;
}
/**
* 獲取時間 ---- 呼叫 Java 方法
*/
void GetTime() {
if(TestProvider == NULL || getTime == NULL) {
int result = InitProvider();
if (result != 1) {
return;
}
}
jstring jstr = NULL;
char* cstr = NULL;
__android_log_print(ANDROID_LOG_INFO, "JNIMsg", "GetTime Begin" );
jstr = (*jniEnv)->CallStaticObjectMethod(jniEnv, TestProvider, getTime);
cstr = (char*) (*jniEnv)->GetStringUTFChars(jniEnv,jstr, 0);
__android_log_print(ANDROID_LOG_INFO, "JNIMsg", "Success Get Time from Java , Value = %s",cstr );
__android_log_print(ANDROID_LOG_INFO, "JNIMsg", "GetTime End" );
(*jniEnv)->ReleaseStringUTFChars(jniEnv, jstr, cstr);
(*jniEnv)->DeleteLocalRef(jniEnv, jstr);
}
/**
* SayHello ---- 呼叫 Java 方法
*/
void SayHello() {
if(TestProvider == NULL || mTestProvider == NULL || sayHello == NULL) {
int result = InitProvider() ;
if(result != 1) {
return;
}
}
jstring jstrMSG = NULL;
jstrMSG =(*jniEnv)->NewStringUTF(jniEnv, "Hi,I'm From C");
__android_log_print(ANDROID_LOG_INFO, "JNIMsg", "SayHello Begin" );
(*jniEnv)->CallVoidMethod(jniEnv, mTestProvider, sayHello,jstrMSG);
__android_log_print(ANDROID_LOG_INFO, "JNIMsg", "SayHello End" );
(*jniEnv)->DeleteLocalRef(jniEnv, jstrMSG);
}
四、java函式
最後再反呼叫java的方法
package com.duicky;
public class TestProvider {
public static String getTime() {
LogUtils.printWithSystemOut( "Call From C Java Static Method" );
LogUtils.toastMessage(MainActivity.mContext, "Call From C Java Static Method" );
return String.valueOf(System.currentTimeMillis());
}
public void sayHello(String msg) {
LogUtils.printWithSystemOut("Call From C Java Not Static Method :" + msg);
LogUtils.toastMessage(MainActivity.mContext, "Call From C Java Not Static Method :" + msg);
}
}