Flutter # Change Android compileSdkVersion, minSdkVersion and targetSdkVersion in better way
There are two ways you can change Android compileSdkVersion. The first one is for the projects that are created before Flutter 2.8 update and the second one is for the projects created after the Flutter 2.8 update.
For the Projects Created After Flutter 2.8 Update
To change Android compileSdkVersion in Flutter for the project created after the 2.8 update, you have to make changes in the local.properties file and then reference the new variable from the local.properties file inside the build.gradle file.
Step 1: Locate the **local.properties** file under the **project_folder/android/local.properties**
Step 2: Inside the local.properties file, add the line as **flutter.compileSdkVersion=33**
Step 3: Now, open the **build.gradle** file under the **project_folder/android/app/build.gradle**
Step 4: Find the **defaultConfig**** section update the **compileSdkVersion** **to the **localProperties.getProperty(‘compileSdkVersion’).toInteger()**.
Step 5: Inside the terminal, run the **flutter clean** command.
Code Example
Under local.properties file
sdk.dir=/Users/User1/Library/Android/sdk
flutter.sdk=/Users/User1/development/flutter
flutter.buildMode=debug
flutter.versionName=1.0.0
flutter.versionCode=1
flutter.compileSdkVersion=33 #new
flutter.minSdkVersion=23 #new
flutter.targetSdkVersion=33 #new
** Under build.gradle file**
android {
compileSdkVersion localProperties.getProperty('flutter.compileSdkVersion').toInteger()
ndkVersion flutter.ndkVersion
.......
}
defaultConfig {
applicationId "com.example.myproject"
minSdkVersion localProperties.getProperty('flutter.minSdkVersion').toInteger()
targetSdkVersion localProperties.getProperty('flutter.targetSdkVersion').toInteger()
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Conclusion
In this tutorial, we saw how to change Android compileSdkVersion,minSdkVersion and targetSdkVersion in Flutter with practical examples.