Applying Flutters app_plugin_loader Gradle plugin imperatively using the apply script method which is deprecated, will be removed in a future release

29.1k views Asked by At

After Upgrading the Flutter version to 3.19.0 I am getting this warning when running the app:

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: https://flutter.dev/go/flutter-gradle-plugin-apply

You are applying Flutter's main 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: https://flutter.dev/go/flutter-gradle-plugin-apply

How to remove these deprecated gradle settings.

9

There are 9 answers

0
Munsif Ali On BEST ANSWER

First easy way to update it is to delete the android directory of your flutter project and run the following command in your flutter repository

flutter create .

But before that please note that it will create all the android directory from scratch and you will loose settings like if you have added permissions, launcher icons or have done some work in native side etc so be on safer side initialize git repo and commit the code before deleting it and compare android directory after running the following command

if you have created the git repository then restore all the previous files except both project and app level build.gradle and settings.gradle

another way is to follow the steps given in the official documentation here which was little bit confusing for me for the first time. be careful while following the guide otherwise you might get into trouble.

2
Mohamed Sonata On

Since Flutter 3.16, projects generated with flutter create use the Plugin DSL to apply Gradle plugins. Projects created with versions of Flutter prior to 3.16 need to be migrated manually.

To Follow The steps:

Step one : Replace the contents of /android/settings.gradle with the below

Moving (Plugins) Section to new Below one Out of pluginManagement curly Brackets

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.3.0" apply false
    id "org.jetbrains.kotlin.android" version "1.9.22" apply false // Newer Version
}

Add this Section to pluginManagement Below Line >>

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

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

Step Two: Remove the whole buildscript block from <app-src/android/build.gradle:

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

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

Step Three: Remove this code if it's found from <app-src/android/build.gradle:

-def flutterRoot = localProperties.getProperty('flutter.sdk')
-if (flutterRoot == null) {
-    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
-}

-apply plugin: 'com.android.application'
-apply plugin: 'kotlin-android'
-apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

Last Step : in the same file at first line add this code :

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

Finally You can follow instructions From Flutter Docs: Deprecated imperative apply of Flutter's Gradle plugins

0
Emaborsa On

I had the same problem, tried to follow these instructions, did not really help, leaded to other compile problems, because my gradle file had other customizations too.

The problem was in one of the three files

  • app/build.gradle
  • build.gradle
  • settings.gradle

After trying to fix them based on hints found on the web I tried to delete them completely and let them be created by flutter itself using the command flutter create . --project-name aRandomName. This creates many files, you can delete all except the three mentioned above. Using the git changes comparison you can fix them adding what is missing taking notes from the official documentation linked above, here again.

4
Albert Magistr On

It is necessary to migrate the project if it was created on an older version. Here are the instructions: https://docs.flutter.dev/release/breaking-changes/flutter-gradle-plugin-apply.

3
Adnan K On

WHY

After Flutter 3.16, they introduced a new way to apply Gradle plugins called Plugin DSL, before they used something called imperative apply script. So everyone who created Flutter project before 3.16 need to use this new method.

HOW

First in your project level settings.gradle android/settings.gradle remove whole code and paste this

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 '7.3.0' apply false
    id "org.jetbrains.kotlin.android" version '1.7.10' apply false

}

include ":app"

In project level build.gradle android/build.gradle, Remove the whole buildscript block.

This how android/build.gradle looks like

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
}

And in app level build.gradle android/app/build.gradle remove these 2 block of code

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

and

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

and also remove single line code in dependencies block

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

You may have get an error telling to change your gradle version in app/gradle/wrapper/gradle-wrapper.properties.

If you get that error change that too.

For more information, check out this official link.

2
uzixCode On

Upgrade your flutter to latest version then delete android folder in your project then type flutter create . in terminal or cmd if you're using windows and done

0
Abdullah Al Fahad On

I have solved the problem by following instructions from Flutter Docs: https://docs.flutter.dev/release/breaking-changes/flutter-gradle-plugin-apply

Here is what i have done:

Firstly, I have completely replaced android/settings.gradle file like this:

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
    id "org.jetbrains.kotlin.android" version "{kotlinVersion}" apply false
}

include ":app"

Then, inside android/settings.gradle file more specifically inside plugins{} I have changed {agpVersion} with 7.2.0 and {kotlinVersion} with 1.7.20

Then, i have removed the whole buildscript{} from android/build.gradle file. Now the android/build.gradle looks like this:

allprojects {
repositories {
    google()
    jcenter()
}
}

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

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

Then inside android/app/build.gradle file i have deleted some lines. Those lines are,

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with 
flutter.sdk in the local.properties file.")
}

and,

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

Then also in this file i have added some lines in the top. The lines are,

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

And lastly, inside dependencies{} of android/app/build.gradle i also replaced $kotlin_version with 1.7.20

So, Now the android/app/build.gradle file looks like this:

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
    localProperties.load(reader)
}
}


def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}


android {
    compileSdkVersion flutter.compileSdkVersion

sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
}

lintOptions {
    disable 'InvalidPackage'
}

defaultConfig {
    // TODO: Specify your own unique Application ID 
    (https://developer.android.com/studio/build/application-id.html).
    applicationId "com.brachealthcare.app"
    minSdkVersion 21
    targetSdkVersion flutter.targetSdkVersion
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
}

buildTypes {
    release {
        // TODO: Add your own signing config for the release build.
        // Signing with the debug keys for now, so `flutter run --release` 
works.
        signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20"
}

So, I just edited 3 files [android/settings.gradle], [android/build.gradle] and [android/app/build.gradle] and the issue is resolved. Hope this helps. And sorry for my bad english.

0
Kamel kaher On

Facing this issue week ago after updating flutter version , tried the methods above but leads me to infinite loop of problems, I restored everything like it was before

by that I mean the setting.gradle and build.gradle in android and the one in app

Then I've updated my setting.gradle to this one and fixed the problem without editing anything else !

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 "7.3.0" apply false
    id "org.jetbrains.kotlin.android" version "1.7.10" apply false
}

include ":app"
0
Alireza Mhd On

If you created your project with an old version of Flutter, you need to migrate. To do this, first do all the steps step by step. (Of course, you can completely delete the Android project file and run Flutter create . command) https://docs.flutter.dev/release/breaking-changes/flutter-gradle-plugin-apply If your problem is not resolved, use flutter doctor instructions and follow all its steps. If your problem is still not solved, the problem is with the Kotlin version you are using, which is in the android/setting.gradle file (and the best used version is displayed in the VS Code error in debug console.) Replace it with the current version.

Don't forget to use Flutter Run after completing the steps!