Difference Between Compilesdkversion and Targetsdkversion

What is the difference between compileSdkVersion and targetSdkVersion?

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.

What is the difference between min SDK version/target SDK version vs. compile SDK version?

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.

Should I change both the target SDK and compile SDK to support higher versions?

It's expected that your app will be forward compatible if you don't update your compileSdkVersion. In other words, it's expected to run/ behave the same way on all the versions supported by your minSDKVersion.

For instance, if your minSDKVersion is API 19 your app will run on Android 4.4, Android 8, Android 9 and it's expected to run on Android 10.

Updating your compileSdkVersion it's a good approach to follow because each new version have another year of development on it - it's expected to be more stable and with newer API's/ functionalities that you can use. Sometimes, doing so, means that you'll have to change some of your behaviours (for more information see here on the official documentation).

If you're looking to difference between compileSdkVersion and targetSdkVersion - you can find a really good answer on another StackOverflow thread.

Briefly:

  • compileSdkVersion

The compileSdkVersion is the version of the API the app is compiled against.

  • targetSdkVersion

    The targetSdkVersion is supposed to indicate that you have tested your app on (presumably up to and including) the version you specify.

Android Studio What is the difference between minSdkVersion, targetSdkVersion and minSdk, targetSdk?

There is no such thing named minSdk , targetSdk and compileSdk in build.gradle.
you should config your app compatibility like this :

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "com.app.test"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0.0"
}
}

What is difference between compileSdk and compileSdkVersion in android studio gradle script when using jetpack compose

With the new Android Gradle Plugin 7.0.0 (currently 7.0.0-alpha14) you can use:

  • minSdk instead of minSdkVersion
  • targetSdk instead of targetSdkVersion
  • compileSdk instead of compileSdkVersion

These attributes work with an Int and you can use them with something like:

//minSdkVersion 21
//targetSdkVersion 30
minSdk 21
targetSdk 30

If you want to use a preview version you have to use:

  • minSdkPreview
  • targetSdkPreview
  • compileSdkPreview

These attributes work with a String and setting these values will override previous values of minSdk/targetSdk/compileSdk.

About the String format of the preview versions currently (7.0.0-alpha14) it is not clear. Maybe it will change with 7.0.0-beta01 (you can check this commit) and it should be:

compileSdkPreview = "S"

Does changing the compileSdkVersion affect the behavior of the app at runtime?

Will this have an impact on how the other features of the app behave?

It could only impact on a library, which wants higher API. At runtime you could check version API and skip some operations, but your case inside a box (lib), no way. However, some of classes could be deprecated or even erased in the next releases of android support library.

Iak Lake wrote a good topic about compileSdkVersion vs minSdkVersion vs targetSdkVersion. Here is a link.

targetSDKVersion, minimumSDKVersion, and compileSDKVersion for specific device

Version settings

Among those version settings, only minSdkVersion takes effect to detect API level error. If you'd like to know details of the version settings, see documentation.

How to let build system report API level error

For release builds, place "lintOptions" in app/build.gradle and mark "NewApi" as "fatal". Then lintVitalRelease gradle task reports error during compilation.

android {

lintOptions {
fatal "NewApi"
}

compileSdkVersion 30

defaultConfig {
minSdkVersion 25
targetSdkVersion 30

:
}
:
}

Checking API level manually

In debug builds, the lintOptions above doesn't have any effects because the lint is not included in debug build tasks (there is no lintVitalDebug gradle task). So rather, after coding, run "code inspection" manually to detect all the NewApi problems in your code. If incompatible API uses found, you'll see "calling new methods on older version" error in the inspection result.



Related Topics



Leave a reply



Submit