JNI分静态注册和动态注册,这里测试动态注册
库文件的生成
Android.mk文件
include $(BUILD_EXECUTABLE)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= jni_lib.cpp
LOCAL_C_INCLUDES := $(JNI_H_INCLUDE)
LOCAL_SHARED_LIBRARIES := libutils libandroid_runtime libnativehelper
LOCAL_MODULE := libtestjni
include $(BUILD_SHARED_LIBRARY)
jni_lib.cpp
#include <jni.h>
#include "JNIHelp.h"
#define LOG_TAG "TestLib"
#include <utils/Log.h>
#include "android_runtime/AndroidRuntime.h"
#define INTERNALAPI_PKG_NAME "fgt/org/SudokuActivity"
using namespace android;
JNIEXPORT void JNICALL printHello(JNIEnv * env, jobject jobj) ;
static JNINativeMethod gMethods[] = {
{"myprint",
"()V",
(void*)printHello
}
};
jint JNI_OnLoad(JavaVM*vm,void*reserved)
{
JNIEnv* env = NULL;
jint result = -1;
LOGW("TestInteralApi JNI loaded");
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
LOGW("GetEnv failed");
goto bail;
}
assert(env != NULL);
// AndroidRuntime::registerNativeMethods(env, INTERNALAPI_PKG_NAME, gMethods, NELEM(gMethods));
if (AndroidRuntime::registerNativeMethods(env, INTERNALAPI_PKG_NAME, gMethods, NELEM(gMethods))< 0) {
LOGW("TestInternalApi native registration failed");
goto bail;
}
result = JNI_VERSION_1_4;
bail:
return result;
}
JNIEXPORT void JNICALL printHello(JNIEnv * env, jobject jobj)
{
LOGW("Hello LIB!/n");
}
package fgt.org;
public class SudokuActivity extends Activity implements OnClickListener{
static {
try {
System.loadLibrary("testjni");
} catch (UnsatisfiedLinkError e) {
Log.d("itest_jni", "itest jni library not found!");
}
}
public native static void myprint();