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
Asked Answered
I

24

115

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
Inside answered 14/1, 2020 at 11:28 Comment(5)
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.1Recalesce
@Recalesce 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 buildInside
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'Recalesce
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?Inside
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.Howlet
D
167

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

Darkroom answered 21/4, 2020 at 15:55 Comment(10)
in which file need to add the configuration?Accounting
build.gradle app level @KalpeshDabhiSettling
how to run this android studio when flutter project is openForetoken
Who requires to add flavor.Bolero
When I tried to run the command I am getting Target file "flavor" is not found error. how to solve this ?Breeze
@KalpeshDabhi file at .vscode/launch.jsonDarkroom
How to setup a default flavor, in order to run flutter test (integration testing)Heroine
Solved the problem for both Android and iOS. Thanks!Anility
For Android Studio users - You can add additional arguments in "Edit configurations"Roana
I had flavors configured in my project too and forgot to add the flavor tag to the flutter build command. That was the issue.Koss
M
56

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

Merchandise answered 9/7, 2020 at 6:20 Comment(4)
Right beside the device selection dropdown @DhananjayGavaliMerchandise
This is indeed all I needed to do thank you :-)Halsy
should be the same build flavor name, I make a mistake that I write "stage" instead of "staging" in build flavorVassell
in build flavor you should type your own flavor name from gradle file, that helped meDiddle
M
23

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

Motherwort answered 6/5, 2020 at 22:33 Comment(0)
B
20

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 
Beardless answered 20/8, 2021 at 14:37 Comment(0)
F
18

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

enter image description here

Feola answered 27/8, 2020 at 2:55 Comment(0)
M
10

This works for me,

flutter run --flavor dev lib/main.dart
Music answered 23/5, 2023 at 7:5 Comment(0)
N
9

In my case, it was setting debuggable false in android app build.gradle that caused this issue. I agree not a lot of people are going to modify debuggable configuration, but it might help someone one day to reference this.

 buildTypes {
    debug {
        debuggable false <= causing problem
        ...
    }
Nowt answered 21/9, 2022 at 14:40 Comment(3)
For me it was debuggable true added to a release configuration.Eutrophic
In my case, I need to debug a release build and haven't succeded yet...The debuggable true inside the release build type prevents the build....Any suggestions? I have a flutter projectShaeshaef
@RamiroG.M. check flutter repo issues, I think there is one on this topic. Depending of what you want to achieve, you could put prod specifics to a flavor instead of the buildType. This way you could generate a productionDebug build.Nowt
K
5

Sometimes this issue happens when you have the following code mentioned in build.gradle file. Comment this code and try running the build and it succeeded for me. If you want to build multiple apk files please use them while taking release build and remove it for debug builds. From the screenshot you shared I can see multiple apk generated for you and commenting split apks will help you fix it.

splits {
    abi {
        enable true
        reset()
        include 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
        universalApk true
    }
}
Kleper answered 1/7, 2020 at 11:54 Comment(0)
Q
5

I solved my problem by disabling split

splits {

    abi {

        enable false

    }
}
Quadrumanous answered 26/11, 2020 at 14:19 Comment(0)
M
4

Step 1: You need to configure flavor from your project/android/app/build.gradle file like this

    defaultConfig {

    flavorDimensions "env"

    productFlavors {
        prod {
            dimension "env"
            applicationIdSuffix ""
            manifestPlaceholders = [appName: "Project Name"]
            applicationId "com.example.prod"

        }
        develop {
            dimension "env"
            manifestPlaceholders = [appName: "[Dev] Project Name"]
            versionNameSuffix ""
            applicationId "com.example.develop"
        }

    }
}

Step 2: Now create separate 2 main file for run different environment like this - folder structure

Here main_develop.dart and main_prod.dart has the same code like this

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

Step 3: Now you are able to run and build different flavor by following those command

  1. Run: flutter run --flavor development -t lib/main_development.dart
  2. Build: flutter build apk --flavor production -t lib/main_production.dart
Mercier answered 14/11, 2023 at 6:36 Comment(0)
S
2

I don't know if it is gonna help you or not but I solved my problem by following solution-1.

Solution-1

Replace your project level build.gradle file with following replace the versions and classpath with your own.

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()
    }

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

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

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

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

The Most Important Part: Make sure your are using two subprojects

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

Source

Solution-2

  • Try rebuilding your android files rename the android folder to
    something else to deactivate it like "android-old" or whatever.

  • Then, go in the parent folder of your project, and run below command

       flutter create --org your.organization <your appname>
    

This will rebuild any missing files. That worked for me

Shult answered 14/8, 2020 at 13:45 Comment(1)
why flutter create??Essentiality
B
2

I add the following section to app/build/gradle file , for I only need arm64-v8a versio apk,

splits {
    abi {
        enable true
        reset()
        include 'arm64-v8a'
        universalApk true
    }
}

then the error occurred, so my workaround to fix this just to make a symbol-link named app-debug.apk to the new named app-arm64-v8a-debug.apk in folder build/app/outputs/flutter-apk/ it works!

Brinna answered 30/10, 2020 at 10:8 Comment(0)
S
1

In my case, the problem was that a module specific SDK was not set for my Android API module. The solution I found is:

Open Android Studio->File->Project Structure->Project SDK box->set Android API
Stannite answered 20/6, 2020 at 10:26 Comment(0)
E
1

After a day of struggling to figure out this problem I found where to start to really solve it. Before we insert random code we have to search what is the exact name of the apk it is generating.

Flutter Build Folder -> app -> outputs -> apk

here you will see exactly what the apk name is called if it is in release or debug mode.

In this case if the name has app-staging.apk or some other weird name there is something in your gradle build that is not working well. In my case it was because I had a flavor that was not needed with the name staging.

If this is not the case in your case, then surely it will always be some gradle setting to check. Try to change from release to debug mode or the opposite from "launch.json", in this case you will be able to see if one side and the other not and understand the configuration differences.

Essentiality answered 24/9, 2020 at 14:3 Comment(0)
M
1

I found out that these lines in build.gradle cause the issue

splits {
abi {
    enable true
    reset()
    include 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
    universalApk true
}}

and also found out that we don't need it, to build split apk run

flutter build apk --split-per-abi

this will generate v7, v8a & x86_64 automatically

Molder answered 17/5, 2021 at 16:12 Comment(0)
P
1

I found out that these lines in build.gradle cause the issue. Then I solved this problem set splits abi enable false

splits{
    abi{
        enable false
        reset()
        include "x86", "x86_64", "armeabi", "armeabi-v7a", "arm64-v8a"
        universalApk false
    }
}
Penny answered 5/12, 2021 at 12:51 Comment(0)
F
0

In my case, I don't add flavor section in gradle files at all. after a few times trying, finally I solved this by adding the following code in root build.gradle file:

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.evaluationDependsOn(':app')
}
Farnesol answered 8/9, 2021 at 9:52 Comment(0)
L
0

Steps to follow.

1- cd your_project

2- flutter create .

3- finally flutter run

Larval answered 6/3, 2022 at 9:49 Comment(1)
rebuilding gradle is required after thisUnlawful
E
0

VIEW IMAGE

Adding the line in the image after the comment in the root android folder build.gradle in subprojects section solved the problem for me

This is the line to add:

project.buildDir = "${rootProject.buildDir}/${project.name}"
Elicia answered 6/3, 2022 at 19:48 Comment(0)
R
0

As others suggested, in my case it was indeed a flavors issue. I checked out a fresh copy of my repo and tried running the app with default settings, which triggered this error. Turns out I forgot to edit my "Run/Debug Configurations", which are stored in Android Studio's settings (.idea/) that are not tracked by my repo. I needed to re-add my flavor options, which was as simple as adding a Flutter configuration (See Run > Edit Configurations... > + button in upper left corner) and specifying its dart entrypoint file+Build flavor name to be able to build anything.

Rustle answered 1/11, 2022 at 8:23 Comment(0)
J
0

Go to .vscode then open the launch.json file and add

{
   "name":"app name",
   "request":"launch",
   "type":"dart",
   "flutterMode":"debug",
   "program":"lib/'main of that flavor'",
   "args":[
      "--flavor",
      "<name of flavor'"
   ]
}
Jarrad answered 14/12, 2022 at 7:20 Comment(0)
U
0

I have stuck on this for a week and applied almost everything I found on the internet. At last, upgrading the gradle (also flutter in my case) resolved this. And yes, upgrading gradle has so many consequences, and spent almost a day fixing the build process with the new gradle version. I have bumped the gradle from 7.2 to 8.0 (current latest stable).

Universality answered 16/4, 2023 at 21:23 Comment(0)
F
0

I had the same issue.. it turns out I had debuggable set to false in build.gradle file. You can't really run the app in debug mode when debuggable is set to false.

Setting it to true resolved the issue.

debug {
            debuggable true

}
Furman answered 23/12, 2023 at 7:11 Comment(0)
S
0

run cd android and then run ./gradlew clean this will solve your problem

Sheelagh answered 22/3, 2024 at 11:5 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.