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

时间:2020-03-15 16:09:55

标签: android flutter gradle

尝试在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.

我一直在通过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'

这是我的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

1 个答案:

答案 0 :(得分:0)

以下可能可以帮助您或将来的观众的一些事情:

  1. 运行flutter clean等同于删除build文件夹
  2. 再次检查您的.vscode\launch.json是否有其他参数。
  3. 检查IDE的调试和构建设置。
  4. 检查您的android\app\build.gradle。我知道您提到您不使用调味料,但是当转移到新的开发环境时,某些设置可能会让人难以承受!
  5. 可能无法解决您的问题,但是尝试创建一个新的Flutter项目并开始移动android根文件夹的自定义部分。

祝您编程愉快! :)