如何在 Android 牛轧糖中替换 dlopen()?

How to substitute dlopen() in Android Nougat?

我有一个 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,但当然没有人能说出它还能工作多久。

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

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

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

备案;这在 android-ndk google group/mailing 列表中进行了讨论,结果证明 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).