'Flutter Cannot run Project.afterEvaluate(Closure) when the project is already evaluated

Workaround way: Looks like there is dependecy of onesignal plugin on flutter_tools. The solution is to place onesignal plugin after it in the sequence in the "android/app/build.gradle":

apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'

After upgrading Flutter version from 2.8.1 to 2.10, I started getting the following error. I reinstalled Flutter but the result did not change. When I downgrade Flutter to 2.8.1, it works fine, but when I upgrade to 2.10 it doesn't.


Running Gradle task 'assembleDebug'...

FAILURE: Build failed with an exception.

* Where:
Script '/Users/aksoyhlc/flutter/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 421

 What went wrong:
A problem occurred evaluating root project 'android'.
> A problem occurred configuring project ':app'.
   > **Cannot run Project.afterEvaluate(Closure) when the project is already evaluated.**

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUİLD FAILED in 798ms
Exception: Gradle task assembleDebug failed with exit code 1

part of android/app/build.gradle file

buildscript {
    repositories {
       
        google()  // Google's Maven repository
        maven { url 'https://plugins.gradle.org/m2/' } 
    }
    dependencies {
       
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.6, 0.99.99]'
        classpath 'com.google.gms:google-services:4.3.8'
    }
}
allprojects {

    repositories {
        // Check that you have the following line (if not, add it):
        google()  // Google's Maven repository

    }
}
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'

android/build.gradle file

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

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.8'

    }


}

allprojects {
    repositories {
        google()
        mavenCentral()
       /* maven {
            url "${project(':background_fetch').projectDir}/libs"
        }*/
    }
}

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

task clean(type: Delete) {
    delete rootProject.buildDir
}



[✓] Flutter (Channel stable, 2.10.0, on macOS 12.0.1 21A559 darwin-x64, locale
    tr-TR)
    • Flutter version 2.10.0 at /Users/aksoyhlc/flutter/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5f105a6ca7 (4 days ago), 2022-02-01 14:15:42 -0800
    • Engine revision 776efd2034
    • Dart version 2.16.0
    • DevTools version 2.9.2

[✓] Android toolchain - develop for Android devices (Android SDK version
    32.1.0-rc1)
    • Android SDK at /Users/aksoyhlc/Library/Android/sdk
    • Platform android-32, build-tools 32.1.0-rc1
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)

[✓] VS Code (version 1.64.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.34.0

[✓] Connected device (2 available)
    • Redmi Note 8 Pro (mobile) • ------- • android-arm64  • Android 11
      (API 30)
    • Chrome (web)              • chrome           • web-javascript • Google
      Chrome 98.0.4758.80

[✓] HTTP Host Availability
    • All required HTTP hosts are available


Solution 1:[1]

Have the same issue. Still didn't solve it, but looks like something wrong with an onesignal plugin. If you will remove it, everything works. At least for me.

Issue opened in the OneSignal-Gradle-Plugin repo: https://github.com/OneSignal/OneSignal-Gradle-Plugin/issues/184

Solution 2:[2]

Looks like there is dependecy of onesignal plugin on flutter_tools. The solution is to place onesignal plugin after it in the sequence in the "android/app/build.gradle":

apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2