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 1, 2023

Rest of the Story:

When creating a flutter app the content of android\app\build.gradle is the following.

defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).  
applicationId "com.example.app_name"  
minSdkVersion flutter.minSdkVersion  
targetSdkVersion flutter.targetSdkVersion  
versionCode flutterVersionCode.toInteger()  
versionName flutterVersionName
}  

Where do these values come from?

flutter.minSdkVersion value is coming from your installed flutter SDK's flutter.gradle file which is located inside your_flutter_sdk/packages/flutter_tools/gradle/flutter.gradle Right now Jan 2023 it looks like...

/** Sets the compileSdkVersion used by default in Flutter app projects. */
static int compileSdkVersion = 31

/** Sets the minSdkVersion used by default in Flutter app projects. */
static int minSdkVersion = 16

/** Sets the targetSdkVersion used by default in Flutter app projects. */
static int targetSdkVersion = 31

/**
 * Sets the ndkVersion used by default in Flutter app projects.
 * Chosen as default version of the AGP version below.
 */
static String ndkVersion = "21.1.6352462"

Moving forward, how do we change these defaults?

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.

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

```flutter
    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"}
```

Approach 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.

```flutter
    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:

```flutter
    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 approach going forward you will maintain minimum and target SDK versions within the local.properties file.