Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Set target file base on Android product flavors

Currently I am using Flutter to build my application.

Background

I have followed some guide on building different environments entry files:
https://iirokrankka.com/2018/03/02/separating-build-environments/
which create main_dev.dart and main_prod.dart.

Also I have learnt to build flavor for both iOS and Android: https://medium.com/@salvatoregiordanoo/flavoring-flutter-392aaa875f36
which now I can use --flavor <FLAVOR> in the command to build different flavor application.

Now I have encountered a problem when I try to combine two skills.


Target Result

Below is what I would like to achieve:

development flavor -> main_dev.dart entry file
production flavor -> main_prod.dart entry file


Problem Encounter

in iOS side, I can target the entry file in .xcconfig file like following:

// ios/Flutter/development.xcconfig

#include "Generated.xcconfig"

FLUTTER_TARGET=lib/main_dev.dart

I know I can add -t lib/main_dev.dart after flutter run command.
However I would like to ask if there is any solution to set
the flutter target file in Android side inside flavor config?

Appreciate for any help.

like image 955
Wing Choy Avatar asked Oct 26 '18 04:10

Wing Choy


1 Answers

I would like to ask if there is any solution to set the flutter target file in Android side inside flavor config?

I don't know the equal of FLUTTER_TARGET for Android flavor. I would to like to learn that too.

But, flutter run -t is not the only option here. When you open a Flutter project (the root project) with Android Studio you will have a default run/debug configuration like below:

enter image description here

When you click Edit Configurations below screen will appear:

enter image description here

There you can set Build flavor and Dart entrypoint. Obviously, you can create multiple configurations for each flavor.

Reference: https://cogitas.net/creating-flavors-of-a-flutter-app/

So, that's a solution for Flutter in Android Studio. For VSCode, I have a workaround. I'm using -t parameter. But I have it automated by VSCode. Under .vscode/launch.json I have configurations like below:

"configurations": [
        {
            "name": "GoodOne",
            "request": "launch",
            "type": "dart",
            "args": ["--flavor",
                "good",
                "-t",
                "./lib/main-good.dart"
            ]
        },
        {
            "name": "BadOne",
            "request": "launch",
            "type": "dart",
            "args": ["--flavor",
                "bad",
                "-t",
                "./lib/main-bad.dart"
            ]
        }
]

With this, you can run your flavors by just pressing F5 and choose your config at the upper-left corner.

Again, this is not an exact answer to OP's question, but some workarounds.

like image 131
Sait Banazili Avatar answered Nov 19 '22 15:11

Sait Banazili