Flavors In Flutter — Part 1
Flavors has always been an Intrinsic part in native applications that can now be achieved in Flutter’s latest stable version which is equipped with key Integral updates & features.They let you define specific build configuration & modify them as you want . Flavors are largely used in large scale apps to ensure the correct deployment of desired version of the app by using different icons, app names which can also be easily switched up .
Android Flavors (or schemes on iOS) are defined by categorising different compile-time configuration build types for different stages of development taking in consideration the version of app the particular team is currently working to get in-lined the maximum effeciency.
Flavor: Same App, Different Behaviour.
Flutter, even in its own possess the different build modes which can be amicably used for different purposes
The different modes in flutter SDK are :-
:: Debug: It is the build mode we most use . A ‘ debug ’ banner is usually shown in the top right when you run this mode which can be easily removed by disabling a bool property debugshowcheckmodebanner.The final apk size of this build mode is large. By default, flutter run compiles to debug mode.
:: Profile: Profile mode usually disabled in emulators and can be checked only for real devices to profile your app’s performance .Tracing and service extensions are enabled.Use command flutter run — profile in the terminal for execution.
:: Release: Release mode is the one with most optimal and minimal sized build mode as it is mainly used for deployment. Debugging & Service extensions are disabled. Use command flutter run — release in the terminal for execution.
In Testing, we mostly use flutter release as the app will surely behave much closer to the experience final user will get.
What is the Dire Need to use flavor ::
- Large scale apps are to be dealt with Caution making flavors a must Use .While dealing with live projects a wrong update may prove to be fatal . Flavors prevent such to happen.
- Flavors are apparently the Need of the hour as the team may be working in different stages in parallel so the same version may not always be appropriate for the whole team.
- More focus can be laid down at Innovation by experimenting in earlier stages without any hindrance in release mode .
Create a new flutter app
:: Open your IDE — Select new flutter project . Enter project name andthen click Finish .This will install the sdk (if needed) and will create a new project
Creating Dart Flavors ::
Defining Flavours in dart can be easily done with the help of dart file FlavoursConfig.dart
:
In FlavoursConfig
we declare the flavors’s appname
and other functionality specific as per flavour types .
FlavoursConfig
,an Inherited Widget will be used to configure our flavour making it easily accessible throughout the app. Now for each flavor, we create the main.dart file, used to start up the app . Here we have created 3 such main files:
development.dart
:
qatesting.dart
:
production.dart
:
In qatesting.dart , FlavorsConfig
created previously can be readily used to define a flavor, a custom color, and some values. The same must be done with development.dart
& production.dart
In app.dart
, Homescreen
is created so that futher app can be made.
Flavor Platform Based Configuration ::
Dart created flavors are quite useful , It may also be worthwhile defining them on the basis of patform helping us in setting up personalised appicon
& appname
. This needs to be done by modifying android/app/build.gradle
adding productFlavors
to it :
The flavourDimesions
can be named as per developer ‘s intent but it should be concerned that it shall remain same to all the flavors created up.We have also created applicationIdSuffix
as per flavors.
Flavor Based App Names ::
We can also assign different app names to each build flavor as needed.This can be done by creating a file namely strings.xml
seperately inside each res
directory of specific flavour. We assign a string to the app_name
as per our wish though i have listed each string.xml
by adding comments of their flavour names below:
This strings need to be referenced in the manifest also which can be passed by assigning parameter to android:label.
This need to be referenced at android/app/src/main/AndroidManifest.xml:
With the help of this Different app icons can be constructed :
Using Flavours using IDE:
Though we can always run the different flav our configuration with help of commands at the terminal But It is quite Handy to run flavours from your IDE (Android Studio ) by making custom edit configuration for them as :
:: From the Select panel we can Add/Edit Configuration. Choose Edit Configuration
:
:: Select Add New Configuration — This can be done by selection add from the + at left hand corner or by using the shortcut alt+insert
.
:: Select Flutter from the dropdown menu .
:: Select a appropriate name in the name menu . At the Data EntryPoint , we need to choose the corresponding main file needed from the Choose Dart File
menu. Here , I have taken the name as Development.dart
.
In the build flavour , We need to provide the build flavour defined in build.gradle
. After Providing this Click apply and Select ok. This will create the configuration sucessfully.
Run different flavours in different flavour mode as by inserting the following commands in the terminal of your IDEs
::
Flavor on DEBUG mode can be run as :
flutter run –flavor production-t lib/production.dart
flutter run –flavor qa-t lib/qatesting.dart
flutter run –flavor development-t lib/development.dart
We can also run the flavour in Profile and Release
from the help of terminal as PROFILE mode:
flutter run –profile –flavor production -t lib/production.dart
RELEASE mode:
flutter run –release –flavor production -t lib/production.dart
If Felt stuck between running multiple flavors , Ensure running flutter clean
to clean build files created by app time-to-time.
Closing Thoughts
Flavors is a Quintessential tool in large scale apps in native applications which has now also been introduced in flutter’s latest version also .This basically ensures deployment of the rightly version of the app & decrease in development time by preventing any undesirable errors to occur.
If you have not used Flavors, I hope this article has provided you with valuable information about what is all about Flavors, and that you will give it Flavors — a Try. Begin using flavors for your apps !!
Feel free to connect with us:
And read more articles from FlutterDevs.com
FlutterDevs has been working on Flutter from quite some time now. You can connect with us on Facebook and Twitter for any flutter related queries.
We welcome feedback, and hope that you share what you’re working on using #Flutter. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences!
Thank you for reading. 🌸