Flutter: Gradle build failed to produce an .apk file. It's likely that this file was generated under <app_root>\build, but the tool couldn't find it

36,930

Solution 1

in my case I have a multi flavor app like this:

update 04/15/2021:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
            {
                    "name": "YOUR_PROJECT_NAME",
                    "program": "lib/main.dart",
                    "request": "launch",
                    "type": "dart",
                    "args": [
                            "--flavor",
                            "ADD_YOUR_FLAVOR_NAME_HERE" //development or staging or production
                        ]
            }
    ]

Another way:

 android {

    ...

    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
        }
    }

    flavorDimensions "flavor-type"

    productFlavors{
        development{
            dimension "flavor-type"
        }
        staging{
            dimension "flavor-type"
        }
        production{
            dimension "flavor-type"
        }
    }
}

So if you want to run app you have to write the flavor name and then the class name that hold main() function

flutter run --flavor staging -t lib/main_staging.dart

and I solved my error and built the .apk

Solution 2

Added the flavor name in Build name from Edit Configurations and it worked! enter image description here

Solution 3

Ok, I found that it's args in launch.json if you use vscode

enter image description here

Solution 4

Add below code in to the app level build.gradle file after buildTypes:

flavorDimensions "flavor"

productFlavors {
    staging {
        dimension "flavor"
    }
}

And Modify your Edit Configurations like below Image : enter image description here

Solution 5

After confirming the android/app/build.gradle have no issues related to flavor, please run command

flutter run --flavor dev lib/main_dev.dart 

instead of

flutter run lib/main_dev.dart --flavor dev 
Share:
36,930
oetoni
Author by

oetoni

Updated on March 07, 2022

Comments

  • oetoni
    oetoni over 2 years

    I am trying to test drive an app. I keep getting a strange issue with this problem as app fail to debug/run. SDK version is 28 while the rest is below:

    Flutter 1.13.9-pre.79 • channel master • https://github.com/flutter/flutter.git
    Framework • revision 9eb9ea0ffa (6 hours ago) • 2020-01-13 21:30:42 -0800
    Engine • revision 0235a50843
    Tools • Dart 2.8.0 (build 2.8.0-dev.0.0 28c335d5a2)
    

    enter image description here

    Gradle build failed to produce an .apk file. It's likely that this file was generated under C:\Development\\build, but the tool couldn't find it.

    Is there a way to pass this issue or a configuration that can allow me to run by providing or giving the output path to Gradle? The .apk seems to generated as the error states.

    enter image description here

    UPDATE:

    Android Studio -v 3.5.3 
    Gradle -v 3.4.2 
    Gradle Wrapper -v 5.1.1
    
    • Ovidiu
      Ovidiu over 4 years
      What version of Android Studio, Gradle Plugin for Android, and Gradle Wrapper are you using? If I remember correctly this error only happened on an older version of one of these. You should be on at least Android Studio 3.5.3, using Gradle Plugin version 3.5.3 and Gradle Wrapper version 5.4.1
    • oetoni
      oetoni over 4 years
      @Ovidiu thanks first. Now the AS version is 3.5.3 (indeed), the GP version was 3.4.2 and GW version was 5.1.1, I updated, but issue still present, I tried also a gradle clean build
    • Ovidiu
      Ovidiu over 4 years
      Ignore my previous comment, that's not what's causing the error. I managed to replicate it by executing 'flutter run' in a project that has configured flavors. According to your screenshot you have a flavor named 'debug', so you'd want to execute 'flutter run --flavor=debug'
    • oetoni
      oetoni over 4 years
      sorry @Ovidiu, I must disagree, I did run along a form pot in github about the flavor argument and a possibility of this causing the issue but I can assure you I have non. According to this cogitas.net/creating-flavors-of-a-flutter-app, I did checked my /android/app/build.gradle and I found non there. Maybe is there another way to check if there are flavors and I don't know off... how did you determine the fact that I have a debug flavor?
    • TWL
      TWL over 4 years
      it looks like you've got apk file name variants, and some workarounds at github.com/flutter/flutter/issues/44796 show that you need to double check the name of the apk files.
  • AlexPad
    AlexPad almost 4 years
    why flutter create??
  • Kalpesh Dabhi
    Kalpesh Dabhi over 3 years
    in which file need to add the configuration?
  • Mohamed Dernoun
    Mohamed Dernoun about 3 years
    build.gradle app level @KalpeshDabhi
  • hiashutoshsingh
    hiashutoshsingh about 3 years
    how to run this android studio when flutter project is open
  • Dhananjay Gavali
    Dhananjay Gavali about 3 years
    Who requires to add flavor.
  • Farwa
    Farwa about 3 years
    Right beside the device selection dropdown @DhananjayGavali
  • selvakumar
    selvakumar about 3 years
    When I tried to run the command I am getting Target file "flavor" is not found error. how to solve this ?
  • Erfan Eghterafi
    Erfan Eghterafi almost 3 years
    @KalpeshDabhi file at .vscode/launch.json
  • Slamit
    Slamit almost 3 years
    This is indeed all I needed to do thank you :-)
  • Dimitri Leurs
    Dimitri Leurs over 2 years
    How to setup a default flavor, in order to run flutter test (integration testing)
  • Corey Stewart
    Corey Stewart over 2 years
    Solved the problem for both Android and iOS. Thanks!
  • Sanjayrajsinh
    Sanjayrajsinh over 2 years
    should be the same build flavor name, I make a mistake that I write "stage" instead of "staging" in build flavor