Flutter - Change Android minSdkVersion, targetSdkVersion, compileSdkVersion


This guide shows how to set up the SDK versions for Android in Flutter.

If your Flutter app supports Android, you need to set the right SDK versions:

  • minSdkVersion: the lowest Android version your app can run on.
  • targetSdkVersion: the Android version your app is aimed to run on.
  • compileSdkVersion: the Android version used to compile your app.

Why Change SDK Versions?

You might need to change SDK versions if you get errors asking for a higher version. For example:

# One or more plugins require a higher Android SDK version.
# Fix this issue by adding the following to /home/user/Projects/flutter/android/app/build.gradle:
android {
  compileSdkVersion 33
  ...
}

Or:


 /home/user/Projects/flutter/android/app/src/debug/AndroidManifest.xml Error:
 uses-sdk:minSdkVersion 19 cannot be smaller than version 21 declared in library [:safe_device]
 /home/user/Projects/flutter/build/safe_device/intermediates/library_manifest/debug/AndroidManifest.xml 
as the library might be using APIs not available in 19
Suggestion: use a compatible library with a minSdk of at most 19,
or increase this project's minSdk version to at least 21,
or use tools:overrideLibrary="com.xamdesign.safe_device" to force usage (may lead to runtime failures)


In Flutter, these values are in the build.gradle file. However, since Flutter version 2.8, they're stored differently.

For Flutter Before Version 2.8

Change the values directly in {project_folder}/android/app/build.gradle:

android {
    compileSdkVersion 30 // change here

    defaultConfig {
        minSdkVersion 21 // change here
        targetSdkVersion 28 // change here
    }
}

Then run flutter clean and restart your app.


For Flutter Version 2.8 or Above

SDK version values are defined using variables in build.gradle:

android {
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    defaultConfig {
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
    }
}

These values come from {flutter_sdk_folder}/packages/flutter_tools/gradle/flutter.gradle. Don't change this file directly. Instead, use a local.properties file in {project_folder}/android/:

flutter.compileSdkVersion=33
flutter.minSdkVersion=21
flutter.targetSdkVersion=33
flutter.ndkVersion=23.1.7779620

Then, update build.gradle to read from local.properties:

android {
    compileSdkVersion localProperties.getProperty('flutter.compileSdkVersion').toInteger()
    ndkVersion localProperties.getProperty('flutter.ndkVersion').toInteger()

    defaultConfig {
        minSdkVersion localProperties.getProperty('flutter.minSdkVersion').toInteger()
        targetSdkVersion localProperties.getProperty('flutter.targetSdkVersion').toInteger()
    }
}

Finally, run flutter clean and restart your app.


Summary

In Flutter, set the Android SDK versions in build.gradle. For Flutter 2.8 and above, use a local.properties file to define these values.