Mouhmmad Moujarkash
Mouhmmad Moujarkash

Reputation: 46

Separate pubspec.yaml file based on flavor

I have an app where I should launch it in two versions, one for china and one global I created flavors and everything but my problem is that I want to separate the pubspec.yaml file into two files based on flavor, because the libraries in china are different from the global and I don't want to build including all the libraries for the two versions

Upvotes: 3

Views: 2228

Answers (3)

Muhammed salah
Muhammed salah

Reputation: 1

I have three flavors of dev, staging and prod , for me i created three pubspec yaml with three different names supporting each for it's flavor

Different pubspec files

Also created script to choose which pubspec is used in its flavor, I placed the script inside switch_pubspec of scripts folder in my project repository

import 'dart:io';

void main(List<String> arguments) {
  if (arguments.isEmpty) {
    print('Please specify the environment: dev, staging, or prod');
    return;
  }

  final environment = arguments[0];
  final pubspecFile = 'pubspec_$environment.yaml';
  const targetFile = 'pubspec.yaml';

  if (File(pubspecFile).existsSync()) {
    File(pubspecFile).copySync(targetFile);
    print('Switched to $environment environment');
    // Process.runSync('flutter', ['pub', 'get']);
  } else {
    print('File $pubspecFile does not exist');
  }
}

Finally i run dart command to switch the pubspec

dart scripts/switch_pubspec.dart staging

For more advanced usage i created tasks.json inside .vscode and put below code inside it

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Switch to development pubspec",
            "type": "shell",
            "command": "dart scripts/switch_pubspec.dart dev",
            "problemMatcher": []
        },
        {
            "label": "Switch to staging pubspec",
            "type": "shell",
            "command": "dart scripts/switch_pubspec.dart staging",
            "problemMatcher": []
        },
        {
            "label": "Switch to production pubspec",
            "type": "shell",
            "command": "dart scripts/switch_pubspec.dart prod",
            "problemMatcher": []
        }
    ]
}

Also modified launch.json file

{
   
    "version": "0.2.0",
"configurations": [
        {
            "name": "Launch development",
            "request": "launch",
            "type": "dart",
            "program": "lib/main_development.dart",
            "args": ["--flavor", "development", "--target", "lib/main_development.dart"],
            "preLaunchTask": "Switch to development pubspec"
        },
        {
            "name": "Launch staging",
            "request": "launch",
            "type": "dart",
            "program": "lib/main_staging.dart",
            "args": ["--flavor", "staging", "--target", "lib/main_staging.dart"],
            "preLaunchTask": "Switch to staging pubspec"
        },
        {
            "name": "Launch production",
            "request": "launch",
            "type": "dart",
            "program": "lib/main_production.dart",
            "args": ["--flavor", "production", "--target", "lib/main_production.dart"],
            "preLaunchTask": "Switch to production pubspec"
        }
    ]
}

Upvotes: 0

died74
died74

Reputation: 1

You can use a gradle script to do that

First on the project configuration you can configure a BeforeRunTask

For example (.idea/runConfigurations/Global_prod.xml)

<component name="ProjectRunConfigurationManager">
  <configuration default="false" name="global-prod" type="FlutterRunConfigurationType" factoryName="Flutter" singleton="false">
    <option name="additionalArgs" value="--flavor=globalprod" />
    <option name="buildFlavor" value="globalprod" />
    <method v="2">
      <option name="Gradle.BeforeRunTask" enabled="true" tasks="copyGlobalPubspec" externalProjectPath="$PROJECT_DIR$/android" vmOptions="" scriptParameters="--quiet" />
    </method>
  </configuration>
</component>

and then on glade create a task to replace the pub spect

por example: (android/app/build.gradle)

task copyCAPubspec(type: Copy) {
    from('../../app/markets/global/pubspec-global.yaml') {
        rename '.*', 'pubspec.yaml'
    }
    into '../../'

    from('../../app/markets/global/analysis_options_market.yaml')
    into '../../'
}

Upvotes: 0

Thibault
Thibault

Reputation: 415

It seems to be a current limitation of Flutter, cf #46979.

Upvotes: 3

Related Questions