We take a look at some of the ways you can upgrade your Gradle, Kotlin, SDKs and NDK versions in your Flutter app, with Android Studio
The versions used in this tutorials are as follows:
- Android Studio: Android Studio Iguana | 2023.2.1 Patch 2 (April 2024)
- Flutter: 3.19.6 • channel stable (April 2024)
- Kotlin: Upgrading from 1.7.0 to 1.9.23
- Gradle Plugin: Upgrading from 7.3.0 to 8.3.2
- Gradle: Upgrading from 7.6.3 to 8.4
YouTube: Flutter Flustered – Upgrading SDKs, NDK, Kotlin & Gradle
Method 1: Sequential / Prompted Upgrading
This method is where you make the necessary changes, when you're prompted to, throughout your project making.
We'll look at an example of how this works:
Let's say you've now added a plugin package to your new flutter project, clicked on "Pub Get", and ran the "main.dart".
During the build, you get presented with a minSDK error, that you need to upgrade your minSDK from 19, to 21.
So you go into your App Level "build.gradle" file, update the minSDK to the recommended minSDK of 21, and run "main.dart" again.
This time, you get shown a Kotlin version error, that you need to upgrade your Kotlin from version 1.7.1, to 1.9.0.
So now you go into your Android Level "settings.gradle" file ("build.gradle" file in the previous versions of flutter), update the Kotlin version to 1.9.0 and run "main.dart" again.
This time your build completes.
Before we get too excited about a successful build, let's go into the Android Module to double check everything's working. This is a step I always recommend doing when you add any new plugin package.
If you have problems opening the Android Module in Android Studio Iguana, you can view my post here on how to overcome that.
Flutter Flustered – Open Android Module in Android Studio Iguana
So it can build successfully, but we do get a recommendation to upgrade our Gradle Plugin version. Although this recommendation doesn't say which version we should upgrade to.
Since we haven't been prompted with a specific Gradle Plugin version, we'd have to go to the Android Developer's website, to find the minimum version for our project's targetSDK or compileSDK. (https://developer.android.com/build/releases/gradle-plugin#api-level-support).
Here we see that the minimum required Gradle Plugin version is 8.1.1.
So back into our "settings.gradle" file, and we update our Gradle Plugin Version to 8.1.1, and we run "main.dart" again.
Now we have an error, prompting us to upgrade our Gradle Wrapper Version to "gradle-8.0-all.zip".
At this point you'd go into android > gradle > wrapper > gradle-wrapper.properties, and update the Gradle Wrapper to the prompted version, but we've done enough steps, to show how the "Sequential / Prompted method" of upgrading works.
Method 1 Summary
You basically go through your project, and as you get a warning, you upgrade it according to the prompts.
This method is probably the quickest to start your project with, and could work for a simple app. However, as your app gets more complicated, this method could end up being more time consuming in the long run. As you're only upgrading to the minimum version that's prompted, you'd often be upgrading the versions with every plugin package you add. You could also end up in a conflict of versions.
Method 2: Pre-emptive / Latest Versions Upgrading
This is where, at the very start of your new Flutter project, you actively find the latest versions for the Gradle, Gradle Plugin, Kotlin, SDK and NDK versions.
Method 2 - Project & Module SDKs
The first step I take whenever I open a new Flutter project, is to go into the "IDE and Project Settings" menu, and select "Project Structure".
The reason I've chosen the API Level 34, you can understand by going into the Android Developer's website the Minimum Target API Level requirement for Google Play (https://developer.android.com/google/play/requirements/target-sdk).
Here you can see, that as of today (03-May-24), the minimum required API Level is 33.
If we now went to the Google Play Support's website, you can see that they set a deadline of August 31st of every year, to update the Target API Level (https://support.google.com/googleplay/android-developer/answer/11926878).
So that means, I'm just a few months away from the new Target API Level to come into effect. So although the current minimum Target Level is API 33, I'd prefer to work ahead and implement API 34 into my project.
Method 2 - Gradle Plugin & Kotlin
Our next step is to go into our Android Level "settings.gradle" file, and scroll down to see the current Gradle Plugin version and Kotlin version.
The easy way to get this Gradle Plugin and Kotlin versions, is to visit the Android Developer website for the latest Gradle Plugin Release, and go to the section about updating the Android Gradle Plugin (https://developer.android.com/build/releases/gradle-plugin#updating-plugin).
Here we can see, that the versions they've provided, is version 8.3.0 for the Gradle Plugin, and version 1.9.23 for the Kotlin.
Now that we have the Gradle Plugin and Kotlin version, we can go back to our "settings.gradle" file, and update those versions.
Method 2 - Gradle Version / Gradle Wrapper Distribution URL
Next, we go into android > gradle > wrapper > gradle-wrapper.properties, where you can see your project's current Gradle Distribution URL. Which in this case, is showing the filename of "gradle-7.6.3-all.zip".
To find the latest Gradle Version, we again go back to the Android Developers website for the Latest Gradle Plugin Release, and go to the section "Update Gradle" (https://developer.android.com/build/releases/gradle-plugin#updating-gradle).
We can see that for our Gradle Plugin Version of 8.3, we want to use the Gradle Version of 8.4.
We can go back into our "gradle-wrapper.properties" file, and amend the Gradle Distribution URL filename from "gradle-7.6.3-all.zip" to the version we found, which is "gradle-8.4-all.zip".
Method 2 - Gradle Version / Gradle Wrapper Distribution URL (Simpler Alternative)
A much simpler way to get the Gradle Version and Gradle Wrapper Distribution filename, is to simply run "main.dart", after updating the Gradle Plugin version.
You'd then be shown an error, with the correct Gradle Version, and the Distribution URL.
It's much easier to just copy from this error message.
Method 2 - NDK Version
If you now run your "main.dart" with the update Kotlin, Gradle Plugin and Gradle versions, you might be shown an NDK version warning, and with the prompt of what version you should be using.
You go into android > app > build.gradle, and update the ndkVersion to the version you've been prompted.
To get your NDK version before the build, you can simply go to the Android Developers website for the Latest Gradle Plugin Release, and go to the "Compatibility" section (https://developer.android.com/build/releases/gradle-plugin#compatibility).
Here you'd be shown the NDK version, along with other versions for compatibility with the latest Gradle Release.
Method 2 - Summary
This method of starting off your project by first updating all the versions, might seem tedious, by having to get sources from an external website, but it would help to avoid stress in later parts of your project.
Method 3: Using AGP Upgrade Assistant
This is using the AGP Upgrade Assistant within Android Studio, which makes upgrading the Gradle Plugin, Gradle Version and Gradle Wrapper Distribution URL much easier.
Method 3 - Kotlin Version
As the name suggests, the AGP (Android Gradle Plugin) Upgrade Assistant, only attends to the Gradle related versions. So we would still need to upgrade the Kotlin version manually.
In the previous method, we went to the developer website to find the Gradle Plugin and Kotlin versions. This time, since we only need the Kotlin version, let's instead go into the Kotlin Website for the latest Kotlin release (https://kotlinlang.org/docs/releases.html#release-details).
Here we can find the latest version as being 1.9.23.
We can update the Kotlin version 1.9.23 into our "settings.gradle" file, and then we open the Android Module as a new Android Studio window.
If you have problems opening the Android Module in Android Studio Iguana, you can view my post here on how to overcome that.
Flutter Flustered – Open Android Module in Android Studio Iguana
Method 3 - Gradle Version / Gradle Wrapper Distribution URL (AGP Upgrade Assistant)
If a new version of the Gradle Plugin is available, you'd be shown the recommended version to upgrade to. Here, we are shown that we can upgrade from version 7.3.0 to version 7.4.2.
Ensure the "Upgrade" checkbox is checked, and click "Run Selected Steps" to start the upgrade.
When the process is complete, you'd be shown if the upgrade was successful, and also a recommendation to test your project.
So we can now go into our Flutter Project window, where we see that the updated version of 7.4.2 is now applied in our "settings.gradle" file, and we run "main.dart" to test our project with the upgrade.
Back in our Android Module window, if we click "Refresh" in the AGP Upgrade Assistant, we'd be shown that our current Gradle Version of 7.4.2 is up-to-date.
If possible, we'd also be shown a suggestion of available newer versions we can upgrade to. Here we can see we can upgrade up to version 8.3.2.
You can scroll up from the drop down, and select the newest Gradle Plugin version, then repeat the same steps as before, of ensuring the "Upgrade" checkbox is checked, and click "Run Selected Steps" to start the upgrade.
When completed, we are now shown that the Gradle Plugin update to version 8.3.2 is successful. We also see that our Gradle version has been updated to version 8.4.
Now when we click on "Refresh", we are shown that our Gradle Plugin version 8.3.2 is up-to-date, and we are not shown any higher versions as being available.
Method 3 - Upgrade Confirmation & Build
With the upgrades with AGP Upgrade Assistant complete, we can go back to our Flutter project window.
We can see in our "settings.gradle" file, that our Gradle Plugin version is now showing the updated version of 8.3.2.
Going into our "gradle-wrapper.properties" file, we can see that our Distribution URL is now showing our updated filename of "gradle-8.4-all.zip".
If we run our "main.dart" now, we get shown the NDK Version warning. The NDK version, like the Kotlin version, is not affected by the AGP Upgrade Assistant. So we should've done that manually as well.
But our build has still completed successful, so our Gradle Plugin and Gradle Updates have gone well.
Method 3 - Summary
The method of using the AGP Upgrade Assistant, and using the Kotlin website is surely the easiest method.
As you can also tell, the AGP Upgrade Assistant has offered us a higher version (8.3.2), then what we had seen in the Android Developers website.
Similarly, I've also experiences the Kotlin website to show a newer release before the Android Developers website would show it.
Method 4: Editing Flutter Folder Files
All the methods we've seen, are methods that you need to do each time you start a new project. I'm sure you'd be tempted to find a way to apply changes to all your new projects.
In the previous version of Flutter, this was still feasible, as you'd only needed to amend the "flutter.gradle" file. In the new version of flutter, you'd have to go through many files.
Here, I'll show you some of the files you'd need to amend, if you do want to try a more permanent solution. However, it is quite a risky step to try, and I would not recommend this method.
⚠ High Risk! | ⚠ Do Not Try
The first file you want to look at, is the "flutter.groovy", in the "flutter > packages > flutter-tools > gradle > src > main > groovy" folder.
In this file, you'd find the following:
- Compile SDK Version
- Min SDK Version
- Target SDK Version
- NDK Version
- Gradle Plugin Version
The next file you want to look at, is the "build.gradle.kts", in the "flutter > packages > flutter-tools > gradle" folder.
In this file, you'd find the following:
- Gradle Plugin Version
The next file you want to look at, is the "gradle_utils.dart", in the "flutter > packages > flutter-tools > lib > src > android" folder.
In this file, you'd find the following:
- Gradle Plugin Version
- Gradle Version
- Kotlin Version
- Compile SDK Version
- Min SDK Version
- Target SDK Version
- NDK Version
The last file you want to look at, is the "README.md", in the "flutter > packages > flutter-tools > lib > src > android" folder.
In this file, you'd find the requirements they've set out, if you do want to amend the versions in any of the files.
Method 4 - Summary
As you can guess, compared to the previous version of Flutter, the new version, requires you to amend much more files. The versions in all the files need to match, and you also have further requirements set out in the "README.md" file. There is a lot more that can go wrong.
So I'd definitely never use this method, and would not recommend anyone else to use it.
Summary
As we've seen a lot of different methods in this tutorial, I'm going to summarise which is the easiest, and the best method to update your versions.
minSdkVersion
Since you want to minSdkVersion to be as low as possible, it is best to only upgrade it when you're prompted to.
compileSdkVersion
targetSdkVersion
Android Developer Website
https://developer.android.com/google/play/requirements/target-sdk
Google Play Support
https://support.google.com/googleplay/android-developer/answer/11926878
ndkVersion
Android Developer Website
https://developer.android.com/build/releases/gradle-plugin#compatibility
Kotlin
Kotlin Releases Website
https://kotlinlang.org/docs/releases.html#release-details
Gradle Plugin
Gradle Distribution Wrapper
AGP Upgrade Assistant
0 Comments