Skip to content
DeveloperMemos

Building the APK for a Specific Flavor Using Flutter

Flutter, Android Development2 min read

In Flutter, flavors allow you to create different variants of your app with distinct configurations or environments. Each flavor can have its own set of resources, assets, and behavior tailored to specific requirements. When building your app for deployment, it's essential to generate the APK for the desired flavor accurately. In this article, we will explore the approach to building the APK for a specific flavor using Flutter.

Defining Flavors

To start, let's define the flavors for our Flutter app. Suppose we want to create two flavors named development and production. Open the android/app/build.gradle file of your Flutter project and add the following code snippet inside the android block:

1flavorDimensions "environment"
2
3productFlavors {
4 development {
5 dimension "environment"
6 applicationIdSuffix ".dev"
7 manifestPlaceholders = [appName: "MyApp Development"]
8 }
9 production {
10 dimension "environment"
11 manifestPlaceholders = [appName: "MyApp"]
12 }
13}

In the above configuration, each flavor is associated with a dimension named environment, which allows us to group related flavors together.

Building the APK for a Specific Flavor

Once the flavors are defined, you can build the APK for a specific flavor using the following command:

1flutter build apk --flavor <flavorName>

Replace <flavorName> with the desired flavor, such as development or production. Running this command will generate the APK file for the specified flavor.

For example, to build the APK for the development flavor, use the following command:

1flutter build apk --flavor development

The generated APK file will be located under the build/app/outputs/flutter-apk directory, named app-development.apk.

Similarly, you can replace <flavorName> with production or any other flavor you define. Ensure that you use the correct flavor name when building the APK.

Customizing Flavor-Specific Resources

To leverage the full potential of flavors, you can provide custom resources and assets specific to each flavor. This allows you to tailor aspects like logos, app names, API endpoints, or feature flags based on the target environment.

To add flavor-specific resources, create a new folder inside the android/app/src directory with the same name as your flavor. For instance, if you have a flavor named development, create a folder named development at android/app/src/. Similarly, create a corresponding folder for other flavors.

Next, place the flavor-specific resources in their respective folders. For example, if you want to customize the app icon, replace the ic_launcher.png file located at android/app/src/main/res/mipmap with a different icon file for each flavor.

Remember to sync your project after making these changes by running the command:

1flutter pub get

Verifying the Flavor-Specific Build

To verify that the APK is built for the correct flavor, you can inspect the BuildConfig class from within your Flutter codebase. This class contains various constants that reflect the current build configuration.

For instance, to retrieve the application ID defined in the manifest placeholders, you can use the following code:

1import io.flutter.BuildConfig
2
3...
4
5String applicationId = BuildConfig.APPLICATION_ID;

By accessing BuildConfig.APPLICATION_ID, you can obtain the application ID set for each flavor.