6

I need to have 3 different APKs (dev, UAT, prod), each targetting different Firebase database. But on each Firebase project, I need to supply a permanent app id. That means I need to have 3 build variants that each deploying APK with different app id. But on Android Studio, I seem can't find such a way to build variants for Flutter for this purpose (the build variants section is empty).

What I'm looking for is not just a different entry point and different constants, but different app id altogether. From what I gather, changing app name and app id in Flutter requires 6-steps like this. I don't think doing these 6-steps each time I want to change build scope is an efficient and correct way to handle this.

Chen Li Yong
  • 5,459
  • 8
  • 58
  • 124
  • Have you found a solution? – Yacine Belarbi Mar 12 '21 at 08:44
  • @YacineBelarbi I have, but it's an external solution rather than internal Android Studio/Flutter solution. I use git to have one base branch, then create 3 branches out of that base branch. Each of the branches only differs in the app id data. If I want to compile a different version, I need to activate a different branch first. – Chen Li Yong Mar 12 '21 at 14:19

1 Answers1

2

In Flutter, you should select build flavors instead of build variants (combination of build flavor and build type). Flutter run has a --flavor option, but not buildType.

Specify build flavors:

In build.gradle below defaultConfig {}:

android {
    ...
    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
        }
        debug {
            applicationIdSuffix ".debug" // Optional, you don't need to create a separate applicationId for debug.
            signingConfig signingConfigs.debug
        }
    }

    flavorDimensions "default"
    productFlavors {
        dev {
            applicationIdSuffix ".dev"
        }
        qa {
            applicationIdSuffix ".qa"
        }
        prod {
        }
    }
}

Run your Flutter app with specific build flavor:

  • On the command line: use flutter run --flavor flavorName, or
  • In Android Studio: Run/debug configuration Drop Down → Edit Configuration... → Additional run args: → Add --flavor dev or --flavor qa or --flavor prod

Confirm the applicationId has changed. I use package_info_plus for this:

  • Add package_info_plus: ^1.0.4 to pubspec.yaml
  • Add the code somewhere:
  PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
    print("Package name: ${packageInfo.packageName}");
  });
  • The packageName will have both the build flavor and build type. In my case, because of applicationIdSuffix in both buildTypes and productFlavors, com.example.dev.debug. You could remove applicationIdSuffix ".debug" if you don't need/ want it.

Now we can have separate build flavor directories with their own google-services.json.

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167