无法在 Flutter 中构建具有多种口味的发布 apk?

Can't build release apk with multi flavors in Flutter?

我正在使用 Flutter(版本 1.17.4)开发 Android 并为 运行 多环境配置 multi Flavors

我的源代码有 3 个环境:dev(开发),uat(QC),prod(生产)。

Source: https://github.com/huubao2309/demo_multi_flavors

我的 multiFlavors.gradle 文件 (/android/app/multiFlavors.gradle):

android {
    flavorDimensions "app"

    productFlavors {
      prod {
        dimension "app"
        applicationIdSuffix ".prod"
        versionNameSuffix "-prod"
      }
      dev {
        dimension "app"
        applicationIdSuffix ".dev"
        versionNameSuffix "-dev"
      }
      uat {
        dimension "app"
        applicationIdSuffix ".uat"
        versionNameSuffix "-uat"
      }
   }
}

每次,我构建应用程序以使用终端发布:

flutter build apk --flavor uat -t lib/main-uat.dart

文件 .apk 已创建但终端抛出异常:

Gradle build failed to produce an .apk file. It's likely that this file was generated under D:\demo_multi_flavors\build, but the tool couldn't find it.

我在运行的debug模式下也有这个问题,但是在AndroidStudio(4.0版)上,我添加了配置build flavor,问题就解决了:

如何解决release模式的问题?

此错误似乎是由于 Flutter 正在寻找确切的 .apk 名称,但无法找到。在我的例子中,生成的 .apk 的名称是 app-release-unsigned.apk 但需要是 app-release.apk。这是我的解决方案:

  buildTypes {
    debug {
        signingConfig signingConfigs.debug
    }
    release {
        applicationVariants.all { variant ->
            variant.outputs.all {
                outputFileName = "app-${variant.getName()}.apk"
            }
        }
    }
}

参考:https://github.com/flutter/flutter/issues/44796#issuecomment-602278977