You need to enable JavaScript to run this app. Flutter下flutter run正常但构建APK失败,常规修复无效问题咨询
优惠活动
大模型
产品
解决方案
定价
更多

Flutter下flutter run正常但构建APK失败,常规修复无效问题咨询

Flutter Release 构建异常说明

基础现象

  • 调试阶段执行flutter run可正常运行项目
  • 执行正式APK构建流程时触发中断报错
  • 执行flutter doctor环境检测未发现异常项

已尝试未生效的操作

  • 执行flutter clean清理全量构建缓存
  • 删除C:\src路径下的Flutter安装目录,重新部署SDK
  • 执行flutter pub cache repair修复本地Pub依赖缓存

构建阶段报错原文

FAILURE: Build failed with an exception.                                 

* Where:
Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 904

* What went wrong:
Execution failed for task ':app:compileFlutterBuildRelease'.
> Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

注:当前展示的是Gradle框架抛出的通用失败提示,未透出Flutter编译子进程返回的真实错误栈

项目现有配置内容

项目级build.gradle配置

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.2'
        classpath 'com.google.gms:google-services:4.3.4'
        /* classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.12' */
    }
}

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
}

应用级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 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

   lintOptions {
    disable 'InvalidPackage'
    checkReleaseBuilds false
}

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

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

    buildTypes {
        release {
            // minifyEnabled false
            
            signingConfig signingConfigs.debug

           // signingConfig signingConfigs.release

        }
    }
}

flutter {
    source '../..'
}

dependencies {
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test:runner:1.3.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.google.firebase:firebase-analytics:18.0.0'
    implementation 'com.google.firebase:firebase-messaging:21.0.0'
}

apply plugin: 'com.google.gms.google-services'

常见排查方向

  • 执行带详细日志的构建命令,抓取真实错误栈:flutter build apk --release --verbose,定位Flutter编译子进程退出的根因
  • 校验SDK版本兼容关系:当前项目compileSdkVersion为28,搭配的Android Gradle Plugin 4.0.2、低版本Firebase依赖,与新版Flutter SDK存在版本适配断层,可优先将compileSdkVersion升级至33及以上,匹配对应AGP、Firebase依赖的版本要求
  • 替换过时依赖:当前项目中引用的com.android.support:multidex:1.0.3为旧版Android Support库组件,项目已切换为AndroidX生态,可替换为androidx.multidex:multidex:2.0.1避免类冲突
  • 核对Release专属流程:flutter run默认运行Debug变体,不会执行TreeShake、资源裁剪、代码混淆等Release构建专属流程,这类流程触发的异常不会在调试阶段暴露,可逐阶段关闭Release专属校验流程定位触发点

内容的提问来源于stack exchange,提问作者L. Dabbeet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.06 00:06:10