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
AndroidGradleFlutterDartApkAndroid Problem Overview
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)
> Gradle build failed to produce an .apk file. It's likely that this > file was generated under C:\Development<app_root>\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.
UPDATE:
Android Studio -v 3.5.3
Gradle -v 3.4.2
Gradle Wrapper -v 5.1.1
Android Solutions
Solution 1 - Android
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 - Android
Solution 3 - Android
Solution 4 - Android
Add below code in to the app level build.gradle file after buildTypes:
flavorDimensions "flavor"
productFlavors {
staging {
dimension "flavor"
}
}
Solution 5 - Android
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
Solution 6 - Android
I solved my problem by disabling split
splits {
abi {
enable false
}
}
Solution 7 - Android
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
}
}
Solution 8 - Android
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!
Solution 9 - Android
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
Solution 10 - Android
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')
> }
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
Solution 11 - Android
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.
Solution 12 - Android
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
Solution 13 - Android
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')
}
Solution 14 - Android
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
}
}
Solution 15 - Android
Steps to follow.
1- cd your_projetc
2- flutter create .
3- finally flutter run