zoukankan      html  css  js  c++  java
  • Android ndk另一种注册方式

    不使用Java_com_xxx方式调用ndk的方式,这种方法的优点是灵活,可配置,不必限制在Java_com_xxx命名依赖中。

    步骤如下:

    第1步,在JNI初始化方法中调用自定义注册方法,并判断成功与否通知Java端。

    jint JNI_OnLoad(JavaVM* vm, void* reserved) {
         JNIEnv* env = NULL;
         sVm = vm;
    
        if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) == JNI_OK) 
            return JNI_ERR;
        if (注册方法(env) == JNI_OK)
            return JNI_VERSION_1_4;
    
        return JNI_ERR;
    }

    第2步,在cpp或c文件(位置任意)中声明JNINativeMethod 方法数组

    static JNINativeMethod methods[] = {
        "nativeInit", "()V", (void*) xxx_init, //这样就可以映射到java的nativeInit方法了
        "", "", (void *) xxx
    };

    其中 JNINativeMethod  的定义在 jni.h 中,结构如下:

    typedef struct {
           const char * name;
           const char * signature;
           void * fnptr;
    }JNINativeMethod;

    第3步,最后通过方法注入到java的jni接口中

    jint custom_register_all_jni_methods(JNIEnv *env) {
        return jniRegisterNativeMethods(env, "com/xxx/xxx", methods, sizeof(methods) / sizeof(methods[0]));
    }

    com/xxx/xxx为所对应的java接口类名


    万里长城十亿兵, 
    国耻岂待儿孙平. 
    愿提十万虎狼族,
    越马扬刀入东京!
  • 相关阅读:
    Lucky Substrings
    KMP
    圆桌问题(hdu4841)
    codeforces 624C Graph and String
    Joseph(hdu1443)
    The Longest Straight(FZUoj2216)
    C1. 组队活动 Small(BNUOJ)
    A1. 道路修建 Small(BNUOJ)
    Problem 2221 RunningMan(fuzoj)
    CODEFORCEs 621E. Wet Shark and Blocks
  • 原文地址:https://www.cnblogs.com/dellinger/p/3456348.html
Copyright © 2011-2022 走看看