[英]dlopen fails on older Android versions
I'm loading my native library like this at runtime 我正在运行时加载我的本机库
dlopen("mylib.so", RTLD_LAZY);
This works fine on recent version of android (eg marshmallow, nougat, etc.). 这适用于最新版本的android(例如marshmallow,nougat等)。 However, on older versions (eg Jellybean), this fails with the following message in logcat
但是,在旧版本(例如Jellybean)上,这在logcat中失败并显示以下消息
Failed to load mylib.so. Error: Cannot load library:
load_library[1093]: Library 'mylib.so' not found
I have ensured that mylib.so
is part of the apk. 我确保
mylib.so
是apk的一部分。 I have this as part of x86
and armeabi_v7a
architectures. 我有这个作为
x86
和armeabi_v7a
架构的一部分。
myapp.apk
- lib
- armeabi-v7a
mylib.so
- x86
mylib.so
As per readelf -d mylib.so | grep NEEDED
根据
readelf -d mylib.so | grep NEEDED
readelf -d mylib.so | grep NEEDED
the dependencies are readelf -d mylib.so | grep NEEDED
依赖项
0x00000001 (NEEDED) Shared library: [liblog.so]
0x00000001 (NEEDED) Shared library: [libm.so]
0x00000001 (NEEDED) Shared library: [libdl.so]
0x00000001 (NEEDED) Shared library: [libc.so]
I tried loading these dependencies through dlopen
prior to loading mylib.so
. 我在加载
mylib.so
之前尝试通过dlopen
加载这些依赖项。 Though loading of those dependencies succeed, loading mylib.so
always fails with the same error. 虽然这些依赖项的加载成功,但加载
mylib.so
总是失败并出现相同的错误。
As mentioned before, I see this failure only on older versions of android. 如前所述,我只在较旧版本的android上看到此失败。
How can I get this to work? 我怎样才能让它发挥作用? Thanks in advance for any help.
在此先感谢您的帮助。
Prior to Lollipop , you must supply the full path to dlopen() . 在Lollipop之前,您必须提供dlopen()的完整路径。 This path can be obtained in Java with
getApplicationInfo().nativeLibraryDir
. 可以使用
getApplicationInfo().nativeLibraryDir
在Java中获取此路径。
At any rate, you can check the contents of the /data/data/your.package/lib directory (sometimes it has -1
suffix, or starts with /data/app-lib ). 无论如何,您可以检查/data/data/your.package/lib目录的内容(有时它有
-1
后缀,或者以/ data / app-lib开头)。 This directory is world-readable, so you you should see the file libmylib.so there from Android Studio or adb shell
. 这个目录是世界可读的,所以你应该从Android Studio或
adb shell
看到libmylib.so文件。
Another reason for the observed behavior could be that the older device does not support armeabi-v7a ABI. 观察到的行为的另一个原因可能是旧设备不支持armeabi-v7a ABI。 That's easy to check with
adb shell getprop
. 使用
adb shell getprop
很容易检查。
Also, make sure that the library is built with the correct APP_PLATFORM
, which should match the minSdkVersion . 此外,请确保使用正确的
APP_PLATFORM
构建库,该APP_PLATFORM
应与minSdkVersion匹配 。
Your library luckily does not have 'private' dependencies. 你的图书馆幸运地没有'私人'依赖。 But for those that do, you need to take this into account, too.
但对于那些做的人,你也需要考虑到这一点。 Prior to Lollipop , these dependencies would not be loaded automatically (because the
nativeLibraryDir
was not scanned for dlopen ). 在Lollipop之前,不会自动加载这些依赖项(因为未对dlopen扫描
nativeLibraryDir
)。 You had to load all libraries in reverse dependency order. 您必须以反向依赖顺序加载所有库。
尝试将文件夹名称从lib
更改为jniLibs
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.