android - 如何在 Android Nougat 中替换 dlopen()?

标签 android android-ndk dlopen

我有一个 NDK 应用程序,可以通过 dlopen 加载的插件进行扩展。这曾经工作得很好,但它不再适用于 Android N。如 developer documentation 中所述, dlopen 现已正式废弃:

Starting in Android 7.0, the system prevents apps from dynamically linking against non-NDK libraries, which may cause your app to crash. This change in behavior aims to create a consistent app experience across platform updates and different devices.
...
All apps generate a runtime error when they call an API that is neither public nor temporarily accessible. The result is that System.loadLibrary and dlopen(3) both return NULL, and may cause your app to crash.

我看到有一个 hack简单地为 Android 手动实现 dlopendlsym。这似乎适用于 Android N,但当然没有人知道它还能工作多久。

这就是为什么我正在考虑将我的插件设计更改为官方支持且面向 future 的东西。最明显的选择是使用 Android 的服务 API,并将我的插件简单地分发为实现服务的单独 APK。

但是,我不太喜欢这个想法,因为我使用的是 NDK,而且我认为服务 API 不适用于 NDK。当然,我可以用 Java 创建一个服务,然后使用 JNI 将它与我的 C 代码集成,但这当然不是一个非常优雅的解决方案。

这就是为什么我想问一下,在 Android N 中支持以前基于 dlopen 的插件的推荐方法是什么?是否有替代路线,或者我现在是否被迫使用服务 API?

最佳答案

备案;这在 android-ndk 谷歌组/邮件列表中进行了讨论,事实证明 dlopen 根本不是原始海报的罪魁祸首:

https://groups.google.com/d/msg/android-ndk/hhUv1mg3c_A/a5BbRtr-AwAJ

Oops, sorry, actually, dlopen() is still working fine on Android 7.0. I was getting the following error message:

Detected problems with app native libraries (please consult log for detail): 
foobar.so: text relocations 

进一步阐明了开发者文档中的措辞:

https://groups.google.com/d/msg/android-ndk/hhUv1mg3c_A/eIKHZZcDBAAJ

I guess the wording does technically make it sound like you're now [sic: read not] allowed to link to your own libraries. That definitely is not the case. The policy is that your cannot link to or dlopen private system libraries (anything in /system/lib that is not in the NDK).

关于android - 如何在 Android Nougat 中替换 dlopen()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41897797/

相关文章:

android - Android NDK路径变量

dynamic-linking - dlopen/dlsym : error getting function pointer

c++ - 为什么链接器在用于在 linux 中编译的路径中搜索库

android - 在 Android 中构建 Webkit

android - 生成 header 后,Android studio 未选取 JNI 文件夹

java - dlopen:无法使用静态 TLS 加载更多对象

android - 如何在表面 View 上绘制?

java - 具有高级功能的 Android Eclipse 项目布局

android - Linphone 使用 Android 重新注册到 Sip 服务器

android - 使用远程调解器和分页源对具有分页 3 的存储库进行单元测试