You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter打包Release AAB失败::app:signReleaseBundle任务执行出错

Gradle bundleRelease构建失败:NullPointerException错误

错误详情

执行Gradle任务 bundleRelease 时构建失败,具体错误信息如下:

FAILURE: 构建过程出现异常。

  • 错误原因:
    任务 ':app:signReleaseBundle' 执行失败。

执行com.android.build.gradle.internal.tasks.FinalizeBundleTask$BundleToolRunnable时发生失败
java.lang.NullPointerException(无错误消息)

  • 官方建议尝试:
    使用--stacktrace选项获取堆栈跟踪;使用--info或--debug选项获取更多日志输出;使用--scan选项获取完整分析信息。

构建耗时18秒失败,最终退出码1

项目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 32

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

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "co.demo.flutter"
        minSdkVersion 21
        multiDexEnabled true
        targetSdkVersion 32
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }
      signingConfigs {
       release {
      keyAlias keystoreProperties['keyAlias']
          keyPassword keystoreProperties['keyPassword']
       storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
          storePassword keystoreProperties['storePassword']
       }
   }
    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.release
        }
    }

}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
//    implementation 'com.android.support:multidex:2.0.1'
    implementation 'com.google.android.gms:play-services-tagmanager:17.0.0'
    implementation "androidx.multidex:multidex:2.0.1"

}

问题分析与解决方案

这个NullPointerException是由于release签名配置中的storeFile为null导致的——当key.properties文件不存在、或文件内未配置storeFile属性时,代码会给storeFile赋值null,而签名release包不允许使用null的密钥库文件。

解决步骤:

  1. 检查并配置key.properties文件:确认项目根目录下存在key.properties,并填入完整签名信息:
    storePassword=你的密钥库密码
    keyPassword=你的密钥密码
    keyAlias=你的密钥别名
    storeFile=密钥库文件路径(可使用绝对路径,或相对于项目根目录的相对路径)
    
  2. 修复签名配置的空值逻辑:如果没有正确的签名配置,避免使用无效的release签名,修改signingConfigs代码:
    signingConfigs {
        release {
            if (keystorePropertiesFile.exists()) {
                keyAlias keystoreProperties['keyAlias']
                keyPassword keystoreProperties['keyPassword']
                storeFile file(keystoreProperties['storeFile'])
                storePassword keystoreProperties['storePassword']
            } else {
                // 测试阶段可临时改用debug签名,正式发布必须配置有效签名
                signingConfig signingConfigs.debug
            }
        }
    }
    
  3. 验证密钥库路径有效性:确保storeFile指向的文件真实存在,优先使用绝对路径避免相对路径引发的问题。
  4. 清理缓存后重新构建:执行命令 ./gradlew clean bundleRelease 清理缓存后重新运行构建任务。

内容的提问来源于stack exchange,提问作者sonalika

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 21:55:30