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

Viewed 21780

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.

9 Answers

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

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

upgrade your dependencies in your pubspec.yaml

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.

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

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.

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'

I had this problem in (line 1156) when I wanted to build an APK file, after almost executing solutions to solve it, finally found this command build release apk:

flutter build apk --release --no-tree-shake-icons

build apk with the lowest size:

flutter build apk --split-per-abi --no-tree-shake-icons

I could build my APK very well

Related