Skip to main content

Short explanation on Gradle, minSdkVersion, maxSdkVersion, compileSdkVersion and targetSdkVersion in Android

Gradle:

The Android build system compiles app resources and source code, and packages them into APKs that you can test, deploy, sign, and distribute. Android Studio uses Gradle.
The Android plugin for Gradle works with the build toolkit to provide processes and configurable settings that are specific to building and testing Android applications.Gradle and the Android plugin run independent of Android Studio. Gradle architecture is shown below.




Example:
android {
  compileSdkVersion 27
  buildToolsVersion “26.0.2”

  defaultConfig {
    applicationId “com.example.checkyourtargetsdk"
    minSdkVersion 15
    targetSdkVersion 27
    versionCode 1
    versionName “1.0”
  }
}


minSdkVersion:

minSdkVersion is the lower bound for your app. The minSdkVersion is one of the signals the Google Play Store uses to determine which of a user’s devices an app can be installed on.

 your app’s minSdkVersion must be at least as high as your dependencies’ minSdkVersion

When deciding on a minSdkVersion, you should consider the stats on theDashboards, which give you a global look on all devices that visited the Google Play Store in the prior 7 days — that’s your potential audience when putting an app on Google Play .

compileSdkVersion:

compileSdkVersion is your way to tell Gradle what version of the Android SDK to compile your app with.

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.

App can run on device with lower API level if code execution paths do not attempt to invoke any unavailable APIs (except constans which will be simply copied into an app). Value should be always set to the highest stable API level, even if app does not use any API from it. This is due to the fact that some API may be deprecated, stop working. 

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 prior to working through the behavior changes.
targetSdkVersion is a hint for system on which app is running, indicating which API level is app designed for.

maxSdkVersion:

In docs they stated that they are not effective from 2.1 and also in warning they stated that "declaring the attribute can result in your application being removed from users' devices after a system update to a higher API Level.
An integer designating the maximum API Level on which the application is designed to run.
From Doc: In Android 1.5, 1.6, 2.0, and 2.0.1, the system checks the value of this attribute when installing an application and when re-validating the application after a system update. In either case, if the application's maxSdkVersion attribute is lower than the API Level used by the system itself, then the system will not allow the application to be installed. In the case of re-validation after system update, this effectively removes your application from the device.

Conditions when using the Android SDK Version in build.gradle


- There is also a restriction that minSdkVersion of given module cannot be lower than the value in any dependent module 
eg:  you cannot use version 7.5.0 of Google Play Services library (which minSdkVersion is 9) in an app which has that value set to 8

targetSdkVersion should not be set to value higher than compileSdkVersion 

relationship between the three values
minSdkVersion <= targetSdkVersion <= compileSdkVersion

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

This intuitively makes sense — if compileSdkVersion is your ‘maximum’ and minSdkVersion is your ‘minimum’ then your maximum must be at least as high as your minimum and the target must be somewhere in between.


You’ll hit the biggest audience with a low minSdkVersion and look and act the best by targeting and compiling with the latest SDK — a great way to better apps.

AndroidManifest.xml, you’d see a tag such as:

<uses-sdk android:targetSdkVersion=”27" android:minSdkVersion=”7" />

it’ll be ignored when you build with Gradle (although other build systems might certainly rely on it being there).

Reference:



Comments

Post a Comment

Popular posts from this blog

Google re-branded the support Android libraries to AndroidX

It is important to note, you cannot mix AppCompat and Jetpack in the same project. You must convert everything to use Jetpack if you want to upgrade. The support library artifacts are being deprecated and all future development is going into AndroidX , so there's no avoiding this migration. Alan Viverette sums this up nicely: “There won’t be a 29.0.0, so Android Q APIs will only be in AndroidX” The stable release of 28.0.0 will be the final feature release packaged as android.support . All subsequent feature releases will only be made available as androidx-packaged artifacts. Below tips will give you a clearer transition path. The current version of AppCompat (v28.x) is exactly the same as AndroidX (v1.x). In fact, the AppCompat libraries are machine generated by changing maven coordinates and package names of the AndroidX codebase. For example, android.support.v7.app.AppCompatActivity is now androidx.appcompat.app.AppCompatActivity For a complete listi

Android Beginners Guide

                                                                                                               Android Operation System: Android is an operating system based on Linux with a Java programming interface. It provides tools, e.g. a compiler, debugger and a device emulator as well as its own Java Virtual machine (Dalvik Virtual Machine - DVM). Android is created by the Open Handset Alliance which is lead by Google. Android uses a special virtual machine, e.g. the Dalvik Virtual Machine. Dalvik uses special bytecode. Therefore you cannot run standard Java bytecode on Android. Android provides a tool "dx" which allows to convert Java Class files into "dex" (Dalvik Executable) files. Android applications are packed into an .apk (Android Package) file by the program "aapt" (Android Asset Packaging Tool) To simplify development Google provides the Android Development Tools (ADT) for Eclipse. The ADT performs automatically the conversion f

Android Pi migration(28 API support)

I am here to give some inputs on android PI migration. .  Apps must target at least API level 28 to ensure it is built on the latest APIs optimized for security and performance . From August 2019, new apps must target at least Android 9.0 (API level 28) . The objective here is to make sure that your existing app works as-is on Android 9. Because some platform changes might affect the way your app behaves, some adjustments might be necessary, but you do not need to use new APIs or change your targetSdkVersion. Android Service Problem On Oreo, startService() will throw IllegalStateException. This can be fixed by changing it to startForegroundService(). but it also stops the service immediately if you don’t bind to the Notification with Service.startForeground() within 5 seconds. For other background service we decided to use the WorkManager . Implementation: Since we have minSdkVersion 19, We migrated to workmanger for scheduled tasks and we still use JobIntentSer