What is the relation between APP_PLATFORM, android:minSdkVersion and android:targetSdkVersion?
Asked Answered
S

3

20

I'm developing an Android app that uses NDK features. My app defines android:minSdkVersion and android:targetSdkVersion in AndroidManifest.xml and APP_PLATFORM in jni/Application.mk.

My current understanding is that android:minSdkVersion decalres minimal supported OS version, android:targetSdkVersion declares Java library version to be linked against, and APP_PLATFORM declares C++ library to be linked against.

Two questions:

  1. Is my understanding correct?

  2. Is it Ok for APP_PLATFORM to be greater that android:minSdkVersion? Or they must be equal each other?

The reason for my question: I want my app to be available for devices with API >= 10, but I need to use NDK functions (like AMotionEvent_getAxisValue) that are defined in platforms\android-13 folder in NDK. So I use android:minSdkVersion=10 and APP_PLATFORM=13. Project compiles successfully, but would it be runnable on API 10-12 devices?

Saransarangi answered 19/2, 2014 at 17:34 Comment(0)
C
25
  1. android:minSdkVersion is the minimum OS version that your app expects.

  2. android:targetSdkVersion is essentially the maximum OS version that you've designed your app to work with. Here's an example of how this works. Imagine that you tested your app fine with API 19 and you release your app with android:targetSdkVersion=19. Then Google decides to release API 20 with a change in behavior of some API, but they don't want to change the behavior for old apps (to prevent from breaking them). So when your app starts up, Android sees that your app has targetSdkVersion=19, so it gives you the old API behavior, but if some other app says targetSdkVersion=20, Android will give it the new API behavior.

  3. APP_PLATFORM is the version of the native headers and libraries that the NDK will compile your native code with. If you set APP_PLATFORM to a specific value and you use APIs that are only available in that platform version, then your app will not run properly on older platforms. So APP_PLATFORM is a minimum value. The solution is to use a lower value and not use those newer APIs, or to write code that decides at runtime whether to call the new APIs or not (and probably use dlopen/dlsym).

It seems like in general it doesn't make sense to use an APP_PLATFORM value newer than android:minSdkVersion, unless you're doing some special (like being careful not to call new APIs by checking the version at runtime, plus making sure not to link to new APIs and instead using dlopen/dlsym).

So if you use APP_PLATFORM=13 and you call AMotionEvent_getAxisValue (which is not in earlier platform headers, implying that it isn't available at runtime on earlier platforms), your app will not run on devices with API < 13. The one caveat would be if AMotionEvent_getAxisValue is actually available on older versions, but it just wasn't in the header/library files or it just wasn't documented. But I don't know if that's the case for this particular API (basically, that would require more research and risk analysis of whether you want to depend on something unsupported).

Catie answered 24/2, 2014 at 8:52 Comment(4)
Thanks. But there is another version specifier: What about 4. project.properties 'target=android-xx' ?Mathison
@Mathison That's the one that actually determines which version of the Android library your (Java) code is built against. It's equivalent to APP_PLATFORM but for non-native code. If you don't specify APP_PLATFORM, it defaults to the value set in project.properties.Conclusive
Note that for native code, you don't necessarily have to use new APIs for your code to be incompatible with older Android versions. Just setting APP_PLATFORM too high can result in the compiler deciding to use primitives that don't exist (e.g. if APP_PLATFORM >= 24, calling std::vector::resize maxe the compiler produce code that doesn't work on Lollipop). In short, it's dangerous to ever have APP_PLATFORM higher than minSdkVersion (and since it defaults to the SDK version you're building non-native code for, you should always set it explicitly).Conclusive
Cool example Karu. I included that in a roundup answer: https://mcmap.net/q/610405/-what-is-the-relation-between-app_platform-android-minsdkversion-and-android-targetsdkversionAnderton
A
39

This answer brings together hard-to-find but important information from many excellent websites and stackoverflow answers/comments on this under-documented NDK subject:

Avoiding Crashes: NDK Version, minSdKVersion, targetSdkVersion, and APP_PLATFORM (in project.properties)

The short version

To prevent your native Android app from mysteriously crashing on older customer devices, the short answer is to make your NDK APP_PLATFORM (in project.properties or Application.mk) the same as your minSdkVersion (in AndroidManifest.xml).

But depending on what NDK features you use, doing so may severely limit the set of customers who can download your app.

To find out why that is and whether you have other options, read on...

Four different concepts

  • NDK Version (e.g. r10e, r13b): this is the version of the Android NDK release (the tar/zip file) you download from Google. Here are all the NDK versions

  • minSdkVersion (e.g. 15,19,21) is a setting you make in your AndroidManifest.xml in a <uses-sdk> element under the <manifest> element. This setting affects both NDK (native) and Java development.

  • targetSdkVersion (e.g. 15,19,21) is a different setting you make in your AndroidManifest.xml in a <uses-sdk> element under the <manifest> element. This setting affects only Java development.

  • APP_PLATFORM (e.g. 15,19,21) is a setting you make in your Native NDK project. Typically this setting is found in the project.properties file at the root of your project as the last number at the end of the target= line, e.g. target=Google Inc.:Google APIs:21 for level 21 (and usually the way that "21" got there is by being the -t/--target option to a command-line invocation of the android create project or android update project command). You can also make this setting by putting APP_PLATFORM := android-21 into your Application.mk file.

Three of these concepts use API Level Numbers

minSdkVersion, targetSdkVersion and APP_PLATFORM use Android's consistent numbering scheme for API levels:

Click here to see Google's API Level Chart

As you can see, the levels roughly correspond to Android releases, but do not even remotely match the number of the Android release (that would be too easy). For example, API level 10 corresponds to Android OS 2.3.3 and 2.3.4.

The cute code-names like "Lollipop" and "Nougat" are at a coarser granularity than API versions. For example, several API versions (21 and 22) are "Lollipop"

These same API version numbers are encoded by the Java Build.VERSION_CODES type:

Click here to see Build.VERSION_CODES

For example, level 22 is LOLLIPOP_MR1

Not every number exists for each concept. For example, you might want to support API level 10, but there is no APP_PLATFORM 10 so you would go back to the last available APP_PLATFORM, APP_PLATFORM 9.

Click here to see distribution of API versions in the installed base in the wild

What is minSdkVersion?

This setting is by far the easiest to understand. When you set minSdkVersion to, say, 21 (corresponding to Android OS 5.0), that means that the Google Play Store will only advertise your app as supporting Android OS 5.0 and beyond, and Android will prevent users from installing your app if their Android device has lower than Android OS 5.0 installed.

So minSdkVersion is the lowest OS you support. Nice and simple.

This setting obviously has implications both for Java and C code: you want to set it to the lowest OS version that both parts of your code can support.

Both targetSdkVersion and APP_PLATFORM must be greater than or equal to minSdkVersion.

What is targetSdkVersion?

This setting only has effect in the Android Java world (it has no effect on your C NDK code). Its fulfills a purpose in the Android Java world that is similar to APP_PLATFORM in the Android C NDK world.

Sometimes you want your app to support older devices, but also use new features only available on newer Java API versions.

For example, Android added a nifty Java VoiceInteractor API that's only supported in API 23 (Android 6.0) or later. You might want to support VoiceInteractor if your customers have a new device, but still have your app run on older devices.

By setting targetSdkVersion to 23, you are making a simple contract with Android:

  • Android agrees to give your app code access to API-23-only Java features like VoiceInteractor
  • In return, you agree to check, at runtime in your Java code, whether that feature is available on the customer's device before calling it (otherwise your app will crash).

This contract works because in Java, it is "ok" if your code has references to classes/methods that might not exist on the customer's device, as long as you don't call them.

The contract applies to all Android Java features added after your minSdkVersion up to and including your targetSdkVersion.

In addition to giving you access to certain new Java APIs, the targetSdkVersion setting also enables or disables certain well-documented compatibility behaviors:

Click here to see which behavior changes come with each targetSdkVersion

These well-documented changes also form a kind of contract. For example, around Android 4, Google migrated their Android device designs away from having a dedicated menu button and towards having an on-screen Action bar. If your app has a targetSdkVersion lower than 14 (Android 4.0.1), Google would put a software menu button up on the screen to make sure your app kept working even if the device didn't have a dedicated menu button. But by choosing a targetSdkVersion greater than or equal to 14 at build time, you are promising Google that you either don't have a menu or you use an Action bar, so Google no longer puts up the software menu button.

What is APP_PLATFORM?

APP_PLATFORM performs a similar function in the C NDK world that targetSdkVersion performs in the Java world.

But, sadly, due to a combination of limitations in the C language and bad behavior by Google, APP_PLATFORM is significantly more dangerous and, frankly, nearly unusable.

Let's start from the beginning...

APP_PLATFORM is an NDK-only setting that tells the build-ndk tool which subdirectory of your NDK to look in for certain key include files and libraries which collectively are referred to as an NDK "platform." Each NDK distribution (each NDK tar/zip that we developers download from Google) contains multiple platforms.

For example, if you set APP_PLATFORM to android-21, build-ndk will look in:

$(ndk_directory)/platforms/android-21/arch-$(architecture)/usr/include
$(ndk_directory)/platforms/android-21/arch-$(architecture)/usr/lib

for include files and libraries.

If you installed your NDK by simply downloading a zip/tar from Google's NDK Downloads Website, then $(ndk_directory) is simply the directory where you extracted the file.

If you installed your NDK by first downloading the Android (Java) SDK and then running the Android SDK Manager to install the "NDK" item, then $(ndk_directory) is $(sdk_directory)/ndk-bundle, where $(sdk_directory) is wherever your SDK is installed.

$(architecture) is arm, arm64, x86, etc.

What is in a "platform"?

The $(ndk_directory)/platforms/android-XX directory contains two super-important things:

  • all your C library calls like fopen(), atof(), sprintf() etc. The C library on Android is called "bionic."
  • Android-specific NDK calls/types/defines like AInputQueue and EGLContext

What changes at different APP_PLATFORM levels?

In each android-XX version, Google adds more calls to the NDK. For example,

  • APP_PLATFORM API level 9 added the very useful NativeActivity
  • APP_PLATFORM API level 18 added OpenGL ES 3.0

Some APP_PLATFORM versions also add calls to the C library, and/or "fix" things that are missing (for example, the token PTHREAD_KEYS_MAX got added in APP_PLATFORM 21).

Click here to read Google's incomplete documentation on what changed in each APP_PLATFORM level

So far this is similar to the Java world. Nobody expects Google or any other OS vendor to make every new feature available on old devices, especially when those features rely on hardware only found on newer devices (e.g. faster processors, new camera features, new audio features).

But Google's NDK team did a naughty thing that the Java team did not.

In some APP_PLATFORM versions, Google made gratuitous, breaking API changes that cannot possibly be excused by any legitimate argument such as those above.

These are the types of breaking API changes that Android Java developers would never accept. For example, Google has

  • renamed C library functions and
  • changed C library functions from being inlined to being not inlined

The most serious case of this was APP_PLATFORM 21, where Google made many breaking changes that generated an extremely high number of stackoverflow issues (many examples here and more below).

But there also have been changes in previous APP_PLATFORMs (e.g. signal() in API 19).

And there are even some breaking changes in APP_PLATFORMs after 21 such as APP_PLATFORM 24 (e.g. std::vector::resize as Karu mentions in a comment of this question).

So this is clearly a bad Google habit that is here to stay.

Why do these changes make my app crash on old devices?

To see why these naughty changes are a problem, remember that the C library on Android is a shared library, meaning that the implementation of non-inline, non-macro calls like sprintf() is not compiled into your program but rather present in the C library on your test devices and on each customer device.

So it doesn't just matter what API version you have in your development environment. It also matters what API version of C library is on each device where your app might run.

Suppose your app calls atof() and you build your app with APP_PLATFORM 21 and test it on your modern test devices that run Android 5 or later (API version 21 or later). Everything looks ok.

Then you release your app and suddenly find tons of customers with Android OS versions 4.4 and earlier (API versions less than 21) report your app crashing on their devices.

What's going on?

In APP_PLATFORM 21 (Android 5), atof() is a regular (not inline, not macro) function. So the native part of your app (the myapp.so file that ndk-build will create, and that you load from your Java code using System.loadLibrary("myapp")) will be marked as having a dependency on an external function called atof() in the C library.

When you run your app on a given device, Android will open your myapp.so, see the dependency on atof(), and find atof() in the C library on that device.

But the shock surprise is that in APP_PLATFORMs earlier than 21, atof() was an inline function in the platform header files, meaning that:

  • its implementation (its definition, its code, its body) was in the header file and got compiled into your app when you built your app
  • there is no atof() implementation in the C library on any customer device with API < 21 (any customer device running Android < 5). There never needed to be, since atof() was inline back in those days.

So when you run your app on devices running API version < 21 (Android OS < 5), the Java call System.loadLibrary("myapp") fails because the run-time loader cannot find all the symbols needed by your myapp.so. Android knows your myapp.so needs atof() but cannot find atof() in the C library on the device. Crash.

This atof() example is only one of many, undocumented or barely-documented breaking changes that Google brazenly refers to as WorkingAsIntended. Besides atof(), you can find huge numbers of other stackoverflow items with the same cause (e.g. with mkfifo() and, unbelievably, even rand())

How can I fix it?

In the atof() example above, you might say to yourself "okay, if there is no atof() on older devices, I'll provide one in my app and ship a new app version."

And in fact that would work.

But you'll have a sinking feeling in your stomach when you realize there is no answer to a much more important question:

How can I know what changed, and what old devices will be affected?

Here's the real kicker. You can't.

Unlike the Android Java API, where Google carefully maintains backwards compatibility with old APIs, clearly documenting any behavior changes that are keyed to the targetSdkVersion parameters, there is no such documentation for Android NDK APP_PLATFORM levels.

Like the Java API, you can look up an NDK call and find out what is the earliest API version (the earliest customer Android OS) where that call is supported.

But unlike the Java API with targetSdkVersion, when you change your NDK APP_PLATFORM level, you will not be able to find any Google documentation that tells you:

  • what API changes (possibly even C library API changes) exist that might break your app on older devices. For example, a list of functions like atof(), mkfifo() and rand() for which you would need to provide your own implementation for older devices
  • what effect not providing those re-implemented routines will have on the lowest Android OS that you can now support with your app

Simply put, Google won't tell you the earliest Android version that each APP_PLATFORM supports.

If you happen to have a lot of old devices lying around and a lot of time, you could try your app on every possible old Android version and see what crashes with missing C library symbols, providing custom implementations for functions that are not found. Of course that's only the first level of testing: in reality Google could have made breaking changes where the symbol is still there (so no crash), but the call behaves differently. This would never be accepted at the Java level but for some reason Google feels entitled to do it with the NDK.

Of course, nobody has time to do this, nor should developers have to.

So effectively what that means is that this is the official Google NDK policy:

Every time you increase the APP_PLATFORM of your project, you get access to new APIs, but you also get some breaking changes that will cause your app to crash on some older devices. Oh, and we're not going to give you a specific list of those changes. Nor are we going to tell you the earliest Android OS version on which your app is still guaranteed to work.

And effectively what that means is:

Every time you increase the APP_PLATFORM of your project, you have to set minSdkVersion equal to APP_PLATFORM, preventing your app from running on older devices. Otherwise your app may crash on some older devices.

It is hard to overstate how tragic this is.

Google is effectively telling you "in order to use new NDK features, you must abandon all your customers with old devices and abandon future sales to customers with older devices."

To make this tragedy concrete with a real-world example, notice that Google added support for OpenGL ES 3.1 in API Level 21 (Android OS 5.0). Suppose you wanted to support new OpenGL ES 3.1 features on new devices, but still support OpenGL ES 3.0 (API level 18 (Android OS 4.3)) and OpenGL ES 2.0 (API level 5 (Android OS 2.0)) on older devices. This is a very likely scenario since (unlike the transition from OpenGL ES 1 to 2) the changes in OpenGL ES 2 to 3 are quite minor and cumulative.

In order to support ES 3.1 from your app with Google's absurd NDK policy, you would have to drop support for all devices with less than Android 5.

Are there workarounds?

Kind of, but it's unlikely any developer has the time for them.

The first workaround was mentioned above: carefully test your app on every possible old Android version, not only for symbol-not-found crashes but behavior changes too.

The second workaround is that you can, in theory, "ship" different versions of your NDK code to customers with different API versions.

The easiest way is probably do that at the NDK level. For example, you could build multiple myapp.sos in your NDK build, each with a different APP_PLATFORM value in Application.mk, and bundle all of them into your app .apk. Then from your Java code you could System.LoadLibrary() a different .so depending on the API version of the customer's device.

This would be similar in structure to how NDK developers currently bundle multiple NDK versions for each architecture (e.g. armeabi, armeabi-v7a, mips, x86).

However, there is a massive practical difference: unlike the multiple ABIs which ndk-build more or less provides for free without wasting developer time, the developer would have to spend a lot of time hacking both the NDK and Java build scripts to create and distribute multiple APP_PLATFORM .so versions. Then every time developers change their C code, they must carefully consider how each function they call behaves (if it even exists) in each API version. This kind of work is totally expected and acceptable for calls that relate directly to new hardware features, but it is totally ridiculous that Android's NDK team makes us do this for calls like atof() and rand().

The third workaround is the one I suspect most developers do: fix problems as angry customers report them, and pray that there are not more such crashes to come (or causing customers to give their apps bad reviews and never reporting the issue to the developer).

dlsym() is a non-workaround

You might compare the C NDK APP_PLATFORM compatibility issue to the much cleaner Java targetSdkVersion and say

"hey, if I can set targetSdkVersion and then check for new features at runtime in Java, can't I set APP_PLATFORM and check for new features in runtime at C?"

Well, no.

The first problem is that in order to do this in C, unlike in Java, you have to refrain from even referencing the routine in your code. Then you'd have to open the C library with dlopen() and try to extract the routine you want with dlsym(). Let's not even get into the likelihood of vendor device-dependency even finding the C library. Plus the complexity that due to Android breaking changes, some routines have changed name so even the name you look up would have to depend on the API version of the device.

But the second, worse problem is that sometimes you are not the one making the call. As we will explain just below, the compiler may insert calls to routines that Google has broken, like stpcpy() and std::vector::resize, and you are not in a position to replace these invocations with a call to dlopen() and dlsym(). The only way to prevent the compiler from calling them is to reduce APP_PLATFORM, and this defeats the purpose of accessing new features on compatible devices.

Would having a list of changed calls help?

Amazingly, no. The problem is even worse than it sounds.

Let's say hypothetically Google did publish a complete list of all routines like atof() with breaking changes. You could just scan your code for those routines, and if you don't call them, you're safe, right?

Wrong.

It turns out that sometimes the bleeding compiler will call those breaking-changes routines without them actually appearing in your code:

  • if you build to APP_PLATFORM 21 (Android 5) and run on older devices (Android <5), you may be shocked to see crashes because the older devices cannot find stpcpy(), a routine you never call. It turns out the compiler will notice certain stpcpy()-like patterns in your code and replace them with a call to stpcpy()! This can be seen in many stackoverflow examples: example 1 example 2 example 3 example 4. If you attempt to implement stpcpy() yourself for backwards compatibility, you will get an infinite loop unless you are clever enough to implement it in a sufficiently non-stpcpy() way! Insanity.
  • if you build to APP_PLATFORM 24 you will encounter a similar problem with std::vector::resize as Karu mentions in a comment of this question).

In both cases, the compiler decides it can insert these calls because it inspects the set of include files you are using---your APP_PLATFORM---and decides the calls are available. You can't reduce APP_PLATFORM without losing access to the new routines you want to use on new devices. Catch-22.

How could Google get away with this?

Summarizing the above, Google's effective NDK policy is:

Every time you increase the APP_PLATFORM of your project, you have to set minSdkVersion equal to APP_PLATFORM, preventing your app from running on older devices, unless you are willing to do massive exhaustive testing on old devices, or pray.

I could never actually find an official Google policy statement to this effect. Google's official documentation says exactly the opposite. In particular, this passage from Google's official NDK Levels documentation is utter nonsense:

Each new release of NDK headers and libraries for a given Android API level is cumulative; you are nearly always safe if you use the most recently released headers when building your app. For example, you can use the NDK headers for Android API level 21 for an app targeting API level 16. By doing so, however, you increase your APK's footprint.

In fact the exact opposite is true: it is imperative that you use API level 16 headers to target API level 16 devices, otherwise your app will crash if you use one of a large set of APIs like atof() with undocumented breaking changes.

About the most help we ever got from Google was a cryptic NDK build warning WARNING: APP_PLATFORM android-XX is larger than android:minSdkVersion without any associated documentation that I could find.

I hope this answer has been eye-opening and helpful for developers, and perhaps as we increase awareness amongst developers it might motivate the Google NDK developers to respect backwards compatibility as much as the Google Java developers do.

References: many, many links to other stackoverflow answers and other webpages are interleaved in the text above.

Anderton answered 10/12, 2016 at 19:34 Comment(12)
FWIW, it does seem like Google are working on improving this situation, in the form of a feature they call "Unified Headers". See github.com/android-ndk/ndk/issues/120 and android.googlesource.com/platform/ndk.git/+/master/docs/… for info on the feature, and github.com/android-ndk/ndk/issues/113 for a discussion on the matter. Currently it seems to be planned to be included in NDK r14.Jobholder
Thanks for the links! I looked at them but it's still unclear whether the "Unified Headers" project really has anything to do with backwards compatibility: the ability for an app that uses, say, android-15 to change to using android-21 to get some new features but also have the old features still work on older devices with Android versions as far back as API level 15. That is the basic meaning of "backwards compatibility" in the Android Java world and pretty much any other world, but it doesn't seem to be a priority for the NDK team.Anderton
As far as I know, that is the intention, although I haven't explored it fully. It will be one single set of headers that will include all features, and you'll tell it what your minimum version is, and will behave accordingly. I'm not sure if it will completely hide all newer functionality though, or if it will keep new functionality available (which you'd need to use with care, e.g. dlsym or whatnot). At least it should switch the tricky functions (rand etc) to the version supposed to be supported by the baseline version. I guess the details will be seen when the first r14 beta is out.Jobholder
The Unified Headers feature seems to be out for testing now, since NDK r14 beta 1 is available. From a quick glance it seems to work pretty much as expected. But as feared, most/all the newer APIs are also hidden if you indicate that you target an older API version. Perhaps they'd be open to exposing new enums and structs even if you target an older version (to help using new APIs conditionally), but you would still need to load new functions using dlsym in any case.Jobholder
"Simply put, Google won't tell you the earliest Android version that each APP_PLATFORM supports." The earliest Android version that each APP_PLATFORM supports is APP_PLATFORM. I have actually documented this, fwiw: android.googlesource.com/platform/ndk/+/master/docs/user/…. Yes, the published docs say pretty much the opposite. I found that a week or so ago and rewriting it is in someone's task queue.Phatic
Talking about backward compatibility is tricky. No OS I can think of guarantees that a program using features only available in a newer version of the OS will be able to load on an older OS. Games using DirectX 11 will not work on Windows 98. Android doesn't treat Java any differently here. You can't use new APIs on an old device. The behavioral difference here comes from the language spec, not any policy.Phatic
dlsym is the supported way of getting new APIs with a lower target version. If you're dlsyming public APIs and your app breaks on some future version of Android, that's a bug. Android has developer previews and beta releases. If your app is broken on a beta for this reason, we can fix it as long as it gets reported. I don't really follow the issue with lrand48. The issue mentioned is that you can't dlsym rand because it's an inline on older versions. So, why bother dlsyming it? Just use it; it's available.Phatic
"but it doesn't seem to be a priority for the NDK team." You don't have to presume our priorities, I publish them: android.googlesource.com/platform/ndk/+/master/docs/…. The compatibility library has been my end game for nearly two years now, there was just a lot of prerequisite things to get done before it was feasible. The compatibility library is our plan for easing the pains that are dealing with dlsym.Phatic
Apologies for the barrage of comments, but there was a lot of content to cover and the character limit is short (seems like comments on long answers should get a higher character count). There's actually quite a lot more to be said here, but this really isn't the venue for it, so I tried to keep my comments limited to things that could be altered or included in your answer. tl;dr: we're working on it, but these things take time.Phatic
This is an excellent writeup and should be the first thing anyone reads that updates their NDK to a newer release and/or increases their platform levels. I have spent countless hours on the problems Google introduced by changing stuff around in later platforms. Even one of Google's own, in several messages I found, recommends always using the latest NDK platform. Yeah right...don't do that. As a rule of thumb, it seems, you should select the next level down from the min SDK, so if min sdk is 16, use NDK platform 15. Android Studio further complicates this by forcing the platforms on you..Lynnet
"Both targetSdkVersion and APP_PLATFORM must be greater than or equal to minSdkVersion." This is incorrect. APP_PLATFORM can be as low as you like. There's nothing wrong with having minSdkVersion=16 and APP_PLATFORM=android-10, it's just a bit pointless. Also android-10 does exist and is a valid value for APP_PLATFORM.Conclusive
@reactive-core: "Even one of Google's own, in several messages I found, recommends always using the latest NDK platform." I've certainly never given that advice. Do our docs say that somewhere? I'd be happy to fix them. You should be using the latest NDK, but your NDK API level should be less than or equal to your minSdkVersion.Phatic
C
25
  1. android:minSdkVersion is the minimum OS version that your app expects.

  2. android:targetSdkVersion is essentially the maximum OS version that you've designed your app to work with. Here's an example of how this works. Imagine that you tested your app fine with API 19 and you release your app with android:targetSdkVersion=19. Then Google decides to release API 20 with a change in behavior of some API, but they don't want to change the behavior for old apps (to prevent from breaking them). So when your app starts up, Android sees that your app has targetSdkVersion=19, so it gives you the old API behavior, but if some other app says targetSdkVersion=20, Android will give it the new API behavior.

  3. APP_PLATFORM is the version of the native headers and libraries that the NDK will compile your native code with. If you set APP_PLATFORM to a specific value and you use APIs that are only available in that platform version, then your app will not run properly on older platforms. So APP_PLATFORM is a minimum value. The solution is to use a lower value and not use those newer APIs, or to write code that decides at runtime whether to call the new APIs or not (and probably use dlopen/dlsym).

It seems like in general it doesn't make sense to use an APP_PLATFORM value newer than android:minSdkVersion, unless you're doing some special (like being careful not to call new APIs by checking the version at runtime, plus making sure not to link to new APIs and instead using dlopen/dlsym).

So if you use APP_PLATFORM=13 and you call AMotionEvent_getAxisValue (which is not in earlier platform headers, implying that it isn't available at runtime on earlier platforms), your app will not run on devices with API < 13. The one caveat would be if AMotionEvent_getAxisValue is actually available on older versions, but it just wasn't in the header/library files or it just wasn't documented. But I don't know if that's the case for this particular API (basically, that would require more research and risk analysis of whether you want to depend on something unsupported).

Catie answered 24/2, 2014 at 8:52 Comment(4)
Thanks. But there is another version specifier: What about 4. project.properties 'target=android-xx' ?Mathison
@Mathison That's the one that actually determines which version of the Android library your (Java) code is built against. It's equivalent to APP_PLATFORM but for non-native code. If you don't specify APP_PLATFORM, it defaults to the value set in project.properties.Conclusive
Note that for native code, you don't necessarily have to use new APIs for your code to be incompatible with older Android versions. Just setting APP_PLATFORM too high can result in the compiler deciding to use primitives that don't exist (e.g. if APP_PLATFORM >= 24, calling std::vector::resize maxe the compiler produce code that doesn't work on Lollipop). In short, it's dangerous to ever have APP_PLATFORM higher than minSdkVersion (and since it defaults to the SDK version you're building non-native code for, you should always set it explicitly).Conclusive
Cool example Karu. I included that in a roundup answer: https://mcmap.net/q/610405/-what-is-the-relation-between-app_platform-android-minsdkversion-and-android-targetsdkversionAnderton
M
0

You are right about the minimum. I am not sure what the target should represent, I think it gives some features of the target SDK but also makes sure the app will run with the minimum SDK.

Manassas answered 19/2, 2014 at 17:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.