Flutter 调试错误“Gradle build 无法生成 .apk 文件”

sbd*_*bd2 6 android gradle flutter

当尝试在 Android 模拟器上调试我的 Flutter 应用程序时,出现以下错误:

Gradle build failed to produce an .apk file. It's likely that this file was generated under <project_folder>\app\build, but the tool couldn't find it.
Run Code Online (Sandbox Code Playgroud)

我一直在 StackOverflow 和 GitHub 上爬行以找到根本原因,但找不到任何有效的方法。

我在许多帖子中看到,这种行为可能是由于运行调试时未指定的应用程序风格造成的,但我根本没有定义任何风格。

我尝试创建一个全新的项目并将代码粘贴回 lib 文件夹,但没有任何改变。

命令flutter build appbundle也会失败,因此当我想生成应用程序包时,我必须通过 Android Studio 来完成(我使用 VS Code 进行编码)。

我能让这个应用程序在调试模式下工作的唯一方法是切换回调试模式,flutter version 1.9.1+hotfix.6这非常烦人,因为我有另一个运行良好的应用程序,并且我必须使用最新版本进行开发。

这是我的app/build.gradle文件:

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    compileSdkVersion 28

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.app"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        resConfigs "es"
    }

    signingConfigs {
       release {
           keyAlias keystoreProperties['keyAlias']
           keyPassword keystoreProperties['keyPassword']
           storeFile file(keystoreProperties['storeFile'])
           storePassword keystoreProperties['storePassword']
       }
   }

    buildTypes {
        release {
           signingConfig signingConfigs.release
           minifyEnabled true
           shrinkResources true
           useProguard true

           proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
       }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.facebook.android:facebook-login:5.15.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

apply plugin: 'com.google.gms.google-services'
Run Code Online (Sandbox Code Playgroud)

这是我的gradle-wrapper.properties文件,以防万一

#Fri Jun 23 08:50:38 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
Run Code Online (Sandbox Code Playgroud)

Und*_*ore 5

以下是一些可能对您或未来的观众有所帮助的事情:

  1. 运行flutter clean相当于删除build文件夹
  2. 仔细检查您的.vscode\launch.json额外参数。
  3. 检查 IDE 的调试和构建设置。
  4. 检查你的android\app\build.gradle. 我知道您提到过您不使用风味,但是当迁移到新的开发环境时,某些设置可能会让人不知所措!
  5. 可能为时已晚,无法帮助解决您的问题,但请尝试创建一个新的 Flutter 项目并开始移动根文件夹的自定义部分android

快乐编码!:)