flutter_flavorizr 2.0.0
flutter_flavorizr: ^2.0.0 copied to clipboard

A flutter utility to easily create flavors in your flutter application

Flutter Flavorizr #

A flutter utility to easily create flavors in your flutter application

Pub Dart CI Star on GitHub License: MIT

Getting Started #

Let's start by setting up our environment in order to run Flutter Flavorizr

Prerequisites #

Side note: this tool works better on a new and clean Flutter project. Since some processors reference some existing files and a specific base structure, it could be possible that running Flutter Flavorizr over an existing project could throw errors.

Before running Flutter Flavorizr, you must install the following software:

These prerequisites are needed to manipulate the iOS project and schemes. If you are interested in flavorizing Android only, you can skip this step. Keep in mind that you will have to use a custom instructions set with Android and Flutter processors only, otherwise an error will occur.

Installation #

This package is intended to support development of Dart projects. In general, put it under dev_dependencies, in your pubspec.yaml:

dev_dependencies:
  flutter_flavorizr: ^2.0.0

You can install packages from the command line:

pub get

Create your flavors #

Once all of the prerequisites have been installed and you have added flutter_flavorizr as a dev dependency, you have to edit your pubspec.yaml and define the flavors.

Example #

Add a new key named flavorizr and define two sub-items: app and flavors. Under the flavors array you can define the name of the flavors, in our example apple and banana. For each flavor you have to specify the app name, the applicationId and the bundleId.

flavorizr:
  app:
    android:
      flavorDimensions: "flavor-type"
    ios:

  flavors:
    apple:
      app:
        name: "Apple App"

      android:
        applicationId: "com.example.apple"

      ios:
        bundleId: "com.example.apple"

    banana:
      app:
        name: "Banana App"

      android:
        applicationId: "com.example.banana"
      ios:
        bundleId: "com.example.banana"

Available fields #

flavorizr

keytypedefaultrequireddescription
appObjecttrueAn object describing the general capabilities of an app
flavorsArraytrueAn array of items. Each of them describes a flavor configuration
instructionsArrayfalseAn array of instructions to customize the flavorizr process
assetsUrlStringhttps://github.com/AngeloAvv/flutter_flavorizr/releases/download/v2.0.0/assets.zipfalseA string containing the URL of the zip assets file. The default points to the current release
ideStringfalseThe IDE in which the app is being developed. Currently only vscode or idea
Available instructions
valuecategorydescription
assets:downloadMiscellaneousDownloads the assets zip from the network
assets:extractMiscellaneousExtracts the downloaded zip in the project .tmp directory
assets:cleanMiscellaneousRemoves the assets from the project directory
android:buildGradleAndroidAdds the flavors to the Android build.gradle file
android:androidManifestAndroidChanges the reference of the app name in the AndroidManifest.xml
android:dummyAssetsAndroidGenerates some default icons for your custom flavors
flutter:flavorsFlutterCreates the flutter flavor configuration file
flutter:appFlutterCreates the app.dart entry
flutter:pagesFlutterCreates a set of default pages for the app
flutter:targetsFlutterCreates a set of targets for each flavor instance
google:firebaseGoogleAdds Google Firebase configurations for Android and iOS for each flavor
ide:configIDEGenerates debugging configurations for each flavor of your IDE
ios:xcconfigiOSCreates a set of xcconfig files for each flavor and build configuration
ios:buildTargetsiOSCreates a set of build targets for each flavor and build configuration
ios:schemaiOSCreates a set of schemas for each flavor
ios:dummyAssetsiOSGenerates some default icons for your custom flavors
ios:plistiOSUpdates the info.plist file
ios:launchScreeniOSCreates a set of launchscreens for each flavor

android (under app)

keytypedefaultrequireddescription
flavorDimensionsString"flavor-type"falseThe value of the flavorDimensions in the android build.gradle file

ios (under app)

keytypedefaultrequireddescription
buildSettingsDictionary{}falseAn XCode build configuration dictionary XCode Build Settings

app (under flavorname)

keytypedefaultrequireddescription
nameStringtrueThe name of the App

android (under flavorname)

keytypedefaultrequireddescription
applicationIdStringtrueThe applicationId of the Android App
firebaseObjectfalseAn object which contains a Firebase configuration
generateDummyAssetsbooltruefalseTrue if you want to generate dummy assets (icon set, strings, etc)

ios (under flavorname)

keytypedefaultrequireddescription
bundleIdStringtrueThe bundleId of the iOS App
buildSettingsDictionary{}falseA flavor-specific XCode build configuration dictionary XCode Build Settings
firebaseObjectfalseAn object which contains a Firebase configuration
generateDummyAssetsbooltruefalseTrue if you want to generate dummy assets (xcassets, etc)

firebase

keytypedefaultrequireddescription
configStringfalseThe path to the Firebase configuration file (google-services.json for Android and GoogleService-Info.plist for iOS)

Usage #

When you finished defining the flavorizr configuration, you can proceed by running the script with:

flutter pub run flutter_flavorizr

You can also run flutter_flavorizr with a custom set of processors by appending the -p (or --processors) param followed by the processor names separated by comma:

flutter pub run flutter_flavorizr -p <processor_1>,<processor_2>

Example

flutter pub run flutter_flavorizr -p assets:download
flutter pub run flutter_flavorizr -p assets:download,assets:extract

Run your flavors #

Once the process has generated the flavors, you can run them by typing

flutter run --flavor <flavorName> -t lib/main-<flavorName>.dart

Example

flutter run --flavor apple -t lib/main-apple.dart
flutter run --flavor banana -t lib/main-banana.dart

Default processors set #

By default, when you do not specify a custom set of processors by appending the -p (or --processors) param, a default processors set will be used:

  • assets:download
  • assets:extract
  • android:androidManifest
  • android:buildGradle
  • android:dummyAssets
  • flutter:flavors
  • flutter:app
  • flutter:pages
  • flutter:targets
  • ios:xcconfig
  • ios:buildTargets
  • ios:schema
  • ios:dummyAssets
  • ios:plist
  • ios:launchScreen
  • google:firebase
  • assets:clean
  • ide:config

Customize your app #

Flutter_flavorizr creates different dart files in the lib folder. In the flavors.dart file we have the F class which contains all of our customizations.

class F {
  static Flavor? appFlavor;

  static String get title {
    switch (appFlavor) {
      case Flavor.APPLE:
        return 'Apple App';
      case Flavor.BANANA:
        return 'Banana App';
      default:
        return 'title';
    }
  }

}

The process creates a simple title customization: a switch which checks the current appFlavor (defined in our app starting point) and returns the correct value. Here you can write whatever you want, you can create your custom app color palette, differentiate the URL action of a button, and so on.

If you are wondering how to use these getters, you can find an example under the pages folder: in the my_home_page.dart file, the page shown after the launch of the app, we can see a clear reference on the title getter defined in the F class.

Side notes #

I haven't found yet a good groovy parser to guarantee the idempotency of the AndroidBuildGradleProcessor.
The only way to keep track of the autogenerated flavorDimensions is to mark up the beginning and the end of the section with magic comments.
Please do not erase these comments otherwise you will break down the AndroidBuildGradleProcessor.

Third party services #

Google Firebase #

In order to flavorize your project and enable Firebase in your flavor
you have to define a firebase object below each OS flavor. Under the
firebase object you must define the config path of the google-services.json
(if you are under Android configuration) or GoogleService-Info.plist
(if you are under iOS configuration).

As you can see in the example below, we added the path accordingly

flavorizr:
  app:
    android:
      flavorDimensions: "flavor-type"
    ios:

  flavors:
    apple:
      app:
        name: "Apple App"

      android:
        applicationId: "com.example.apple"
        firebase:
          config: ".firebase/apple/google-services.json"

      ios:
        bundleId: "com.example.apple"
        firebase:
          config: ".firebase/apple/GoogleService-Info.plist"

    banana:
      app:
        name: "Banana App"
        
      android:
        applicationId: "com.example.banana"
        firebase:
          config: ".firebase/banana/google-services.json"
      ios:
        bundleId: "com.example.banana"
        firebase:
          config: ".firebase/banana/GoogleService-Info.plist"

Further developments #

  • Let the user define its custom set of available instructions.

Please feel free to submit new issues if you encounter some problems with it.

License #

Flutter Flavorizr is available under the MIT license. See the LICENSE file for more info.

110
likes
100
pub points
84%
popularity

Publisher

angelocassano.it

A flutter utility to easily create flavors in your flutter application

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

archive, args, checked_yaml, collection, flutter, io, json_annotation, xml, yaml

More

Packages that depend on flutter_flavorizr