Skip to content

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:

plaintext
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.

Step 1: Identify AGP and Kotlin Versions

Locate your Android Gradle Plugin (AGP) and Kotlin versions in android/build.gradle:

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:

gradle
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:

gradle
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

  1. Remove Legacy Configuration: Delete these blocks:
gradle
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"
  1. Add Plugins Block (top of file):
gradle
plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}
  1. Remove Obsolete Dependency: In the dependencies block, remove:
gradle
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

Alternate Solution: Regenerate Android Directory

For projects with minimal Android customizations:

bash
rm -rf android/ 
flutter create . --platforms=android

WARNING

  • 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:

  1. In settings.gradle, add in the plugins{} block:
gradle
id "com.google.gms.google-services" version "4.4.0" apply false
id "com.google.firebase.crashlytics" version "2.9.9" apply false
  1. In app/build.gradle, add in the plugins{} block:
gradle
id "com.google.gms.google-services"
id "com.google.firebase.crashlytics"

Verification

Run the app to confirm warnings are resolved:

bash
flutter run

Best Practices

  1. Use Version Variables: Declare AGP/Kotlin versions in gradle.properties for centralized management
  2. Follow Documentation: Refer to the official Flutter migration guide
  3. 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.