Flutter 3.16+ Gradle Changes — Android API 34

Manav Garg @sigmapie8
4 min readJan 18, 2024

--

What, Where & How to migrate your projects to be compatible with Flutter 3.16+

Image creds to Vasiliy Zukanov from techyourchance

If you were living under a rock like me and the new DSL changes in gradle environment took you by surprise too, you’re in the right place. This post shows you what changes to make in your gradle, android gradle plugin versions and kotlin gradle plugin versions to run your projects.

I’m going to start by migrating a newly minted project so that, we have a handle on what went where and you can make your project specific changes accordingly.

STEP 1: Flutter upgrade

Upgrade your flutter by running the following command:

flutter upgrade --force

Why force? you can do it without force too, but some people have changes in their flutter checkout which leads to an error message.

STEP 2: Upgrade your java

If not already, come to java 17. That’s the minimum you’re going to require to run Flutter 3.16.

Mac users can install it using — brew install openjdk@17
more info here. Other OS users can download it from here.

After you install your java, don’t forget to add it to your JAVA_HOME environment variable, I’m guessing you would have one already.

Now it’s time to add it your flutter config:

flutter config --jdk-dir <path to your jdk>

Since I am a mac user, my path was - `/opt/homebrew/opt/openjdk@17`. Notice that we use only top level directory NOT the bin folder.

STEP 3: Flutter create

flutter create <name your project>

Create a new project, let’s call it paper-note for now.

STEP 4: android/settings.gradle

Replace the contents of <app-src>/android/settings.gradle with the below, remembering to replace {agpVersion} and {kotlinVersion}

pluginManagement {
def flutterSdkPath = {
def properties = new Properties()
file("local.properties").withInputStream { properties.load(it) }
def flutterSdkPath = properties.getProperty("flutter.sdk")
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
return flutterSdkPath
}
settings.ext.flutterSdkPath = flutterSdkPath()

includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")

repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}

plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "{agpVersion}" apply false
id "org.jetbrains.kotlin.android" version "{kgpVersion}" apply false
}

include ":app"

We have two shiny new blocks here, pluginManagement and plugins

NOTE: If you need to add anything extra to the file, please add it below these blocks. Gradle wants these two blocks to be at the top at all times.

AGPVersion: Android Gradle Plugin version helps interface between gradle and android. You can find the list of all AGPs here. Choose the gradle version according to you target android APIs.

KGPVersion: Kotlin Gradle Plugin version helps interface between Kotlin language and Gradle. You can find the list of all KGPs here. Choose the KGP version according to your AGP version.

I usually update to the latest so this time my plugins looks something like this:

plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "8.2.1" apply false // AGP - https://mvnrepository.com/artifact/com.android.tools.build/gradle?repo=google
id "org.jetbrains.kotlin.android" version "1.9.22" apply false // KGP - https://kotlinlang.org/docs/gradle-configure-project.html#apply-the-plugin
}

STEP 5: android/build.gradle

We need to remove old stuff from project level build.gradle:

// buildscript {
// ext.kotlin_version = '1.7.10'
// repositories {
// google()
// mavenCentral()
// }

// dependencies {
// classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// }
// }

allprojects {
repositories {
google()
mavenCentral()
}
}

rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
delete rootProject.buildDir
}

Remove everything that has been commented out. We do not need to remove anything from app level build.gradle if created the app using Flutter 3.16, if not please follow the steps here.

STEP 6: android/gradle/wrapper/gradle-wrapper.properties

We have changed the gradle plugin, but now it’s time to change the gradle version that plugin supports. You can find all the gradle versions here.

As our policy of keeping things to latest, here’s what my file looks like:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-all.zip

And that’s it. Save and run the app, it should work fine.

Flutter team has written an amazing document on the same stuff but it didn’t pop up on my search results immediately so I felt the need to write this tutorial, you can check the document here.

--

--

Responses (2)