This tutorial shows you how to set the minSdkVersion
, targetSdkVersion
, compileSdkVersion
, and ndkVersion
for Android platform in Flutter.
If your Flutter application supports the Android platform, you may need to set the appropriate SDK versions. That includes the minimum SDK version (the earliest version that the application can run on), target SDK version (the version that the application is targeted to run on), and compile SDK version (the version used for compiling).
A common condition where you need to change the SDK version is when you get an error that indicates to use a higher SDK version like the following one.
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
...
}
You may also get another error like below.
/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)
Like using native Android, Flutter also relies on the values from the build.gradle
file. However, since version 2.8, the values are stored in another file by default. This tutorial is divided into two parts: for Flutter versions below 2.8 and for Flutter versions starting from 2.8.
Before Version 2.8
If you use Flutter before version 2.8, you can change the values directly in the {project_folder}/android/app/build.gradle
file. In that file, search for the android
block. You should be able to find the compileSdkVersion
keyword. For minSdkVersion
and targetSdkVersion
, the values are inside the defaultConfig
sub-block.
android {
compileSdkVersion 30 // change here
defaultConfig {
minSdkVersion 21 // change here
targetSdkVersion 28 // change here
}
}
After that, run flutter clean
and re-run your application.
Version 2.8 or Above
For projects created using Flutter 2.8 or above, the values of SDK versions are no longer hard coded in build.gradle
by default. If you look at the build.gradle
file, those values are defined using variables which include flutter.compileSdkVersion
, flutter.compileSdkVersion
, and flutter.compileSdkVersion
. Newer versions also have the flutter.ndkVersion
variable.
android {
compileSdkVersion flutter.compileSdkVersion
ndkVersion flutter.ndkVersion
defaultConfig {
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
}
}
Those values are obtained from the {flutter_sdk_folder}/packages/flutter_tools/gradle/flutter.gradle
file. However, it's not recommended to modify the file or change the values in the build.gradle
file to be hard-coded. The recommended way to change it is by using a properties file. Go to {project_folder}/android/local.properties
. Then, add the following properties. You can adjust each value with the version that you choose.
flutter.compileSdkVersion=33
flutter.minSdkVersion=21
flutter.targetSdkVersion=33
flutter.=23.1.7779620
In the {project_folder}/android/app/build.gradle
file, you need to adjust the values to read from the localProperties
file.
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 re-run the application.
Summary
In Flutter, the values of Android minSdkVersion
, targetSdkVersion
, compileSdkVersion
, and ndkVersion
depend on the {project_folder}/android/app/build.gradle
file. For Flutter 2.8 and above, it's recommended to define the values in a properties file if you need to modify the values.
You can also read about: