Android NDK and Newer API Support

前端 未结 2 717
再見小時候
再見小時候 2021-01-16 15:16

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 th

相关标签:
2条回答
  • 2021-01-16 15:17

    Take a look at https://developer.android.com/ndk/guides/stable_apis.html

    For example, if you don't use OpenGL ES 3.0 and OpenGL ES 3.1 APIs and don't link libGLESv3, then your app has compatibility to API level 14.

    0 讨论(0)
  • 2021-01-16 15:37

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

    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.

    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:

    • 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.)

    • 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.

    • 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.

    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.

    0 讨论(0)
提交回复
热议问题