HomeAndroidminSdk vs targetSdk vs compileSdk

minSdk vs targetSdk vs compileSdk


Let’s take a look at what minSdk, targetSdk and compileSdk actually means in construct.gradle script.

android {
    compileSdk 32
    defaultConfig {
        
        minSdk 21        
        targetSdk 32        
         
    }
}
  • minSdk – What’s the minimal API Stage required for the app to run?

  • targetSdk – Which API stage the app was designed and examined on?

  • compileSdk – Which API stage is utilized by Gradle to compile your app?

minSdk

In case your minSdk is ready to 21, your app can’t be run on any Android model that’s beneath API stage 21. If the Android model (API stage 20) makes an attempt to put in your app, you get this error.

Set up didn’t succeed. The appliance couldn’t be put in: INSTALL_FAILED_OLDER_SDK

The Google Play Retailer prevents the person from putting in the app too if the cellphone’s Android model would not meet the minSdk requirement by the app.

targetSdk

App runs on API stage > targetSdk

If the app is run on the Android model (API stage) that’s increased than the targetSdk, the Android working system will attempt to run backward compatibility conduct to match conduct as in targetSdk API stage.

For instance, runtime app permission is launched in API stage 23. Earlier than API stage 23, runtime app permission is just not wanted.

In case your targetSdk is ready to 22 and your app runs on Android model (API stage 23), the Android OS will attempt to match the conduct as in API stage 22. Thus, no runtime permission is requested.

In case your targetSdk is ready to 23 and your app runs on Android model (API stage 23 or increased), runtime permission is requested.

App runs on API stage < targetSdk

What concerning the app that runs on the Android model (API stage) that’s < targetSdk? The app behaves primarily based on that Android model (API stage).

In case your targetSdk is ready to 23 and your app runs on Android model (API stage 22), runtime permission is just not requested.

You may attempt to mess around with this demo app which requests runtime permission

compileSdk

In case your app makes use of an API that’s launched in API stage 26, your compileSdk should be set to minimal 26. Should you set it to 25, it fails to compile.

Assuming the minSdk is 21, you doubtless get this warning/error too

Name requires API stage 26 (present min is 21): android.app.NotificationChannel()

As really useful by the Android Studio IDE, you may repair the warning/error by annotating your perform with

@RequiresApi(Construct.VERSION_CODES.O)
personal enjoyable yourFunction() {
     
}

Nevertheless, in case you run your App on Android model (< API stage 26), it can NOT crash however fails silently. You’ll discover the error messages within the logcat. So, I believe including the @RequiresApi() is a foul follow right here.

What it is best to do is deal with the code above the app runs beneath the API stage 26.

if (Construct.VERSION.SDK_INT >= Construct.VERSION_CODES.O) {
    
} else {
   
}

An instance is the notification channel is barely required / obtainable for API stage 26. Then, we must always wrap it with Construct.VERSION_CODES.O.

if (Construct.VERSION.SDK_INT >= Construct.VERSION_CODES.O) {

    val notificationChannel = NotificationChannel(
        notificationChannelId,
        "DemoWorker",
        NotificationManager.IMPORTANCE_DEFAULT,
    )

    val notificationManager: NotificationManager? =
        getSystemService(applicationContext, NotificationManager::class.java)

    notificationManager?.createNotificationChannel(notificationChannel)
}

This code is taken from the work supervisor demo app beneath:

[Updated – Sept 16, 2022]: I discover that utilizing Construct.VERSION_CODES.O is just not readable in any respect. It would not convey any which means as a result of in every single place else (e.g. warning messages from Android Studio, construct.gradle) is utilizing the quantity(i.e. 26). I do not know the O represents the 26 and I can not memorize it. As a substitute, I want to make use of the hard-coded worth 26!

if (Construct.VERSION.SDK_INT >= 26) {
    
} else {
   
}

[Updated – Jan 20, 2023]: I nonetheless discover hardcoding worth 26 is just not very readable. So I created this BuildUtils Android library not too long ago to make this extra readable.

if (BuildExt.VERSION.isNotificationChannelSupported()) {
    
}

Conclusion

On the whole,

minSdk < targetSdk <= compileSdk

however ideally and virtually,

minSdk < targetSdk == compileSdk == newest SDK model

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments