ndk,c/c++

注册JNI函数的两种方式

2019-03-02  本文已影响0人  Simplelove_f033

前言

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

注册JNI函数的两种方法

静态方法

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

注册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>

/**

// 指定要注册的类
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;
}

/***

/**

}<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>

/**

// 指定要注册的类
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;
}

/***

/**

}<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开发的相关技术,后续也会将自己学到的内容总结分享出来。

上一篇 下一篇

猜你喜欢

热点阅读