颤动: 渐变建造无法产生一个。Apk 文件。这个文件很可能是在 < app_root > build 下生成的,但是工具找不到它

我正在试开一个应用程序。由于应用程序无法调试/运行,这个问题一直让我感到奇怪。SDK 版本为28,其余版本如下:

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 构建未能生成.apk 文件 文件是在 C: Development build 下生成的,但是工具 找不到。

有没有一种方法可以通过提供或给出 Gradle 的输出路径来解决这个问题,或者通过一个配置来允许我运行?那个。Apk 似乎是按照错误状态生成的。

enter image description here

更新:

Android Studio -v 3.5.3
Gradle -v 3.4.2
Gradle Wrapper -v 5.1.1
53805 次浏览

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

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

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

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

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

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

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

enter image description here

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.

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!

I solved my problem by disabling split

splits {


abi {


enable false


}
}

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

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

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

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

Steps to follow.

1- cd your_projetc

2- flutter create .

3- finally flutter run

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

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

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.