'How to solve the error FAILURE: Build failed with an exception. * Where: Script 'C:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1005

I'm trying to run the application in the android emulator but this error keeps popping.

    FAILURE: Build failed with an exception.

* Where:
Script 'C:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1005

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* 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

BUILD FAILED in 3m 33s
Exception: Gradle task assembleDebug failed with exit code 1

I have tried the following with no help.

flutter channel stable
flutter upgrade --force
flutter pub cache repair
flutter clean

in case anyone can help.



Solution 1:[1]

Try this:

  1. Close project.
  2. Go to project repository folder.
  3. Delete all generated files, for example:
    • .dart_tool
    • .idea
    • .build
    • .flutter-plugins
    • .flutter-plugins-dependencies
    • .metadata
    • .packages

Nothing should changed in your git changes due to .gitignore file

  1. Open Android Studio again

  2. Go to File->Invalidate Caches/Restart, then invalidate caches and restart

    if your project structure is corrupted go to: File -> Close Project, close project and reopen it, it should be back to normal.

  3. Run all the necessary scripts starting with:

    flutter pub get

    dart pub get

Solution 2:[2]

For me the thing was to delete android/app/build folder and run flutter clean.

Solution 3:[3]

Working solution:

Just uninstall/remove the entire flutter SDK folder and reinstall the same version of a flutter you have currently from the below link.

Flutter SDK as per version: https://flutter.dev/docs/development/tools/sdk/releases?tab=macos

After the new flutter is installed. Just do flutter clean and run the app.

Solution 4:[4]

For my case, I found my code incompatible with my flutter version as I used flutter upgrade once. So I had to downgrade (delete the flutter folder manually in your src folder and extract the version that supports my code) my flutter sdk in C:\src\flutter to the version that my app supports. After running flutter clean (which just deletes the files that flashrow has mentioned) it got solved for me.

Solution 5:[5]

2022 Working Solution
I got the same error after running my app on a physical device. This may happen if the compile SDK version is lower than that of the physical device. This error message is not limited just to running on physical devices however, here is the solution.

Go to android/app/build.grade
Find

android {
    compileSdkVersion 30 //Or whatever version it currently is

The change the compileSdkVersion to 31

android {
    compileSdkVersion 31

In the future, the maximum compileSdkVersion might increase, so try 32, or 33 if it doesn't work

Solution 6:[6]

Try to run with the -v parameter to see a better logging console. In my case, some plugins are generating the following error:

Cannot run with sound null safety, because the following dependencies

When I saw it, I run with the option --no-sound-null-safety and I could start my app in the emulator.

Solution 7:[7]

Apperently this seems like a null safety related issue. lowering down the Dart sdk's version from pubspec.yaml could solve this issue.

flutter channel stable
flutter upgrade
## pubspec.yaml
environment:<br>
sdk: '>=2.0.0 <3.0.0'

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 Georgii
Solution 3 Patel Pinkal
Solution 4 Indranil Mondal
Solution 5 Mohammad Abd-elmoniem
Solution 6 Néstor
Solution 7