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

Flutter打包Release APK报错:Cannot recover key问题求助

Flutter Release构建报错:Cannot recover key 排查与解决

问题背景

使用已创建的Keystore文件构建Release APK,Keystore放置在android/app/AlZarooniKS路径,assembleDebug构建正常,但assembleRelease时抛出以下错误:

* What went wrong:
Execution failed for task ':app:packageRelease'.
> A failure occurred while executing com.android.build.gradle.tasks.PackageAndroidArtifact$IncrementalSplitterRunnable
   > com.android.ide.common.signing.KeytoolException: Failed to read key AlZarooniKS from store "/Users/hammas/Desktop/parking/android/app/AlZarooniKS": Cannot recover key

* 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

BUILD FAILED in 15s
Running Gradle task 'assembleRelease'...                           16.6s
Gradle task assembleRelease failed with exit code 1

当前配置:

key.properties

storePassword=xxx
keyPassword=xxx
keyAlias=AlZarooniKS
storeFile=/Users/hammas/Desktop/parking/android/app/AlZarooniKS

build.gradle(android/app目录下)

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 FileNotFoundException("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'
// START: FlutterFire Configuration
apply plugin: 'com.google.gms.google-services'
// END: FlutterFire Configuration
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 localProperties.getProperty('flutter.compileSdkVersion').toInteger()
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

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

    defaultConfig {
        applicationId "com.quaidtech.emirates_parking"
        minSdkVersion localProperties.getProperty('flutter.minSdkVersion').toInteger()
        targetSdkVersion flutter.targetSdkVersion
        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 {
            signingConfig signingConfigs.release
        }
    }

}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

排查与解决步骤

  • 验证密码正确性
    直接用keytool命令校验密钥库和密钥的密码:

    1. 验证storePassword:
      keytool -list -v -keystore /Users/hammas/Desktop/parking/android/app/AlZarooniKS
      
      输入storePassword后,如果能正常列出密钥库信息,说明storePassword正确;如果报错,说明密码错误。
    2. 验证keyPassword:
      keytool -exportcert -alias AlZarooniKS -keystore /Users/hammas/Desktop/parking/android/app/AlZarooniKS
      
      输入keyPassword后,若能导出证书文件,说明keyPassword正确。注意密码区分大小写,不要包含未转义的特殊字符。
  • 检查Keystore文件完整性与权限

    1. 确认Keystore文件未损坏:如果是备份文件,尝试使用原始文件;如果是新生成的,重新生成一次。
    2. 修复文件权限:macOS/Linux下执行以下命令确保当前用户有读取权限:
      chmod 644 /Users/hammas/Desktop/parking/android/app/AlZarooniKS
      
  • 核对配置一致性

    1. 确认keyAlias完全匹配:密钥库中的别名必须和key.properties里的keyAlias=AlZarooniKS完全一致,区分大小写。
    2. 优化storeFile路径:可以尝试使用相对路径替代绝对路径,key.properties在android目录下,修改为:
      storeFile=app/AlZarooniKS
      
    3. 检查build.gradle配置:确认signingConfigs中的参数取值正确,没有拼写错误。
  • 重新生成Keystore文件
    如果以上步骤都无效,可能是Keystore文件本身存在问题,重新生成一个:

    keytool -genkey -v -keystore ~/AlZarooniKS -keyalg RSA -keysize 2048 -validity 10000 -alias AlZarooniKS
    

    生成后将文件移动到android/app/AlZarooniKS路径,更新key.properties中的密码和路径,再尝试构建。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 00:45:39