android jni注意


在JNI的c文件中如果用到了#include <utils/Log.h> 

然后用NDK 编译, ndk-build clean && ndk-build 提示error: utils/Log.h: No such file or directory

如果只是用到LOG功能

1 修改Android.mk文件配置,添加如下语句

LOCAL_LDLIBS += -llog -lm

2  在.c文件中修改为如下语句

#pragma once
#include<android/log.h>
#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##args)
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args)
#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##args)


(3)如果在框架中加入jni

static JNINativeMethod methods[] = {
{ "getWorkgroups", "()Ljava/lang/String;", (void*)Java_com_explorer_jni_SambaTreeNative_getWorkgroups},
{"getDetailsBy", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", (void*)Java_com_explorer_jni_SambaTreeNative_getDetailsBy},
};


static const char *classPathName = "com/explorer/jni/SambaTreeNative";
对于的文件路径frameworks\base\core\java\com\explorer\jni\SambaTreeNative.java

static int registerNativeMethods(JNIEnv* env, const char* className,
    JNINativeMethod* gMethods, int numMethods)
{
    jclass clazz;


    clazz = env->FindClass(className);
    if (clazz == NULL) {
        LOGE("Native registration unable to find class '%s'", className);
        return JNI_FALSE;
    }
    if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
        LOGE("RegisterNatives failed for '%s'", className);
        return JNI_FALSE;
    }


    return JNI_TRUE;
}


static int registerNatives(JNIEnv* env)
{
  if (!registerNativeMethods(env, classPathName,
                 methods, sizeof(methods) / sizeof(methods[0]))) {
    return JNI_FALSE;
  }


  return JNI_TRUE;
}


 
typedef union {
    JNIEnv* env;
    void* venv;
} UnionJNIEnvToVoid;


jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
    UnionJNIEnvToVoid uenv;
    uenv.venv = NULL;
    jint result = -1;
    JNIEnv* env = NULL;
    
    LOGI("JNI_OnLoad smbtree");


    if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) {
        LOGE("ERROR: GetEnv failed");
        goto bail;
    }
    env = uenv.env;


    if (registerNatives(env) != JNI_TRUE) {
        LOGE("ERROR: registerNatives failed");
        goto bail;
    }
    
    result = JNI_VERSION_1_4;
    
bail:
    return result;
}


你可能感兴趣的:(android jni注意)