专栏首页along的开发之旅Advice for Middleware Vendors JNI中间件厂商开发建议

Advice for Middleware Vendors JNI中间件厂商开发建议

Advice for Middleware Vendors

Distributing middleware built with the NDK imposes some additional problems that app developers do not need to worry about. Prebuilt libraries impose some of their implementation choices on their users.

Choosing API levels and NDK versions

Your users cannot use a minSdkVersion lower than yours. If your users' apps need to run on Android 16, you cannot build for Android 21.

NDK versions are largely compatible with each other, but occasionally there are changes that break compatibility. If you know that all of your users are using the same version of the NDK, it's best to use the same version that they do. Otherwise, use the newest version.

Using the STL

If you're writing C++ and using the STL, your choice between libc++_shared and libc++_static affects your users if you distribute a shared library. If you distribute a shared library, you must either use libc++_shared or ensure that libc++'s symbols are not exposed by your library. The best way to do this is to explicitly declare your ABI surface with a version script (this also helps keep your implementation details private). For example, a simple arithmetic library might have the following version script:

Note: If you distribute a static library, it does not matter whether you choose a static or shared STL because nothing is linked in a static library. The user can link whichever they choose in their application. They must link something, even for C-only consumers, so be sure to document that it is required and which version of the NDK was used to build in case of incompatibility in STL versions.

LIBMYMATH {
global:
    add;
    sub;
    mul;
    div;
    # C++ symbols in an extern block will be mangled automatically. See
    # https://stackoverflow.com/a/21845178/632035 for more examples.
    extern "C++" {
        "pow(int, int)";
    }
local:
    *;
};

A version script should be the preferred option because it is the most robust way to control symbol visibility. Another, less robust option is to use

-Wl,--exclude-libs,libc++_static.a -Wl,--exclude-libs,libc++abi.a when linking. This is less robust because it will only hide the symbols in the libraries that are explicitly named, and no diagnostics are reported for libraries that are not used (a typo in the library name is not an error, and the burden is on the user to keep the library list up to date).

For Java Middleware with JNI Libraries

Java libraries that include JNI libraries (i.e. use jniLibs) need to be careful that the JNI libraries they include will not collide with other libraries in the user's app. For example, if the AAR includes libc++_shared.so, but a different version of libc++_shared.so than the app uses, only one will be installed to the APK and that may lead to unreliable behavior.

Warning: Bug 141758241: The Android Gradle Plugin does not currently diagnose this error condition. One of the identically named libraries will be arbitrarily chosen for packaging in the APK.

The most reliable solution is for Java libraries to include no more than one JNI library. All dependencies including the STL should be statically linked into the implementation library, and a version script should be used to enforce the ABI surface. For example, a Java library com.example.foo that includes the JNI library libfooimpl.so should use the following version script:

LIBFOOIMPL {
global:
    JNI_OnLoad;
local:
    *;
};

Note that this example uses registerNatives via JNI_OnLoad as described in JNI Tips to ensure that the minimal ABI surface is exposed and library load time is minimized.

https://android.googlesource.com/platform/ndk/+/refs/heads/master/docs/user/middleware_vendors.md

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 网站开发合同,第1部分:如何避免重大灾难 (tech law)

    签订网站开发合同时,你越了解,就越有可能避免可能导致网站无法按时启动或无法正常工作的合同灾难。

    木樾233
  • ROS2编程基础课程--DDS

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

    zhangrelay
  • 新的App Dev CRM模型(Internet)

    C RM是一个充满转折点的行业,我认为这是另一个行业。出现了许多较小的转变,例如增加了分析,社交媒体和流程。但是,为了了解这一时间点,我认为您必须一直追溯到世纪...

    用户8054058
  • 机器人操作系统二(ROS2)- 修行 Taoism

    ROS 2项目的目标是继承ROS 1的优点并改进不合适的部分。The goal of the ROS 2 project is to leverage what...

    zhangrelay
  • 这是在线假冒产品的季节

    2020年的假期季节即将来临,现在应该是购物的黄金时间,但这种大流行迫使许多消费者收紧钱包。除了试图避免人群躲避COVID-19之外,今年的数字假日购物者还必须...

    用户8078865
  • ROS2编程基础课程--概念

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

    zhangrelay
  • 胡立军:从Gartner“Hype Cycle”看 RPA发展走向

    南京大学MBA,微软MCT认证讲师,曾任职大学计算机讲师,国内较早网络及云安全从业者,艺赛旗创始高管之一,现任上海艺赛旗软件股份有限公司高级副总裁,主要负责艺赛...

    艺赛旗RPA
  • webpack4配置详解之慢嚼细咽

      经常会有群友问起webpack、react、redux、甚至create-react-app配置等等方面的问题,有些是我也不懂的,慢慢从大家的相互交流中,也...

    苏南
  • Django 处理http请求之中间件

    中间件“工厂”是一个携带一个可调用get_response参数并返回一个中间件的的可调用对象。中间件则是一个携带request参数并返回一个response的可...

    授客

扫码关注云+社区

领取腾讯云代金券