Deprecation Warnings for Imperative Flutter Gradle Plugin Application
Problem Statement
When upgrading Flutter to version 3.19.0+, you may encounter warnings related to imperative Gradle plugin application:
You are applying Flutter's app_plugin_loader Gradle plugin imperatively using the apply script method, which is deprecated and will be removed in a future release. Migrate to applying Gradle plugins with the declarative plugins block...These warnings occur because projects created before Flutter 3.16 use an outdated imperative plugin application method (apply script) that is being phased out. The solution involves migrating to Gradle's declarative plugins block in your configuration files.
Solution: Manual Migration (Recommended)
Step 1: Identify AGP and Kotlin Versions
Locate your Android Gradle Plugin (AGP) and Kotlin versions in android/build.gradle:
buildscript {
ext.kotlin_version = '1.7.10' // Kotlin version
dependencies {
classpath 'com.android.tools.build:gradle:7.3.0' // AGP version
}
}Write down these versions—you'll need them later.
Step 2: Update settings.gradle
Replace the contents of android/settings.gradle with:
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
}()
includeBuild("$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 // Replace {agpVersion} with your AGP
id "org.jetbrains.kotlin.android" version "{kotlinVersion}" apply false // Replace {kotlinVersion} with your Kotlin
}
include ":app"Step 3: Modify build.gradle
Remove the entire buildscript{} block from android/build.gradle—the rest of the file can remain:
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
}Step 4: Update app/build.gradle
- Remove Legacy Configuration: Delete these blocks:
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found...")
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"- Add Plugins Block (top of file):
plugins {
id "com.android.application"
id "kotlin-android"
id "dev.flutter.flutter-gradle-plugin"
}- Remove Obsolete Dependency: In the
dependenciesblock, remove:
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"Alternate Solution: Regenerate Android Directory
For projects with minimal Android customizations:
rm -rf android/
flutter create . --platforms=androidWARNING
- Back up custom configurations (e.g., signing keys, permissions) first
- This overwrites all Android files—manually restore customizations afterward
Handling Google Services/Crashlytics
If you use GMS or Crashlytics:
- In
settings.gradle, add in theplugins{}block:
id "com.google.gms.google-services" version "4.4.0" apply false
id "com.google.firebase.crashlytics" version "2.9.9" apply false- In
app/build.gradle, add in theplugins{}block:
id "com.google.gms.google-services"
id "com.google.firebase.crashlytics"Verification
Run the app to confirm warnings are resolved:
flutter runBest Practices
- Use Version Variables: Declare AGP/Kotlin versions in
gradle.propertiesfor centralized management - Follow Documentation: Refer to the official Flutter migration guide
- Test Thoroughly: Verify critical features (e.g., Firebase, native plugins) after migration
TIP
Project-level updates (e.g., upgrading AGP or Kotlin) should be handled separately after completing this migration.