Android NDK 中的多个(我的和第 3 方)本机库

时间:2023-03-17
本文介绍了Android NDK 中的多个(我的和第 3 方)本机库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用两个本地库:一个是我自己的,另一个是第 3 方的.只要我在单独的项目中使用它们,一切都很好.但现在我得到了 Exception Ljava/lang/UnsatisfiedLinkError.

I have to use two native libraries: one is my own and the other one is 3rd-party. As long as I used them in separate projects, everything was ok. But now I'm getting the Exception Ljava/lang/UnsatisfiedLinkError.

我正在使用 Eclipse.

I'm using Eclipse.

我发现如果我将现有库放在 libs/armeabi 中,Eclipse 会开始编译本机代码并且它会失败.如果我从命令行重建 JNI 部分,编译成功但第 3 方库消失.真傻.

I found out that if I place the existing library in libs/armeabi, Eclipse begins compilation of the native code and it fails. If I rebuild the JNI part from the command line, compilation succeeds but the 3rd party library disappears. Really stupid.

那么我如何告诉 Eclipse 使用现有的 .so 库以及必须构建的库?库是独立的.

So how do I tell Eclipse to use an existing .so library along with a library that must be built? The libraries are independent.

推荐答案

NDK 允许使用 PREBUILT_SHARED_LIBRARY 变量链接到预构建的用户库.

The NDK allows for linking with prebuilt user libraries, using the PREBUILT_SHARED_LIBRARY variable.

假设你需要链接的库是librandom.so,在项目文件夹的jni子文件夹中创建一个libs文件夹:

Assuming that the library you need to link is librandom.so, create a libs folder in jni subfolder of the project folder:

mkdir -p jni/libs
cp librandom.so jni/libs

然后,只需创建一个 jni/libs/Android.mk 文件:

Then, just create a jni/libs/Android.mk file:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := random
LOCAL_SRC_FILES := librandom.so
include $(PREBUILT_SHARED_LIBRARY)

您可以为每个预构建库创建一个部分,全部放在 jni/libs 中.

You can create a section for each prebuilt library, all placed in jni/libs.

接下来,您只需将上述文件包含到您的 jni/Android.mk 中即可使一切正常工作.在 NDK 文档中,建议在 Android.mk 的末尾完成此操作,而不是中间:

Next, you just need to include the above file into your jni/Android.mk to get things to work. In the NDK docs, it is recommended that this be done at the end of the Android.mk, rather than the middle:

include $(LOCAL_PATH)/libs/Android.mk

但是,您需要在需要此库的模块之前执行此操作.

However, you'll need to do this before the module that requires this library.

对于链接,您需要将以下内容添加到链接到预构建库的模块部分中.

For linking, you'll need to add the following into the module section that links to the prebuilt library.

LOCAL_SHARED_LIBRARIES := random

然后当你做 ndk-build 时,它会在构建模块之前将此库复制到 libs/armeabi/ 中,然后你就可以开始了.

Then when you do ndk-build, it will copy this library into libs/armeabi/ before building the module, and you're good to go.

注意:这并不能解决所需标题的问题.您仍然需要将库标头的位置添加到需要它的模块中的变量 LOCAL_C_INCLUDES 中.

Note: This does not solve problems with required headers. You'll still need to add the location of the headers for the library into the variable LOCAL_C_INCLUDES in the module that requires it.

这篇关于Android NDK 中的多个(我的和第 3 方)本机库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何在安卓上运行 USSD 命令? 下一篇:Linphone Android 如何导入库项目?

相关文章

最新文章