Android NDK和更新的API支持 [英] Android NDK and Newer API Support

查看:265
本文介绍了Android NDK和更新的API支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用大量使用NDK的Android应用程序.在Java方面,我们以最小SDK为16为目标SDK19.是否可以在NDK方面进行类似的操作?

I'm working on an Android app that has heavy use of the NDK. On the Java side of things, we target SDK 19 with a min SDK of 16. Is there a way to do something similar on the NDK side?

现在,我们的Application.mk文件具有APP_PLATFORM := android-16.有没有办法定位到平台19,但在NDK端仍具有回到16的兼容性?

Right now our Application.mk file has APP_PLATFORM := android-16. Is there a way to target platform 19 but still have compatibility back to 16 on the NDK side?

推荐答案

这是可行的,但并不是太容易.

It's doable, but it is not too easy.

对于Java代码,您可以设置更高的目标SDK版本并使用这些功能,只要确保这些代码路径仅在较新的设备上执行即可即可.

For the java code, as you know, you can set any higher target SDK version and use such features, as long as you make sure that those codepaths only are executed on the newer devices - simple.

对于本机代码,原则上可以设置比基线更高的APP_PLATFORM,并尝试执行相同的操作,但是需要注意一些细节:

For the native code, you can in principle set a higher APP_PLATFORM than your baseline, and try to do the same, but there's a few details you need to keep track of:

  • 您不能无条件地从较新的平台链接到函数,需要动态加载它们.也就是说,不是直接调用函数并将库添加到LOCAL_LDLIBS,而是需要通过dlopendlsym加载函数,以确保二进制文件可在较早版本上加载. (或者,您可以构建单独的共享库,其中一个共享库可以在所有平台上加载,而另一个只能在较新的平台上加载.)

  • You can't unconditionally link to functions from the newer platform, you need to load them dynamically. That is, instead of calling functions directly and adding the libraries to LOCAL_LDLIBS, you need to load the functions via dlopen and dlsym instead, to make sure that the binary is loadable on the older versions. (Or alternatively, you can build separate shared libraries, where one shared library can be loaded on all platform, while another one only can be loaded on newer platforms.)

某些仿生libc函数已更改(主要在android-21中,但在此之前也做了一些次要的更改)-以前确实存在但已更改符号名称的函数.已更改的最常见功能之一是rand-在android-21之前,rand是实际上称为lrand48()的内联函数,因此您的二进制文件最终取决于较旧android中存在的lrand48版本的libc.so,而那里没有任何rand.在android-21中,添加了许多此类函数,并且删除了内联函数,因此,如果使用APP_PLATFORM := android-21进行构建,则二进制文件最终将取决于以前不存在的函数rand.请参阅 https://stackoverflow.com/a/27093163/3115956

Some bionic libc functions have changed (mainly in android-21, but some minor ones also changed before that) - functions that did exist before but have changed symbol name. One of the more common functions that has changed is rand - prior to android-21, rand was an inline function that actually called lrand48(), so your binary ended up depending on lrand48 which existed in the older android versions' libc.so, while they didn't have any rand there. In android-21, a lot of such functions have been added, and the inline functions removed, so if you build with APP_PLATFORM := android-21, your binary will end up depending on the function rand which didn't exist before. See https://stackoverflow.com/a/27093163/3115956 and https://stackoverflow.com/a/27338365/3115956 for more details about this.

请记住,您不需要将APP_PLATFORM设置为与Java端的目标SDK相同,仅(可能)需要设置它,如果您想有选择地在上使用较新的功能较新的固件版本.

Keep in mind that you don't need to set APP_PLATFORM to the same as the target SDK on the java side, you only (may) need to set it if you want to selectively use newer features on newer firmware versions.

由于第二个问题,您可能根本不想设置更高的APP_PLATFORM.如果使用dlopen(因此实际上不需要链接.so文件),则可以通过将这些新的头文件从较新的平台版本复制到您自己的项目中,并使用较旧的APP_PLATFORM.

Due to the second issue you might not want to set a higher APP_PLATFORM at all. If you use dlopen (so you don't actually need the .so files to link against), you can manage pretty easily by copying those new headers from the newer platform version into your own project, and building with the older APP_PLATFORM.

这篇关于Android NDK和更新的API支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆