Flutter Change Android minSdkVersion After Update


With the recent 2.8 release how and where the minSdkVersion is specified has changed. It took a while to figure out so I thought I would share this recent tip.

- January 17, 2022

Rest of the Story:

With the 2.8 release where the minimum and target SDK versions is set/established has changed within the Flutter project. How to change Android minSdkVersion in flutter project? There are a couple approaches provided below.

  1. You can change the minSdkVersion directly in the file Project_Name/android/app/build.gradle , defaultconfig

        defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.projectname"
        minSdkVersion 16 // <--- There
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}
    
  2. The more appropriate way and maintainable approach is a little more involved however I would recommend.

    For clarity I will provide what the build.gradle looked like before and after the 2.8 update.

        android {
            compileSdkVersion 30
        
        defaultConfig {
            applicationId "com.example.app"
            minSdkVersion 21
            targetSdkVersion 30
            versionCode flutterVersionCode.toInteger()
            versionName flutterVersionName
            multiDexEnabled true
        }        
    

    After updating to Flutter 2.8:

        android {
            compileSdkVersion flutter.compileSdkVersion
        
        defaultConfig {
                applicationId "com.example.app"
                minSdkVersion flutter.minSdkVersion
                targetSdkVersion flutter.targetSdkVersion
                versionCode flutterVersionCode.toInteger()
                versionName flutterVersionName
            }
    

So here are the changes so that you can maintain the version(s) within your project.

Update the android -> local.properties adding the following 3 lines. This will be where you set the min and target versions going forward.

    flutter.minSdkVersion=21
    flutter.targetSdkVersion=30
    flutter.compileSdkVersion=30

Add/modify to the android/src/build.gradle file

    android {
        compileSdkVersion localProperties.getProperty('flutter.compileSdkVersion').toInteger()
    ...
    defaultConfig {
        minSdkVersion localProperties.getProperty('flutter.minSdkVersion').toInteger()
        targetSdkVersion localProperties.getProperty('flutter.targetSdkVersion').toInteger()
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

Keep in mind with this appraoch going forward you will maintain minimum and target SDK versions within the local.properties file.