何时使用JNIEXPORT和JNICALL在Android的NDK? [英] when to use JNIEXPORT and JNICALL in Android NDK?

查看:2320
本文介绍了何时使用JNIEXPORT和JNICALL在Android的NDK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写我自己的JNI来源。纵观一些NDK样,我发现他们经常使用这些宏JNIEXPORT和JNICALL通过Java包像这样

I'm trying to write my own jni sources. Looking at some ndk samples, I found that they often use those macros JNIEXPORT and JNICALL follewed by the name of java package like this

JNIEXPORT无效JNICALL Java_com_example_plasma_PlasmaView_renderPlasma(JNIEnv的* ENV,jobject OBJ,jobject位图,jlong​​ time_ms)

JNIEXPORT void JNICALL Java_com_example_plasma_PlasmaView_renderPlasma(JNIEnv * env, jobject obj, jobject bitmap, jlong time_ms)

我GOOGLE了它,但我不明白何时以及如何使用这些宏

I googled it but I can't understand when and how to use these macros

推荐答案

JNIEXPORT和JNICALL在NDK_ROOT /平台/ Android的9 /弓臂/ usr / include目录/ jni.h定义。根据您的设​​置这条道路会有所不同,但大多是类似的。

JNIEXPORT and JNICALL are defined in NDK_ROOT/platforms/android-9/arch-arm/usr/include/jni.h. Depending on your setup this path will be different, but mostly similar.

#define JNIIMPORT
#define JNIEXPORT  __attribute__ ((visibility ("default")))
#define JNICALL

JNIEXPORT是用来做本地函数出现在编译的二进制(* .so文件)的动态表。它们可以(在这里更多信息 )设置为隐藏或默认。如果这些功能都没有在动态表,JNI将无法找到函数来调用它们这样RegisterNatives调用会在运行时失败。

JNIEXPORT is used to make native functions appear in the dynamic table of the built binary (*.so file). They can be set to "hidden" or "default" (more info here). If these functions are not in the dynamic table, JNI will not be able to find the functions to call them so the RegisterNatives call will fail at runtime.

值得一提的是,所有的功能最终在动态表在默认情况下,所以任何人都可以很容易反编译你的本地code。每个函数调用内置万一JNI需要找到它的二进制文件。这可以通过使用编译器选项修改 -fvisibility 。我建议大家设置为 -fvisibility =隐藏来保持你的code的安全,然后用JNIEXPORT来标记用作具有外部的知名度。

It is worth noting that all functions end up in the dynamic table by default, so anyone could decompile your native code quite easily. Every function call is built into the binary just in case JNI needs to find it. This can be changed using the compiler option -fvisibility. I would recommend everyone sets this to -fvisibility=hidden to keep your code secure, and then use JNIEXPORT to flag functions as having external visibility.

使用带命令只是删除了调试符号,动态表是分开的。有objdump的一出戏,看一个人能得到多少出你的.so文件。

Using the strip command just removes the debug symbols, the dynamic table is separate. Have a play with objdump to see how much a person could get out of your .so files.

我们最近得到绊倒通过这一点,希望这可以帮助别人。

We recently got tripped up by this, hope this helps someone.

编辑:我们使用自定义的构建系统,使能见度选项可能会被默认为其他构建设置进行设置。更多信息可在这太回答。

We use a custom build system, so the visibility option may be set by default for other build setups. More information is available in this SO answer.

这篇关于何时使用JNIEXPORT和JNICALL在Android的NDK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆