androidflutterandroid-flavors

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


I'm using Flutter (version 1.17.4) for developing Android and config multi Flavors for running multi-environment.

My source code has 3 environment: dev (devlopment), uat (QC), prod (Production).

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

My multiFlavors.gradle file (/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"
      }
   }
}

Every time, I build app for release with terminal:

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

File .apk is created but terminal throws an exception:

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.

error

I also had this problem when running in debug mode, but on Android Studio (version 4.0), I added config build flavor, the problem is resolved:

add_flavor

How to solve the problem with release mode?


Solution

  • This bug appears to be caused by Flutter looking for an exact .apk name, but not being able to find it. In my case, the name of the .apk being generated was app-release-unsigned.apk but needed to be app-release.apk. This was my solution:

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

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