美文网首页ndk,c/c++
注册JNI函数的两种方式

注册JNI函数的两种方式

作者: Simplelove_f033 | 来源:发表于2019-03-02 15:28 被阅读0次

    前言

    前面介绍过如何实现在Android Studio中制作我们自己的so库,相信大家看过之后基本清楚如何在Android studio创建JNI函数并最终编译成不同cpu架构的so库,但那篇文章介绍注册JNI函数的方法(静态方法)存在一些弊端,本篇将介绍另外一种方法(动态注册)来克服这些弊端。

    注册JNI函数的两种方法

    静态方法

    这种方法我们比较常见,但比较麻烦,大致流程如下:

    • 先创建Java类,声明Native方法,编译成.class文件。
    • 使用Javah命令生成C/C++的头文件,例如:javah -jni com.devilwwj.jnidemo.TestJNI,则会生成一个以.h为后缀的文件com_devilwwj_jnidemo_TestJNI.h
    • 创建.h对应的源文件,然后实现对应的native方法,如下图所示:
    注册JNI函数的两种方式 - wangchaocheng2 - wangchaocheng2的博客

    说一下这种方法的弊端:

    • 需要编译所有声明了native函数的Java类,每个所生成的class文件都得用javah命令生成一个头文件。
    • javah生成的JNI层函数名特别长,书写起来很不方便
    • 初次调用native函数时要根据函数名字搜索对应的JNI层函数来建立关联关系,这样会影响运行效率
      摘自:深入理解Android卷I

    既然有这么多弊端,我们自然要考虑一下有没有其他更好的方法下一节就是我要讲的替代方法,Android用的也是这种方法。

    动态注册

    我们知道Java Native函数和JNI函数时一一对应的,JNI中就有一个叫JNINativeMethod的结构体来保存这个对应关系,实现动态注册方就需要用到这个结构体。举个例子,你就一下子明白了:

    声明native方法还是一样的:

    注册JNI函数的两种方式 - wangchaocheng2 - wangchaocheng2的博客

    创建jni目录,然后在该目录创建hello.c文件,如下:

    <pre style="line-height: 28px; white-space: pre-wrap; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><co style="line-height: 28px;"><wbr style="line-height: 28px;">de>//
    // Created by DevilWwj on 16/8/28.
    //
    include <stdlib.h>
    include <string.h>
    include <stdio.h>
    include <jni.h>
    include <assert.h>

    /**

    • 定义native方法
      */
      JNIEXPORT jstring JNICALL native_hello(JNIEnv env, jclass clazz)
      {
      printf("hello in c native co<wbr style="line-height: 28px;">de./n");
      return (
      env)->NewStringUTF(env, "hello world returned.");
      }

    // 指定要注册的类
    define JNIREG_CLASS "com/devilwwj/library/JavaHello"

    // 定义一个JNINativeMethod数组,其中的成员就是Java代码中对应的native方法
    static JNINativeMethod gMethods[] = {
    { "hello", "()Ljava/lang/String;", (void*)native_hello},
    };

    static int registerNativeMethods(JNIEnv* env, const char* className,
    JNINativeMethod* gMethods, int numMethods) {
    jclass clazz;
    clazz = (env)->FindClass(env, className);
    if (clazz == NULL) {
    return JNI_FALSE;
    }
    if ((
    env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) {
    return JNI_FALSE;
    }
    return JNI_TRUE;
    }

    /***

    • 注册native方法
      /
      static int registerNatives(JNIEnv
      env) {
      if (!registerNativeMethods(env, JNIREG_CLASS, gMethods, sizeof(gMethods) / sizeof(gMethods[0]))) {
      return JNI_FALSE;
      }
      return JNI_TRUE;
      }

    /**

    • 如果要实现动态注册,这个方法一定要实现

    • 动态注册工作在这里进行
      /
      JNIEXPORT jint JNICALL JNI_On<wbr style="line-height: 28px;">Load(JavaVM
      vm, void* reserved) {
      JNIEnv* env = NULL;
      jint result = -1;

      if ((vm)-> GetEnv(vm, (void*) &env, JNI_VERSION_1_4) != JNI_OK) {
      return -1;
      }
      assert(env != NULL);

      if (!registerNatives(env)) { //注册
      return -1;
      }
      result = JNI_VERSION_1_4;

      return result;

    }<wbr style="line-height: 28px;">de></co></pre>

    创建jni目录,然后在该目录创建hello.c文件,如下:

    <pre style="line-height: 28px; white-space: pre-wrap; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><co style="line-height: 28px;"><wbr style="line-height: 28px;">de>//
    // Created by DevilWwj on 16/8/28.
    //
    include <stdlib.h>
    include <string.h>
    include <stdio.h>
    include <jni.h>
    include <assert.h>

    /**

    • 定义native方法
      */
      JNIEXPORT jstring JNICALL native_hello(JNIEnv env, jclass clazz)
      {
      printf("hello in c native co<wbr style="line-height: 28px;">de./n");
      return (
      env)->NewStringUTF(env, "hello world returned.");
      }

    // 指定要注册的类
    define JNIREG_CLASS "com/devilwwj/library/JavaHello"

    // 定义一个JNINativeMethod数组,其中的成员就是Java代码中对应的native方法
    static JNINativeMethod gMethods[] = {
    { "hello", "()Ljava/lang/String;", (void*)native_hello},
    };

    static int registerNativeMethods(JNIEnv* env, const char* className,
    JNINativeMethod* gMethods, int numMethods) {
    jclass clazz;
    clazz = (env)->FindClass(env, className);
    if (clazz == NULL) {
    return JNI_FALSE;
    }
    if ((
    env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) {
    return JNI_FALSE;
    }
    return JNI_TRUE;
    }

    /***

    • 注册native方法
      /
      static int registerNatives(JNIEnv
      env) {
      if (!registerNativeMethods(env, JNIREG_CLASS, gMethods, sizeof(gMethods) / sizeof(gMethods[0]))) {
      return JNI_FALSE;
      }
      return JNI_TRUE;
      }

    /**

    • 如果要实现动态注册,这个方法一定要实现

    • 动态注册工作在这里进行
      /
      JNIEXPORT jint JNICALL JNI_On<wbr style="line-height: 28px;">Load(JavaVM
      vm, void* reserved) {
      JNIEnv* env = NULL;
      jint result = -1;

      if ((vm)-> GetEnv(vm, (void*) &env, JNI_VERSION_1_4) != JNI_OK) {
      return -1;
      }
      assert(env != NULL);

      if (!registerNatives(env)) { //注册
      return -1;
      }
      result = JNI_VERSION_1_4;

      return result;

    }<wbr style="line-height: 28px;">de></co></pre>

    先仔细看一下上面的代码,看起来好像多了一些代码,稍微解释下,如果要实现动态注册就必须实现JNI_On<wbr style="line-height: 28px;">Load方法,这个是JNI的一个入口函数,我们在Java层通过System.loadLibrary加载完动态库后,紧接着就会去查找一个叫JNI_On<wbr style="line-height: 28px;">Load的方法。如果有,就会调用它,而动态注册的工作就是在这里完成的。在这里我们会去拿到JNI中一个很重要的结构体JNIEnv,env指向的就是这个结构体,通过env指针可以找到指定类名的类,并且调用JNIEnv的RegisterNatives方法来完成注册native方法和JNI函数的对应关系。

    我们在上面看到声明了一个JNINativeMethod数组,这个数组就是用来定义我们在Java代码中声明的native方法,我们可以在jni.h文件中查看这个结构体的声明:

    注册JNI函数的两种方式 - wangchaocheng2 - wangchaocheng2的博客

    结构体成员变量分别对应的是Java中的native方法的名字,如本文的hello;Java函数的签名信息、JNI层对应函数的函数指针。

    以上就是动态注册JNI函数的方法,上面只是一个简单的例子,如果你还想再实现一个native方法,只需要在JNINativeMethod数组中添加一个元素,然后实现对应的JNI层函数即可,下次我们加载动态库时就会动态的将你声明的方法注册到JNI环境中,而不需要你做其他任何操作。

    总结

    关于JNI技术,在Android中使用是非常多的,我们在实际开发中或多或少可能会使用到第三方或者需要自己开发相应的so库,所以学习和理解JNI中的一些实现原理还是很有必要的,从以前在Eclipse来实现so库开发到现在可以通过Android Studio来开发so库,会发现会方便很多,这个也是技术的发展带来的一些便捷。笔者也计划学习NDK开发的相关技术,后续也会将自己学到的内容总结分享出来。

    相关文章

      网友评论

        本文标题:注册JNI函数的两种方式

        本文链接:https://www.haomeiwen.com/subject/aymsuqtx.html