Tobias
Tobias

Reputation: 5433

What is the difference between "min SDK version", "target SDK version" and "compile SDK" version?

What are the differences between "min sdk version", "target sdk version" and "compile sdk version"? I know what "min sdk version" and "target sdk version" means but what does "compile sdk version" mean?

In Eclipse, I have "min sdk version" and "target sdk version" set but in Android Studio there is also "compile sdk version".

Upvotes: 383

Views: 216076

Answers (7)

qix
qix

Reputation: 7902

Lots of good explanations in earlier answers, but none link to the official docs. If curious, see https://developer.android.com/guide/topics/manifest/uses-sdk-element for:

  • minSdkVersion:

The Android system will prevent the user from installing the application if the system's API Level is lower than the value specified in this attribute.

Aside: if you use the NDK to run native code, minSdkVersion also impacts the NDK's API availability. (https://developer.android.com/ndk/guides/sdk-versions)

  • targetSdkVersion:

This attribute informs the system that you have tested against the target version and the system should not enable any compatibility behaviors to maintain your app's forward-compatibility with the target version. The application is still able to run on older versions (down to minSdkVersion).

As Android evolves with each new version, some behaviors and even appearances might change. 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. You can disable such compatibility behaviors by specifying targetSdkVersion to match the API level of the platform on which it's running. 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 and also disables screen compatibility mode when running on larger screens (because support for API level 11 implicitly supports larger screens).

So the assumption is that you developed the app with the target API in mind, and have tested that everything looks/behaves as you expected, esp if you're trying to use features introduced in this API. Furthermore, your code should be able to handle platforms that don't have that new feature (down to your minSdkVersion, e.g. checking your code handles missing APIs that you use gracefully, etc). But even newer Android versions may do things to keep your app running, which might otherwise break or look even funkier if the OS didn't enable "compatibility behaviors".

See https://developer.android.com/studio/build for:

  • compileSdkVersion:

compileSdkVersion specifies the Android API level Gradle should use to compile your app. This means your app can use the API features included in this API level and lower.

Ideally you'd set target & compile versions the same to the highest release, and of course you don't have to use any of the new features. But you may wish to keep your target at an older version you've already released, while using a newer compile version for better warnings/errors, until you're ready to update the target version. In the past it also let one use newer Java language features in their code with an Android Gradle plugin upgrade, independent of the target Android APIs.


Lastly, don't forget about Google's more recent target API level requirements, which basically require you to release a build targeting up-to-date API levels by a certain date if you want to still be available to users on the Play store that use an OS newer than your target API. This is to motivate the app dev community make available newer performance/security enhancements (like giving the user more privacy options when you request location info).

Every version of Android released since 9 lists behavior changes that will impact all apps regardless of your targetSdkVersion (e.g. here's Android 12's), and what changes when you specifically target it (e.g. Behavior changes: Apps targeting Android 12. When the next version is in preview is a good time to start checking your app's compatibility with the upcoming release, even if it's just that any compat modes are ok without changing your compileSdkVersion, if you aren't prepping to target it yet. The Compatibility framework tools can help with that and in the migration to using new APIs.

Upvotes: 10

yoAlex5
yoAlex5

Reputation: 34175

Android minSdkVersion, targetSdkVersion, compileSdkVersion

The formula is

minSdkVersion <= targetSdkVersion <= compileSdkVersion

minSdkVersion - is a marker that defines a minimum Android version on which the application will be able to install. Also, it is used by Lint to prevent calling API that doesn’t exist. Also, it has an impact on Build Time. So you can use build flavors to override minSdkVersion to the maximum during the development. It will help to make the build faster using all improvements that the Android team provides for us. For example, some features of Java 8 are available only when you are using specific versions of minSdkVersion.

targetSdkVersion - If AndroidOS version is >= targetSdkVersion it says Android system to turn on version specific features. *Please note that some of these new behaviors will be turned on by default even if thought targetSdkVersion is <, you should read the official documentation.

For example:

  • Starting in Android 6.0 (API level 23) Runtime Permissions were introduced. If you set targetSdkVersion to 22 or lower your application does not ask a user for some permission in run time.

  • Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel or it will not appear. On devices running Android 7.1 (API level 25) and lower, users can manage notifications on a per-app basis only (effectively each app only has one channel on Android 7.1 and lower).

  • Starting in Android 9 (API level 28), Web-based data directories separated by process. If targetSdkVersion is 28+ and you create several WebView in different processes you will get java.lang.RuntimeException

compileSdkVersion - actually it is the SDK Platform version and tells Gradle which Android SDK uses to compile. When you want to use new features or debug .java files from Android SDK you should take care of compileSdkVersion. One more example is using AndroidX that forces to use compileSdkVersion - level 28. compileSdkVersion is not included in your APK: it is purely used at compile time. Changing your compileSdkVersion does not change runtime behavior. It can generate for example new compiler warnings/errors. Therefore it is strongly recommended that you always compile with the latest SDK. You’ll get all the benefits of new compilation checks on existing code, avoid newly deprecated APIs, and be ready to use new APIs. One more fact is compileSdkVersion >= Support Library version

You can read more about it here. Also, I would recommend you to take a look at the example of migration to Android 8.0.

[buildToolsVersion]

Upvotes: 291

Ajay Vishwakarma
Ajay Vishwakarma

Reputation: 327

Here is the little clear and easy way to understand -

minSdkVersion should be lower to target the max coverage of android devices on which the app will be installed.

compileSdkVersion is required while developing the app to use the latest and optimize APIs of android.

tarketSkdVersion is the latest/version of android OS on which you want to run your app to achieve the full optimization of android resources.

Note- please correct me if there is a mistake. thanks

Upvotes: 0

Narendra_Nath
Narendra_Nath

Reputation: 5173

Reference- Medium Article by Paulina Sadowska

  1. 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, there was a new API introduced, that allows us to easily implement a splash screen. In this new API, the splash screen can be customized using those properties:

If you want to use that API in your app you first have to:

i)  download SDK version 31 in Android Studio,
ii) and then: update compileSdkVersion to 31 in your app.

Only then you can see these new properties. And only then you can use this new splash screen API in your code.

2.targetSdkVersion is a property that tells the system for which Android version the app was designed and tested on.

If the user runs your app on a device with an android version that is higher than the targetSdkVersion defined in your app, for new android features, the system may introduce some backward-compatibility behavior to ensure your app still looks and works in a way that you designed it.

For example:

In Android 12 the appearance of custom notifications was changed. Previously they could use the whole notification area, but in Android 12 system applies the standard template to all custom notifications so they look more consistent. 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.

Upvotes: 11

Matt
Matt

Reputation: 5684

The min sdk version is the earliest release of the Android SDK that your application can run on. Usually this is because of a problem with the earlier APIs, lacking functionality, or some other behavioural issue.

The target sdk version is the version your application was targeted to run on. Ideally, this is because of some sort of optimal run conditions. If you were to "make your app for version 19", this is where that would be specified. It may run on earlier or later releases, but this is what you were aiming for. This is mostly to indicate how current your application is for use in the marketplace, etc.

The compile sdk version is the version of android your IDE (or other means of compiling I suppose) uses to make your app when you publish a .apk file. This is useful for testing your application as it is a common need to compile your app as you develop it. As this will be the version to compile to an APK, it will naturally be the version of your release. Likewise, it is advisable to have this match your target sdk version.

Upvotes: 360

Vinay John
Vinay John

Reputation: 1029

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.

minSdkVersion : The min sdk version is the minimum version of the Android operating system required to run your application.

targetSdkVersion : The target sdk version is the version your app is targeted to run on.

Upvotes: 21

anuraagy
anuraagy

Reputation: 1479

The min sdk version is the minimum version of the Android operating system required to run your application.

The target sdk version is the version of Android that your app was created to run on.

The compile sdk version is the the version of Android that the build tools uses to compile and build the application in order to release, run, or debug.

Usually the compile sdk version and the target sdk version are the same.

Upvotes: 101

Related Questions