Adjusting MinSdkVersion in Flutter for Android: 2 Simple Approaches [2023]
Integrating external packages into your app can significantly streamline development, allowing you to focus on your app’s core functionality. However, sometimes this process triggers a requirement to increase the Android minSdkVersion due to the plugin’s demands. This scenario applies to projects created both before and after the Flutter 2.8 update. In this comprehensive tutorial, we’ll delve into how to modify the Android 'minSdkVersion'
'(flutter.minsdkversion)'
in Flutter, covering two distinct approaches for different project timelines.
Ways to Change Android MinSdkVersion in Flutter
Let’s explore the two methods to modify the Android minSdkVersion
. The first method applies to projects initiated prior to the Flutter 2.8 update, while the second is tailored for projects commenced after the update.
For Projects Created Before Flutter 2.8 Update
For projects established prior to the 2.8 update, follow these steps to adjust the 'minSdkVersion'
in your Flutter app:
Locate the
'build.gradle'
File: Navigate to the'android/app directory'
within your Flutter project. Inside this directory, locate and open the'build.gradle'
file using a text editor.Update the minSdkVersion: Inside the
'build.gradle'
file, locate the'defaultConfig'
section. Modify the'minSdkVersion'
field to match your desired Android API level. For instance, to set the minimum API level to 21(Android 5.0 - Lollipop)
, make the adjustment as follows:
defaultConfig {
applicationId "com.example.common_project"
minSdkVersion 21 // Update this line
targetSdkVersion 30
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
// ... other configurations ...
}
Screenshot:
- Clean and Re-run: Save the
'build.gradle'
file and execute the'flutter clean'
command in your terminal. Afterward, re-run your Flutter app.
For Projects Created After Flutter 2.8 Update
For projects initiated after the Flutter 2.8 update, altering the Android 'minSdkVersion'
involves modifying the 'local.properties'
file and referencing the new variable in the 'build.gradle'
file. Here’s how:
Locate the
'local.properties'
File: Within your Flutter project, find and open the'local.properties'
file situated in the android directory.Add the
'flutter.minSdkVersion'
Line: Inside the'local.properties'
file, add the following line to set the'flutter.minSdkVersion'
variable:
flutter.minSdkVersion=21
- Update the
'build.gradle'
File: Open the'build.gradle'
file located in the'android/app'
directory. Within the'defaultConfig'
section, update the'minSdkVersion'
using the value from'localProperties.getProperty('flutter.minSdkVersion').toInteger()'
defaultConfig {
applicationId "com.example.sample_project"
minSdkVersion localProperties.getProperty('flutter.minSdkVersion').toInteger()
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
// ... other configurations ...
}
Screenshot:
- Clean and Re-run: As before, save the
'build.gradle'
file and execute the'flutter clean'
command in your terminal. Then, re-run your Flutter app.
Conclusion
This tutorial provided an in-depth exploration of adjusting the Android minSdkVersion (flutter.minsdkversion) in Flutter. By following the detailed steps and practical examples, you can seamlessly tailor the minimum Android API level to meet the requirements of your project. Whether you’re dealing with projects predating the Flutter 2.8 update or ones initiated thereafter, you now possess the knowledge to navigate this crucial aspect of app development.
Curious about other insightful Flutter tutorials? Explore our collection to further enhance your Flutter development expertise.