动态注册native函数出现异常,前提是代码没有问题:

No implementation found for native Lcom/e/he4/JniClazz;.k:()Ljava/lang/String;

resolve解决方案:

经过排查,发现问题如下:

android.mk文件书写问题,当我使用3时,程序崩溃

改成 1,2 都可通过

原因是少了个空格

 android.mk

==============================

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := J
#LOCAL_SRC_FILES := he4.cpp //(1)
SRC_LIST:=$(wildcard $(LOCAL_PATH)/*.cpp)//(2)
#SRC_LIST :=$(wildcard$(LOCAL_PATH)/src/*.cpp) // 错误格式  ${wildcard空格......} // 注意后面有空格//(3)
LOCAL_SRC_FILES += $(SRC_LIST:$(LOCAL_PATH)/%=%)
#LOCAL_SRC_FILES += $(SRC_LIST:$(LOCAL_PATH)/%=%)

include $(BUILD_SHARED_LIBRARY)

===============he4.cpp===============

#include <jni.h>
#include <string.h>
#include <assert.h>
extern "C" jstring key(JNIEnv* env, jobject thiz);
extern "C" jint f(JNIEnv* env, jobject thiz);
extern "C" void g(JNIEnv* env, jobject thiz, jobject context, jint type);
JNINativeMethod mthds[] = { { "k", "()Ljava/lang/String;", (void*) key }, {
"f", "()I", (void*) f }, { "g",
"(Landroid/content/Context;I)V", (void*) g } };
//===========================
void g(JNIEnv* evn, jobject thiz, jobject context, jint type) {

}
//===========================
jint f(JNIEnv* env, jobject thiz) {
return 100;
}
//===========================
jstring key(JNIEnv* env, jobject thiz) {
return env->NewStringUTF("hell sir.............");
//return "";
}
//===========================
jint RegNativeMethod(JNIEnv* env) {
jclass jclz = env->FindClass("com/e/he4/JniClazz");
if (jclz == NULL) {
return JNI_FALSE;
}
if (env->RegisterNatives(jclz, mthds, sizeof(mthds) / sizeof(mthds[0]))
< 0) {
return JNI_FALSE;
}
return JNI_TRUE; // *******************************************************非JNI_OK 

          //  *******************************************************非JNI_OK

          //  *******************************************************非JNI_OK
}

jint JNI_OnLoad(JavaVM* vm, void* reserved) {
jint result = JNI_ERR;
JNIEnv* env = NULL;
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
return result;
}
assert(env != NULL);
if (!RegNativeMethod(env)) {
return result;
}
result = JNI_VERSION_1_4;
return result;
}

 ===============JniClazz==============

package com.e.he4;

import android.content.Context;
import android.content.SharedPreferences;

public class JniClazz {

public static void init(Context context, int type) {
try {
System.loadLibrary("J");
} catch (Exception e) {
e.printStackTrace();
}
String key = k();
SharedPreferences shpf = context.getSharedPreferences(key, 0);
g(context, shpf.getInt(key, -1));
}

public static native String k();

public static native int f();

public static native void g(Context context, int type);
}

 

posted on 2016-01-11 15:09  julyeah  阅读(324)  评论(0编辑  收藏  举报