What is the difference between compileSdkVersion and targetSdkVersion?
Asked Answered
S

13

742

I have looked at the documentation for building with Gradle, but I'm still not sure what the difference between compileSdkVersion and targetSdkVersion is.

All it says is:

The `compileSdkVersion` property specifies the compilation target.

Well, what is the "compilation target"?

I see two possible ways to interpret this:

  1. compileSdkVersion is the version of the compiler used in building the app, while targetSdkVersion is the "API level that the application targets". (If this were the case, I'd assume compileSdkVersion must be greater than or equal to the targetSdkVersion?
  2. They mean the same thing. "compilation target" == "the API level that the application targets"
  3. Something else?

I see that this question has been asked before, but the one answer just quotes the doc, which is what is unclear to me.

Stodgy answered 1/11, 2014 at 22:54 Comment(3)
More details on: Picking your compileSdkVersion, minSdkVersion, and targetSdkVersionFeeding
targetSdkVersion is what your device is running. So if your devices are running lower than Oreo, then don't target 27.Droughty
Does this answer your question? What is the difference between min SDK version/target SDK version vs. compile SDK version?Sitdown
I
735

compileSdkVersion

The compileSdkVersion is the version of the API the app is compiled against. This means you can use Android API features included in that version of the API (as well as all previous versions, obviously). If you try and use API 16 features but set compileSdkVersion to 15, you will get a compilation error. If you set compileSdkVersion to 16 you can still run the app on a API 15 device as long as your app's execution paths do not attempt to invoke any APIs specific to API 16.

targetSdkVersion

The targetSdkVersion has nothing to do with how your app is compiled or what APIs you can utilize. The targetSdkVersion is supposed to indicate that you have tested your app on (presumably up to and including) the version you specify. This is more like a certification or sign off you are giving the Android OS as a hint to how it should handle your app in terms of OS features.

For example, as the documentation states:

For example, setting this value to "11" or higher allows the system to apply a new default theme (Holo) to your app when running on Android 3.0 or higher...

The Android OS, at runtime, may change how your app is stylized or otherwise executed in the context of the OS based on this value. There are a few other known examples that are influenced by this value and that list is likely to only increase over time.

For all practical purposes, most apps are going to want to set targetSdkVersion to the latest released version of the API. This will ensure your app looks as good as possible on the most recent Android devices. If you do not specify the targetSdkVersion, it defaults to the minSdkVersion.

Imperative answered 1/11, 2014 at 23:17 Comment(21)
I see. So the targetSdkVersion should never be greater than the compileSdkVersion -- is that correct?Stodgy
No, targetSdkVersion very likely will be higher than compileSdkVersion and rightfully so. This means that although you designed an app to target API 16, for example, it still runs fine on API 21 (Lollipop) and you should bump your targetSdkVersion to 21 to indicate it is okay for the Android OS to apply any Lollipop-styles that may exist to your app.Imperative
You said earlier: "If you try and use API 16 features but set compileSdkVersion to 15, you will get a compilation error." In your second comment, it sounds like you're saying compileSdkVersion can be 15, and targetSdkVersion can be (and very likely is) higher than 15, say 16. Does that not mean I've "certified" the app to work with API 16 and its features?Stodgy
Fundamentally, I don't understand how you could target an SDK higher than the the SDK you've compiled against.Stodgy
Changing compileSdkVersion to a higher version would mean you want to use some new APIs that are only included in that particular release. If you don't plan on using any Lollipop-specific features in your app, then there's really (usually) no reason to ever set compileSdkVersion to 21. However, your app will likely run just fine on API 21 as-is, thus you change targetSdkVersion to indicate your app runs like you expect (target) on API 21, but you're not using any APIs specific to 21 (compile) and thus your compileSdkVersion can stay at 15 in this example.Imperative
A warning is reported when I do that in Android studio. I have "compileSdkVersion 17" and "targetSdkVersion 22" and it tells me "targetSdkVersion should not be higher than compileSdkVersion". Oh, just changed it and now it's telling me the targetSdkVersion is not the latest 22 and that compatibility mode might kick in. Sigh.Breana
Many developers are gonna want to set compileSdkVersion 23 but stay with targetSdkVersion 22 for a while. (See @jeff-mixon comment for the reason.)Underbody
I still dont 100% get what compileSdkVersion means. Lets say I have an app which I build on API Level 21. Then Google releases API 23. I compile my App against compileSdkVersion 23 BUT did not change anything besides that in my code. Will my app use the 'new' code from API 23? I am asking because Im curious what happens with bug fixes: Lets say there was a method DoStuff() which I use in my APP. Google noticed that it contains a bug and they fixed it for API 23. When I compile with compileSdkVersion 23 does my App get that bugfix? Will that code from that SDK be compiled into my app?Tazza
@Tazza compileSdkVersion only affects compilation time. The API level of the device determines which version of Android it is ultimately running against. An app with an compileSdkVersion of 11 running on a device with API 21 will run version 21 of all the Android APIs. compileSdkVersion is purely for the compiler; it does not influence anything after your app is already built.Imperative
It clearly states in android studio when you set the targetSdkVersion higher than compileSdkVersion.. "The targetSdkVersion should not be higher than the compileSdkVersion"Alexander
This answer contradicts what the Android Studio says. targetSdkVersion matters and it should less than or equal to compileSdkVersionOttar
That is just a Gradle inspection hint for convenience. There is no actual technical issue with setting targetSdkVersion higher than compileSdkVersion. At the same time, it doesn't really hurt to also set compileSdkVersion to the highest level either, as long as you understand the implications.Imperative
"Note that if you use the Support Library, compiling with the latest SDK is a requirement for using the latest Support Library releases." source: medium.com/google-developers/…Groove
I did not understand this part of your answer: "as long as your app's execution paths do not attempt to invoke any APIs specific to API 16". Is it safe to set compileSdkVersion to 16 run the app on an API 15 device?Rootless
The most important issue is - is it compileSdkVersion or targetSdkVersion that can never be downgraded in google play?Wilkey
"This means you can use Android API features included in that version of the API (as well as all previous versions, obviously)." I don't think it is correct that you can use all feature included in previous versions. If a method was present (but deprecated) in 15, then removed in 16, you can't use it if the compileSdkVersion is set to 16 or greater.Kalsomine
In Android Studio, if I set compileSdkVersion lower than targetSdkVersion then AS tells me this is not allowed, so it seems to me you should not set compiled < target as mentioned above... (@Jeff Mixon ?)Negation
I struggle with the phrase "compiled against". To me it sounds like it means, "the byte code coming out of the compiler is this version". But it seems to be used as "the version of the compiler used is this version". That totally changes the meaning. I'd imagine a newer compiler can generate back-compatible byte code, but newer byte code cannot run on an older OS.Saiga
with this explanation, compileSdkVersion should always be set to min sdk or there is chance of breaking ?Related
the targetSdkVersion should not be higher than the compileSdkVersion. compileSdkVersion should NOT be set depending on the minSdkVersion but depending on the targetSdkVersion. If your targetSdkVersion and the minSdkVersion are the same, then obviously the compileSdkVersion should also be the same. There isn't any chance of breaking, but the compileSdkVersion says what featues are possible so....Keefe
Any features in higher SDKs than the compileSDK cant be used. So setting your targetSdkVersion above your compileSdkVersion is like saying: "This is built for [targetSdkVersion], but you only get the features from [compileSdkVersion]!"Keefe
T
241

As a oneliner guide:

minSdkVersion <= targetSdkVersion <= compileSdkVersion

Ideally:

minSdkVersion (lowest possible) <= targetSdkVersion == compileSdkVersion (latest SDK)

Read more from this great post by Ian Lake

P.S. And as clipped by @sshturma in answer below:

Tearing answered 15/6, 2016 at 19:3 Comment(8)
Does minSdkVersion means the lowest device api level app can run on? Presumably because it uses certain API's available from minSdkVersion onwards?Irritability
@NitinBansal yes. For example if minSdkVersion is 15 (which is ICS 4.0.3), devices with API 14 (which is ICS 4.0) should not be able to install the app. And at least for now, the app will run on 15, 16, 17, 18, 19, (20 but that's for the old wear os), 21, 22, 23, 24, 25, 26, 27, 28, and so on in the future (probably)Spectrograph
compileSdkVersion can be less than targetSdkVersionPomfret
@Pomfret care to elaborate why?Tearing
@JimmyKane Not sure if you already saw this, but this comment sort of explains why you would maybe want to do that (not saying I would recommend it, just relaying info).Tarra
Updating compileSdkVersion allows your app to use newer Android APIs. Updating targetSdkVersion opts your app in to new runtime behaviour. Updating minSdkVersion cuts off older devices from being able to install your app.Asturias
In my Cordova app, both compileSdkVersion and targetSdkVersion were set to 32. I changed the targetSdkVersion to 33 and recompiled the app. During the compile I get a warning from the Cordova CLI: The "android-compileSdkVersion" (32) should be greater than or equal to the "android-targetSdkVersion" (33). That being said, the app compiled fine and launched without issue. So far, all is good.Kutchins
@Kutchins isn't that what the answer says ?Tearing
S
63

Late to the game.. and there are several great answers above-- essentially, that the compileSdkVersion is the version of the API the app is compiled against, while the targetSdkVersion indicates the version that the app was tested against.

I'd like to supplement those answers with the following notes:

  1. That targetSdkVersion impacts the way in which permissions are requested:
  • If the device is running Android 6.0 (API level 23) or higher, and the app's targetSdkVersion is 23 or higher, the app requests permissions from the user at run-time.
  • If the device is running Android 5.1 (API level 22) or lower, or the app's targetSdkVersion is 22 or lower, the system asks the user to grant the permissions when the user installs the app.
  1. If the compileSdkVersion is higher than the version declared by your app's targetSdkVersion, the system may enable compatibility behaviors to ensure that your app continues to work the way you expect. (ref)

  2. With each new Android release...

  • targetSdkVersion should be incremented to match the latest API level, then thoroughly test your application on the corresponding platform version
  • compileSdkVersion, on the other hand, does not need to be changed unless you're adding features exclusive to the new platform version
  • As a result, while targetSdkVersion is often (initially) less than than the compileSdkVersion, it's not uncommon to see a well-maintained/established app with targetSdkVersion > compileSdkVersion
Scissure answered 7/1, 2017 at 10:48 Comment(2)
Re: your second point, I don't think the references doc explicitly says that. It says "However, if the API level of the platform is higher than the version declared by your app's targetSdkVersion, the system may enable compatibility behaviors to ensure that your app continues to work the way you expect." I think this means if the API level of the device you are running on is newer than your targetSdkVersion you may see compatibility behaviors. I don't believe it has anything to do with the compileSdkVersion.Iden
If the compileSdkVersion is 23 and targetSdkVersion is 22, would it show permission dialog in the marshmallow and above devices?Antitrust
T
39

The compileSdkVersion should be newest stable version. The targetSdkVersion should be fully tested and less or equal to compileSdkVersion.

Torticollis answered 8/10, 2015 at 15:37 Comment(3)
Any specific reason for saying that targetSdkVersion be less than compileSdkVersion? I believe it's a wrong statementWeisburgh
I guess the point is that the last version is backwards compatible, so the latest API version can "behave" like older ones, if you set the targetSdkVersion to a lower one. So the targetSdkVersion should be the one you have tested and know the exact behavior, and can be <= the latest stable.Winters
I think that your statement 'compileSdkVersion should be newest stable version' should be suffixed with 'of which you use API features'. It makes no sense to compile against API 27 (today's latest stable API) if you only use lower API version features. However, the latest stable version could include some features that automatically become better, e.g. enhanced security or efficient compilation with backwards compatibility. Therefore it is advisable to use the latest or at least a recent stable version, but it 'should [not] be' the latest version per se.Diocesan
S
24

The CompileSdkVersion is the version of the SDK platform your app works with for compilation, etc DURING the development process (you should always use the latest) This is shipped with the API version you are using

enter image description here

You will see this in your build.gradle file:

enter image description here

targetSdkVersion: contains the info your app ships with AFTER the development process to the app store that allows it to TARGET the SPECIFIED version of the Android platform. Depending on the functionality of your app, it can target API versions lower than the current.For instance, you can target API 18 even if the current version is 23.

Take a good look at this official Google page.

Steere answered 18/4, 2016 at 20:24 Comment(1)
I believe this answer is misleading. BuildToolsVersion specifies the version of the compilers and other build tools that you use for compilation. CompileSdkVersion specifies the version of sdk features your app uses (i.e. the app is built using no SDK features beyond CompileSdkVersion), and targetSdkVersion specifies the version you have tested your app against. MinSdkversion specifies the minimum version sdk that this app is allowed to be installed on. It's buildToolsVersion that that you generally want to be the latest, not compileSdkVersion.Naturalistic
B
14

I see a lot of differences about compiledSdkVersion in previous answers, so I'll try to clarify a bit here, following android's web page.

A - What Android says

According https://developer.android.com/guide/topics/manifest/uses-sdk-element.html:

Selecting a platform version and API Level When you are developing your application, you will need to choose the platform version against which you will compile the application. In general, you should compile your application against the lowest possible version of the platform that your application can support.

So, this would be the right order according to Android:

compiledSdkVersion = minSdkVersion <= targetSdkVersion

B - What others also say

Some people prefer to always use the highest compiledSkdVersion available. It is because they will rely on code hints to check if they are using newer API features than minSdkVersion, thus either changing the code to not use them or checking the user API version at runtime to conditionally use them with fallbacks for older API versions.

Hints about deprecated uses would also appear in code, letting you know that something is deprecated in newer API levels, so you can react accordingly if you wish.

So, this would be the right order according to others:

minSdkVersion <= targetSdkVersion <= compiledSdkVersion (highest possible)

What to do?

It depends on you and your app.

If you plan to offer different API features according to the API level of the user at runtime, use option B. You'll get hints about the features you use while coding. Just make sure you never use newer API features than minSdkVersion without checking user API level at runtime, otherwise your app will crash. This approach also has the benefit of learning what's new and what's old while coding.

If you already know what's new or old and you are developing a one time app that for sure will never be updated, or you are sure you are not going to offer new API features conditionally, then use option A. You won't get bothered with deprecated hints and you will never be able to use newer API features even if you're tempted to do it.

Banditry answered 12/4, 2017 at 10:22 Comment(3)
I don't think the Android advice is different. There's a difference between "compile your application against the lowest possible version" and compiling with a particular SDK version. You should generally compile (compileSdkVersion) with the latest version, set your min (minSdkVersion) as low as possible and set your target (targetSdkVersion) as high as possible subject to testing or other compatibility issues.Manpower
Good point @Caltor. I wish they would update that document to clarify the difference. The <uses-sdk> documentation is extremely vague and ambiguous.Iden
Practically scenario A can be true only if TargetSdkVersion = CompileSdkVersion but not higher, when you chose a higher value Android Studio will consider it as an error and request from you to chose a value that is equal or lower than the ComplieSdkVersionImpurity
U
5

compileSdkVersion : compileSdkVersion defines which Android SDK version will be used by gradle to compile your app.

For example: In Android 12, so in SDK version 31, new splash screen API was introduced, that allows us to easily implement a splash screen. If you want to use that API in your app you have to update compileSdkVersion to 31 in your app. Only then you can use this new splash screen API in your code otherwise you will get a compilation error.

That doesn’t of course mean that you can use only this new API and forget about users who have older Android versions where this API is not available. You have to also provide an alternative way to display a splash screen for older devices by using version check condition.

*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {

// New Splash screen API

} else {

// Old splash screen approach

}*

Similarly, some methods or properties could be deprecated in this Android SDK version, and some of them even removed. That's why once you update the compiledSdkVersion in your app, you will often see some warnings and errors during compilation.

But changing the compileSdkVersion alone does not actually change the behaviour of the app when running on android device. So how does the Android system know if it can use new functionalities with your app or not? That’s where targetSdkVersion comes into play.

targetSdkVersion : targetSdkVersion tells the system for which Android version the app was designed and tested on.

For example: In Android 12 the appearance of custom notifications was changed. If your targetSdkVersion is below 31 system will assume that you haven’t tested that feature and will display notifications in the old way to minimize the risk that notification will not be displayed properly. Only after you update the target SDK version to 31 the new notification appearance will be used.

Not all changes that are introduced in new Android versions are targeted and use these backwards-compatibility mechanisms. For each Android version release, in documentation, you can see that changes are split into two groups:

-> behavioral changes for apps targeting specific Android versions

-> and changes for all apps, no matter which targetSdkVersion they define.

An example of the latter may be the introduction of one time permissions in Android 11. When the device uses Android version 11 or higher and the app asks for location permission, the user can grant temporary access to that data and the app has to handle that case properly no matter if it targets SDK version 30 or not.

Relationship between compile and target SDK versions:

-> targetSdkVersion cannot be higher than the compileSdkVersion simply because we cannot target things that we know nothing about during compilation.

-> Ideally, the compileSdkVersion and targetSdkVersion should be equal and both point to the latest SDK.

Ultimo answered 23/8, 2023 at 7:6 Comment(0)
A
4

My 2 cents: Compile against any version of the SDK but take care not to call any APIs that your "minimum SDK version" does not support. That means you "could" compile against the latest version of the SDK.

As for "target version" it simply refers to what you planned to target in the first place and have possibly tested against. If you haven't done the due diligence then this is the way to inform Android that it needs to perform some additional checks before it deploys your lets say "Lollipop" targeted app on "Oreo".

So the "target version" is obviously not lower than your "minimum SDK version" but it can't be higher than your "compiled version".

Albarran answered 16/11, 2017 at 2:51 Comment(1)
"target version" is obviously not lower than your "minimum SDK version"Sundin
B
4

compiledSdkVersion==> which version of SDK should compile your code to bytecode(it uses in development environment) point: it's better use last version of SDK.

minSdkVersion==> these item uses for installation of APK(it uses in production environment). For example:

if(client-sdk-version   <   min-sdk-versoin )
    client-can-not-install-apk;
else
    client-can-install-apk;
Brotherson answered 16/4, 2019 at 20:32 Comment(0)
I
3

Not answering to your direct questions, since there are already a lot of detailed answers, but it's worth mentioning, that to the contrary of Android documentation, Android Studio is suggesting to use the same version for compileSDKVersion and targetSDKVersion.

enter image description here

Impervious answered 12/2, 2018 at 3:3 Comment(0)
J
2

compileSdkVersion

compileSdkVersion specifies the version of the Android SDK that your code will be compiled against. This means that your app can use all the APIs included in the specified version and any previous versions.

For example, If you set the compileSdkVersion to 30, it means that your app can use all the APIs that were introduced up to version 30. For example, if you want to use the Camera2 API, which was introduced in Android 5.0 (API level 21) and above, and you have set your compileSdkVersion to 30, your app can use this API without any issue. However, if you try to use an API that was introduced in Android 31 (which is not yet released), you will get a compile-time error because that API is not yet available in the compileSdkVersion that you have set.

targetSdkVersion

targetSdkVersion This is the version of the Android SDK that the app is designed to run on. It specifies the API level that the app was tested against and is intended to run on, and also indicates the level of compatibility that the app provides with newer versions of Android.

For example, if you set the targetSdkVersion to 30, you are indicating that your app is designed to run on devices running Android 30 or higher. This means that you have tested your app against the APIs introduced in version 30 and made sure that your app behaves correctly on those devices. However, this also means that your app may not take advantage of the new features and APIs introduced in higher versions of Android unless you explicitly code for them.

Summary

In summary, the compileSdkVersion determines the set of APIs that can be used during the build process, while the targetSdkVersion specifies the API level that the app is designed to run on and indicates the level of compatibility that the app provides with newer versions of Android.

Ideally, the compileSdkVersion and targetSdkVersion should be equal and both point to the latest SDK. But of course only after you test that every change introduced in that version works smoothly with your app!

Jollenta answered 13/5, 2023 at 8:0 Comment(0)
F
1

The Application settings of an Android project's properties in Visual Studio 2017 (15.8.5) has them combined:

enter image description here

Furnivall answered 21/9, 2018 at 12:50 Comment(0)
E
1

Quick summary:

For minSDKversion, see latest entry in twitter handle: https://twitter.com/minSdkVersion

TargetSDKversion: see latest entry in twitter handle: https://twitter.com/targtSdkVersion or use the latest API level as indicated at devel https://developer.android.com/guide/topics/manifest/uses-sdk-element.html

Compiled version: make it same as TargetSDKversion

maxSdkVersion: advice from Android is to not set this as you do not want to limit your app to not perform on future android releases

Epperson answered 8/7, 2019 at 5:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.