How does my app work when targetSdk is higher than compile sdk

190 Views Asked by At

I would think that to target an sdk, the compile sdk would have to be at or higher than the target sdk. Can someone please help me understand what I am missing?

I recently changed the target sdk to be higher than the compile sdk and the app still worked. I do not understand how this is possible.

1

There are 1 best solutions below

2
cactustictacs On

Here's a long post from one of the Android devs explaining the whole thing, what you should and shouldn't change, and why!

There's this little formula about the relationship between the different numbers:

minSdkVersion <= targetSdkVersion <= compileSdkVersion

But I think the point there is this is how it should be. The minSdk acts as a minimum API level, and compileSdk is the version of the API that the app is actually compiled against, making it an effective maximum "included" API level (can't add features that aren't in it yet!).

targetSdk is meant to act as a control for features - you can (and should) compile against the most recent API for bug fixes etc, but actual behaviour changes can be restricted by limiting targetSdk to an earlier API level:

targetSdkVersion

The most interesting of the three, however, is targetSdkVersion. targetSdkVersion is the main way Android provides forward compatibility by not applying behavior changes unless the targetSdkVersion is updated. This allows you to use new APIs (as you did update your compileSdkVersion right?) prior to working through the behavior changes.

So really it works like "don't enable any features from APIs higher than this". In which case, it doesn't really make sense to set it higher than compileSdk - there aren't any extra features available beyond the API level you're compiling against. Plus you shouldn't be raising targetSdk without testing against that API level - which you obviously haven't, if you're not compiling against that API!

So I guess there's nothing stopping you from setting targetSdk at whatever level you want, it's really compileSdk setting that upper limit on what actually gets into the app. There just isn't any good reason to set targetSdk higher than that.