Blogs

A Comprehensive Guide to Implementing Flutter Flavors for Android and iOS Applications

  • Purpose of the Article: One of the essential techniques for managing various versions of your Flutter app is through “flavors,” also known as “build variants” or “product flavors.” This blog will delve into implementing Flutter flavors for Android and iOS applications.
  • Intended Audience: Technology experts
  • Tools and Technology: Android Studio (IDE), Dart, Flutter, Android, Java, iOS, Swift, Yaml
  • Keywords: Platform View, Plugin, Android Studio (IDE), Dart, Flutter, Android, Java, iOS, Swift, Yaml

Introduction

Developing applications that cater to different environments and requirements is a common scenario in software development. Flutter, a versatile UI framework, provides a powerful solution for building natively compiled mobile, web, and desktop applications from a single codebase. One of the essential techniques for managing various versions of your Flutter app is through ’Flavors’, also known as ’build variants’ or ’product Flavors’. In this blog, we’ll delve into implementing Flutter Flavors for Android and iOS applications.

Understanding Flutter Flavors

Flutter Flavors are configurations that allow you to build multiple versions of an app from the same codebase, each with distinct behaviors and appearance. Everyday use cases for flavors include developing separate apps for production, staging, development, or customizing the branding for different clients.

Setting Up Flutter Flavours

  1. Creating Flavor-specific Configuration Files:

In your Flutter project, start by creating configuration files for each Flavor. For instance, if you want to make four Flavors named “dev”, “qa”, “uat” and “prod,” first you’d create an enum.

Next, you’d create `lib/config/app_config.dart` files.

In “app_config.dart”:

To create a new configuration file for your project, navigate to the ‘lib’ folder, create a new subfolder and name it ‘config’, then create a new Dart file and name it ‘main_dev.dart. 

Create separate configuration files for different environments (QA, UAT and PROD)under the ‘config’ folder. Name them as main_qa.dart, main_uat.dart and main_prod.dart. 

Here, I have included some images below for your reference. Please use them as needed. 

  1. Defining Flavors in Android and iOS:

Android (app/build.gradle):

IOS (Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme):

In IOS, first, you need to set up the schemes for different environments. To set up a new scheme, follow the below steps.

Open your XCode Project and then click on Project in the title bar select Scheme and then click on Manage Scheme (below is the screenshot for the path that we have discussed so far)

Upon clicking on the manage scheme, a pop-up will display like this.

Once you’re here, find and select Runner, click on the more option (at the bottom left), click on duplicate, name your scheme name like dev, check the shared, and finally, click on close. Similarly, you must create different schemes for your environments like QA, UAT and PROD.

All your schemes will be displayed under the Autocreate schemes section.

Next, you have to set the build configuration for the different environments. Follow the below steps.

  • Open the Project in XCode,
  • Select the info tab under the Runner project, then click on the plus icon under the Configurations section,
  • Add the following configurations: `dev`, `qa`, `uat`, `prod` for the `Debug`, `Release` and ‘Profile’ modes,
  • Then check the parallelize build for the command line builds option under the Configurations section,

  1. Creating Flavor-specific Entry Points:

Create entry point files for each flavor, such as “lib/config/main_dev.dart”,  “lib/config/main_qa.dart”. “lib/config/main_uat.dart” and `lib/main_prod.dart`, where you can configure the flavor-specific code:


Using Flavors in Code

You can now use the `AppConfig` object from your Flavor-specific configuration files to access Flavor-specific properties in your app. For instance:

Building Flavor-specific Apps

To build Flavor-specific apps, use the following command:

For Android:

flutter build apk –flavor <flavor_name>  -t lib/config/main_<env_name>.dart

For iOS:

flutter build ios –flavor <flavor_name> -t lib/config/main_<env_name>.dart

Conclusion

Flutter Flavors is a powerful tool for managing different versions of your app with distinct configurations and behaviors. By following the steps outlined in this guide, you’ll be able to implement flavors for both Android and iOS applications, allowing you to streamline development and deployment process while catering to various environments and requirements. Whether building separate apps for different deployment stages or customizing the branding for various clients, Flutter Flavors provides the flexibility you need to succeed in the ever-evolving world of mobile application development.

References

Author Bio:

Picture of Noor Mahammad JALAPARTHI

Noor Mahammad JALAPARTHI

Software Engineer - Mobility – Digital Practice

I am a software developer in the world of mobile app development with an overall experience of 3+ years. I am passionate about Flutter, and Dart and developed many applications using this framework.

Leave A Comment

Related Post

Purpose to Contact :
Purpose to Contact :
Purpose to Contact :

Purpose to Contact :
Purpose to Contact :
Purpose to Contact :

Purpose to Contact :