Android JNI Demo

Posted 小米渣的逆袭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android JNI Demo相关的知识,希望对你有一定的参考价值。

 注意c++和c语法不一样

c语法(*env)->GetByteArrayElements(env, intArry, 0);

#include <jni.h>
#include <string>
#include "test.h"
#include <android/log.h>

JavaVM *global_jvm = NULL;
jclass uulogger_clazz = NULL;
jint JNI_OnLoad(JavaVM* vm, void* reserved) 
    JNIEnv *env = NULL;
    jint result = -1;

    // 判断是否正确
    if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) 
        return result;
    
    global_jvm = vm;
    uulogger_clazz = env->FindClass("com/example/wjrso/UULogger");
    if(uulogger_clazz != NULL)
    
        //设置全局引用,防止多线程崩溃
        jobject obj = uulogger_clazz;
        obj = (jobject)env->NewGlobalRef(obj);
        uulogger_clazz = (jclass)obj;
    
    return JNI_VERSION_1_6;


void logFunction(const char* data)

    //每次线程环境变量不一样,重新查找
    if(global_jvm == NULL)
    
        return;
    

    JNIEnv* env = NULL;
    global_jvm->AttachCurrentThread(&env, NULL);
    if (env == NULL)
    
        return;
    
    //jclass loggerClass = (*env)->FindClass(env,"com/example/wjrso/UULogger");
    if (uulogger_clazz == NULL)
    
        return;
    
    jmethodID  logMethodID = env->GetStaticMethodID(uulogger_clazz,"log","(Ljava/lang/String;)V");
    if (logMethodID == NULL)
    
        return;
    
    jstring string = env->NewStringUTF(data);
    env->CallStaticVoidMethod(uulogger_clazz,logMethodID,string);
    //(*global_jvm)->DetachCurrentThread(global_jvm);
    env->DeleteLocalRef(string);


extern "C" JNIEXPORT jstring JNICALL
Java_com_example_wjrso_WjrSo_stringFromJNI(
        JNIEnv* env,
        jobject /* this */,
        jstring config) 
    const char* input;
    jboolean isCopy = false;
    input = env->GetStringUTFChars(config, &isCopy);
    std::string hello = getString();
    if (input != NULL)
    
        hello += input;
        //释放资源
        env->ReleaseStringUTFChars(config, input);
    

    logFunction("输出日志");
    return env->NewStringUTF(hello.c_str());


extern "C" JNIEXPORT void JNICALL
Java_com_example_wjrso_WjrSo_intArray(
        JNIEnv* env,
        jobject /* this */,
        jintArray intArry) 

    jboolean isCopy = false;
    jint* intAry = env->GetIntArrayElements(intArry,&isCopy);
    int count = env->GetArrayLength(intArry);
    for (int i = 0; i < count; ++i) 
        char buf[100] = 0;
        int n = intAry[i];
        sprintf(buf,"我是数字:%d",n);
        logFunction(buf);
    

    env->ReleaseIntArrayElements(intArry,intAry,0);

    /* 部分C写法
     *  (*env)->GetByteArrayElements(env, intArry, 0);
     *  (*env)->ReleaseIntArrayElements(env,intArry,intAry,0);
     *  cls = (*env)->GetObjectClass(env, value);
     *  method = (*env)->GetMethodID(env, cls, "booleanValue", "()Z");
     *  if (method != NULL) 
     *       audit = (*env)->CallBooleanMethod(env, value, method);
     *  
     */   

以上是关于Android JNI Demo的主要内容,如果未能解决你的问题,请参考以下文章

Android JNI Demo

Android JNI Demo

Android JNI Demo

Android JNI Demo

靠谱Android Studio JNI Demo

Android的JNI调用