Android NDK/JNI:构建依赖于其他共享库的共享库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21922736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android NDK/JNI: Building a shared library that depends on other shared libraries
提问by djc6535
I am writing an android app that wants to make JNI calls into a shared library built in using the NDK. The trick is this shared library calls functions provided by OTHER shared libraries. The other shared libraries are C libraries that have been compiled elsewhere.
我正在编写一个 android 应用程序,希望将 JNI 调用调用到使用 NDK 内置的共享库中。诀窍是这个共享库调用其他共享库提供的函数。其他共享库是在别处编译的 C 库。
Here's what I've tried:
这是我尝试过的:
My Environment:I'm working in Eclipse. I've added native support and have a jni library. In that library I have my code and a \lib directory where I have copied my other .so files.
我的环境:我在 Eclipse 中工作。我添加了本机支持并有一个 jni 库。在那个库中,我有我的代码和一个 \lib 目录,我在其中复制了我的其他 .so 文件。
Attempt #1 Android.mk: Just telling it where the libs are
尝试 #1 Android.mk:只是告诉它库在哪里
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := native_lib
LOCAL_SRC_FILES := native_lib.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog
LOCAL_LDLIBS += -L$(LOCAL_PATH)/lib/support_lib1
LOCAL_LDLIBS += -L$(LOCAL_PATH)/lib/support_lib2
include $(BUILD_SHARED_LIBRARY)
This builds just fine, but when I try to run I get errors indicating that dlopen(libnative_lib) failed because it couldn't load libsupport_lib1.
这构建得很好,但是当我尝试运行时,我收到错误指示 dlopen(libnative_lib) 失败,因为它无法加载 libsupport_lib1。
Coming here I found this:
来到这里,我发现了这个:
Can shared library call another shared library?
which said that I needed to call load library on all necessary libraries. Great!
这表示我需要在所有必要的库上调用加载库。伟大的!
Attempt #2 Opening each library first
尝试 #2 首先打开每个库
static {
System.loadLibrary("support_lib1");
System.loadLibrary("support_lib2");
System.loadLibrary("native_lib");
}
Again, this builds just fine, however when I run I get a new error:
同样,这构建得很好,但是当我运行时,我收到一个新错误:
couldn't load libsupport_lib1. findLibrary returned null.
无法加载 libsupport_lib1。findLibrary 返回空值。
Now we're getting somewhere. It must not be loading the libraries over to the target.
现在我们正在到达某个地方。它不能将库加载到目标。
Attempt #3 Copying .so files into project/libs/armeabi
尝试 #3 将 .so 文件复制到 project/libs/armeabi
Didn't work. When Eclipse builds it deleted the files I dropped in there.
没用。当 Eclipse 构建时,它删除了我放在那里的文件。
Attempt #4 Creating a new module for each library
尝试 #4 为每个库创建一个新模块
So then I found this:
然后我发现了这个:
Android NDK: Link using a pre-compiled static library
It's about static libraries, but maybe I am having a similar problem. The gist is that I need to declare a module for each library. So my new Android.mk looks like this:
这是关于静态库,但也许我有类似的问题。要点是我需要为每个库声明一个模块。所以我的新 Android.mk 看起来像这样:
LOCAL_PATH := $(call my-dir)
#get support_lib1
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib1
LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/support_lib1.so
include $(BUILD_SHARED_LIBRARY)
#get support_lib2
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib2
LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/support_lib2.so
include $(BUILD_SHARED_LIBRARY)
#build native lib
include $(CLEAR_VARS)
LOCAL_MODULE := native_lib
LOCAL_SRC_FILES := native_lib.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog
LOCAL_LDLIBS += -L$(LOCAL_PATH)/lib/support_lib1
LOCAL_LDLIBS += -L$(LOCAL_PATH)/lib/support_lib2
include $(BUILD_SHARED_LIBRARY)
This builds! Even better, armeabi has the sos now! Even BETTERI get the following messages when I try to run it (telling me that support_lib1 and 2 were opened by LoadLibrary:
这建立!更棒的是,armeabi 现在有了 sos!甚至更好,我得到以下消息时,我尝试运行它(告诉我,support_lib1和2被调用LoadLibrary打开:
Trying to load lib /data/app-lib/com.example.tst/libsupport_lib1.so added shared lib /data/app-lib/com.example.tst/libsupport_lib1.so no JNI_OnLoad found in /data/app-lib/com.example.tst/libsupport_lib1.so, skipping init
尝试加载 lib /data/app-lib/com.example.tst/libsupport_lib1.so 添加共享库 /data/app-lib/com.example.tst/libsupport_lib1.so 在 /data/app-lib/ 中找不到 JNI_OnLoad com.example.tst/libsupport_lib1.so,跳过初始化
but then... dlopen failed: Could not locate symbol func_that_exists_in_libsupport_lib.so referenced by libnative_lib.so
但随后... dlopen 失败:无法找到 libnative_lib.so 引用的符号 func_that_exists_in_libsupport_lib.so
Edit: Attempt 5: Use PREBUILT_SHARED_LIBRARY
编辑:尝试 5:使用 PREBUILT_SHARED_LIBRARY
So I found this: How can i Link prebuilt shared Library to Android NDK project?
所以我发现了这个: 如何将预构建的共享库链接到 Android NDK 项目?
which seems to be exactly what I'm asking. Their answer seems to be 'don't use 'build_shared_library' but instead 'use PREBUILT_SHARED_LIBRARY
这似乎正是我要问的。他们的回答似乎是'不要使用'build_shared_library'而是'使用PREBUILT_SHARED_LIBRARY
Okay, let's try.
好的,让我们试试。
LOCAL_PATH := $(call my-dir)
#get support_lib1
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib1
LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/support_lib1.so
include $(PREBUILT_SHARED_LIBRARY)
#get support_lib2
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib2
LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/support_lib2.so
include $(PREBUILT_SHARED_LIBRARY)
#build native lib
include $(CLEAR_VARS)
LOCAL_MODULE := native_lib
LOCAL_SRC_FILES := native_lib.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog
LOCAL_SHARED_LIBRARIES := support_lib1 support_lib2
include $(BUILD_SHARED_LIBRARY)
Build... fails! The build complains about missing symbols now.
构建……失败!构建现在抱怨缺少符号。
Edit: Attempt 6: Flatten everything
编辑:尝试 6:压平一切
So I went back to the prebuilts documentation in the NDK. It says:
所以我回到了 NDK 中的预构建文档。它说:
Each prebuilt library must be declared as a single independent module to the build system. Here is a trivial example where we assume that the file "libfoo.so" is located in the same directory than the Android.mk below:
每个预构建库必须声明为构建系统的单个独立模块。这是一个简单的示例,我们假设文件“libfoo.so”与下面的 Android.mk 位于同一目录中:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := foo-prebuilt
LOCAL_SRC_FILES := libfoo.so
include $(PREBUILT_SHARED_LIBRARY)
Notice that, to declare such a module, you really only need the following:
请注意,要声明这样的模块,您实际上只需要以下内容:
Give the module a name (here 'foo-prebuilt'). This does not need to correspond to the name of the prebuilt library itself.
为模块命名(此处为“foo-prebuilt”)。这不需要与预构建库本身的名称相对应。
Assign to LOCAL_SRC_FILES the path to the prebuilt library you are providing. As usual, the path is relative to your LOCAL_PATH.
将您提供的预构建库的路径分配给 LOCAL_SRC_FILES。像往常一样,路径是相对于您的 LOCAL_PATH 的。
Include PREBUILT_SHARED_LIBRARY, instead of BUILD_SHARED_LIBRARY, if you are providing a shared, library. For static ones, use PREBUILT_STATIC_LIBRARY. A prebuilt module does not build anything. However, a copy of your prebuilt shared library will be copied into $PROJECT/obj/local, and another will be copied and stripped into $PROJECT/libs/.
如果您提供共享库,则包括 PREBUILT_SHARED_LIBRARY,而不是 BUILD_SHARED_LIBRARY。对于静态的,使用 PREBUILT_STATIC_LIBRARY。预构建模块不会构建任何东西。但是,您的预构建共享库的副本将被复制到 $PROJECT/obj/local 中,另一个将被复制并剥离到 $PROJECT/libs/ 中。
So let's try flattening everything out to match the trivial example. I copied my libraries out of their cozy /lib folder and put them in the jni root. I then did this:
因此,让我们尝试将所有内容展平以匹配这个简单的示例。我从他们舒适的 /lib 文件夹中复制了我的库并将它们放在 jni 根目录中。然后我这样做了:
LOCAL_PATH := $(call my-dir)
#get support_lib1
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib1
LOCAL_SRC_FILES := support_lib1.so
include $(PREBUILT_SHARED_LIBRARY)
#get support_lib2
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib2
LOCAL_SRC_FILES := support_lib2.so
include $(PREBUILT_SHARED_LIBRARY)
#build native lib
include $(CLEAR_VARS)
LOCAL_MODULE := native_lib
LOCAL_SRC_FILES := native_lib.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog
LOCAL_SHARED_LIBRARIES := support_lib1 support_lib2
include $(BUILD_SHARED_LIBRARY)
and... same error. Moreover I'm most definitely NOT seeing library files getting copied to $PROJECT/obj/local.
和...同样的错误。此外,我绝对没有看到库文件被复制到 $PROJECT/obj/local。
sooooo.... now what?
sooooo....现在呢?
回答by Alex Cohn
Your problem is with the naming convention. NDK and Android insist on the shared library names to always begin with lib. Otherwise, the libraries will not be linked properly, and not copied to the libs/armeabi
folder properly, and not installed on the device (copied to /data/data/package/lib
directory properly.
您的问题在于命名约定。NDK 和 Android 坚持共享库名称始终以lib开头。否则,库将无法正确链接,无法正确复制到libs/armeabi
文件夹,也无法安装在设备上(/data/data/package/lib
正确复制到目录。
If you rename support_lib1.so
to libsupport_1.so
and support_lib2.so
to libsupport_2.so
, and put these two files in jni/lib
directory, then your Attempt #5will work with minor change:
如果重support_lib1.so
来libsupport_1.so
,并support_lib2.so
到libsupport_2.so
,并把这两个文件jni/lib
的目录,那么你尝试#5将与小的改动工作:
LOCAL_PATH := $(call my-dir)
#get support_lib1
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib1
LOCAL_SRC_FILES := lib/libsupport_1.so
include $(PREBUILT_SHARED_LIBRARY)
#get support_lib2
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib2
LOCAL_SRC_FILES := lib/libsupport_2.so
include $(PREBUILT_SHARED_LIBRARY)
#build native lib
include $(CLEAR_VARS)
LOCAL_MODULE := native_lib
LOCAL_SRC_FILES := native_lib.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog
LOCAL_SHARED_LIBRARIES := support_lib1 support_lib2
include $(BUILD_SHARED_LIBRARY)
BTW, I don't think you need this -L$(SYSROOT)/../usr/lib
.
顺便说一句,我认为你不需要这个-L$(SYSROOT)/../usr/lib
。
PSDon't forget to update the Java side, too:
PS也不要忘记更新Java端:
static {
System.loadLibrary("support_lib1");
System.loadLibrary("support_lib2");
System.loadLibrary("native_lib");
}
回答by yano
Not sure if this is exactly where you are at, but here's what I know about these sorts of things.
不确定这是否正是您所在的位置,但这是我对此类事情的了解。
- Make each prebuilt libary its own separate Makefile. Multiple targets in Android.mk tends to get wonky. Sad.
- Include each make file using
$(call import-add-path)
and$(call import-module)
- Export as much as you can from the prebuilt's make files, using the
LOCAL_EXPORT_
family of variables.
- 使每个预建库成为自己单独的 Makefile。Android.mk 中的多个目标往往会变得不稳定。伤心。
- 使用
$(call import-add-path)
和包含每个 make 文件$(call import-module)
- 使用
LOCAL_EXPORT_
变量族从预构建的 make 文件中尽可能多地导出。
Prebuilt Shared Library Android.mk
预建共享库 Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := my_module_name
MY_LIBRARY_NAME := shared_library_name
### export include path
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
### path to library
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/lib$(MY_LIBRARY_NAME).so
### export dependency on the library
LOCAL_EXPORT_LDLIBS := -L$(LOCAL_PATH)/libs/$(TARGET_ARCH_ABI)/
LOCAL_EXPORT_LDLIBS += -l$(MY_LIBRARY_NAME)
include $(PREBUILT_SHARED_LIBRARY)
This is assuming that the prebuilt libaries live in a dir structure like this
这是假设预构建的库位于这样的目录结构中
+ SharedProjectFolderName
+--- Android.mk
+--- include/
+-+- libs/$(TARGET_ARCH_ABI)/
|- libshared_library_name.so
If you are not building for multiple ABI, I guess you can leave that bit out
如果您不是为多个 ABI 构建,我想您可以忽略这一点
The Project's Android.mk
项目的 Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := my_jni_module
## source files here, etc...
### define dependency on the other library
LOCAL_SHARED_LIBRARIES := my_module_name
include $(BUILD_SHARED_LIBRARY)
$(call import-add-path,$(LOCAL_PATH)/path/to/myLibraries/)
$(call import-module,SharedProjectFolderName)
$(call import-module,AnotherSharedProject)
I recommend you put all shared libraries in one folder. When you say $(call import-module,SharedProjectFolderName)
it looks for a folder containing an Android.mk
along the search path you told it (import-add-path
)
我建议您将所有共享库放在一个文件夹中。当你说它沿着你告诉它的搜索路径$(call import-module,SharedProjectFolderName)
寻找一个包含 的文件夹Android.mk
时 ( import-add-path
)
By the way, you probably shouldn't specify LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib
. It should be finding the proper libs from NDK by itself. Adding more linker paths will probably confuse it. The proper way is to export the linker paths as flags from the sub-modules.
顺便说一句,您可能不应该指定LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib
. 它应该自己从 NDK 中找到合适的库。添加更多链接器路径可能会混淆它。正确的方法是将链接器路径作为来自子模块的标志导出。
ALSO, you can use ndk-build V=1
to get a ton of info on why it can't find paths, etc
此外,您可以使用它ndk-build V=1
来获取有关为什么找不到路径等的大量信息
回答by Sam
The -L option gives the linker a directory path in which to look for libraries. The -l option gives the linker a library file name to link in. Library file names must begin with "lib". Your libraries should be named libsupport_lib1.so and libsupport_lib2.so. If you do that, then this is probably what you should do (replacing attempt #1):
-L 选项为链接器提供了一个用于查找库的目录路径。-l 选项为链接器提供一个要链接的库文件名。库文件名必须以“lib”开头。您的库应命名为 libsupport_lib1.so 和 libsupport_lib2.so。如果你这样做,那么这可能是你应该做的(替换尝试#1):
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog -lsupport_lib1 -lsupport_lib2
LOCAL_LDLIBS += -L$(LOCAL_PATH)/lib
The linker will prefix the library name you specify using -l with "lib" and suffix it with ".so". (Why do you have -L$(SYSROOT)/../usr/lib?)
链接器会将您使用 -l 指定的库名称加上“lib”作为前缀,并加上“.so”作为后缀。(为什么你有 -L$(SYSROOT)/../usr/lib?)
I believe that attempts #1 and #2 failed because you did not link your libraries into your executable - they are not mentioned in a -l option. By the way, you can verify this yourself. Unzip the .apk file and look in the lib directory and subdirectories. Are your .so files in there?
我相信尝试 #1 和 #2 失败是因为您没有将库链接到可执行文件中 - -l 选项中没有提到它们。顺便说一下,您可以自己验证这一点。解压 .apk 文件并查看 lib 目录和子目录。你的 .so 文件在那里吗?
Looking at the error:
查看错误:
but then... dlopen failed: Could not locate symbol func_that_exists_in_libsupport_lib.so referenced by libnative_lib.so
Can you supply the entire message? dlopen() loads and links libraries into the running process.
你能提供完整的信息吗?dlopen() 将库加载并链接到正在运行的进程中。