VS Code/Flutter/Android - 密钥库文件未设置用于签署配置版本

Posted

技术标签:

【中文标题】VS Code/Flutter/Android - 密钥库文件未设置用于签署配置版本【英文标题】:VS Code/Flutter/Android - Keystore file not set for signing config release 【发布时间】:2020-09-08 16:11:10 【问题描述】:

我按照这些步骤在 VS Code 上构建 apk:https://flutter.dev/docs/deployment/android

但我总是得到:

FAILURE:构建失败并出现异常。

出了什么问题: 任务 ':app:validateSigningRelease' 执行失败。

尝试: 使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获得更多日志输出。运行 --scan 以获得完整的见解。

通过https://help.gradle.org获得更多帮助

在 35 秒内构建失败 正在运行 Gradle 任务“assembleRelease”... 运行 Gradle 任务 'assembleRelease'... 完成 36,0s Gradle 任务 assembleRelease 失败,退出代码为 1

我已经尝试像这样在 build.gradle 上创建我的密钥:

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
        
    

像这样:

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.debug
        
    

但我在构建 apk 时遇到了同样的错误。 官方文档似乎是错误的。 如何在 VS Code 上创建 apk?

【问题讨论】:

【参考方案1】:

我刚刚遇到了同样的错误。我认为editing app/build.gradle 的文档有点令人困惑。

这是我解决它的方法。注意重复的 buildTypes 块。

    signingConfigs 
        release 
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        
    
    buildTypes 
        release 
            signingConfig signingConfigs.release
        
    

    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.debug
        
    

这是我的完整 app/build.gradle(记得更改 applicationId)。

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.google.gms.google-services'
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 
        applicationId "!!!YOUR APP ID HERE!!!"
        minSdkVersion 16
        targetSdkVersion 28
        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 
            signingConfig signingConfigs.release
        
    

    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.debug
        
    


flutter 
    source '../..'


dependencies 
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

【讨论】:

我遇到了另一个问题:Google Play 不接受我的捆绑包,因为它是使用调试密钥签名的。我使用了您的答案,甚至删除了调试构建类型。直到那时我才得到关于找不到我的密钥文件的真正构建错误。如果存在调试构建类型,则将其用作后备,因此在构建期间会吞噬错误。我更正了路径,然后我可以正确签署我的捆绑包以进行 Google Play 上传。谢谢 :) PS:我什至没有安装 Android Studio ;-)【参考方案2】:

答案是 Android Studio。我做到了。 VS Code 似乎推出了类似 Flutter 的框架。也许这个问题是一种考验。耐心测试。

【讨论】:

不,没有必要。请参阅我在上一个答案中的评论。

以上是关于VS Code/Flutter/Android - 密钥库文件未设置用于签署配置版本的主要内容,如果未能解决你的问题,请参考以下文章