build method

Future<BuildResult> build({
  1. required BuildPlatform platform,
  2. required String flavor,
})

Builds the application and returns the generated build result.

Implementation

Future<BuildResult> build({required BuildPlatform platform, required String flavor}) async {
  final config = await ConfigService.load();

  InitValidator.validate(config);

  final flavorConfig = config.flavors[flavor];

  if (flavorConfig == null) throw Exception('Flavor "$flavor" not found.');

  final buildType = switch (platform) {
    BuildPlatform.apk => 'apk',
    BuildPlatform.aab => 'appbundle',
    BuildPlatform.ipa => 'ipa',
    BuildPlatform.web => 'web'
  };

  final arguments = <String>['build', buildType];

  if (config.flavoringEnabled && platform != BuildPlatform.web) {
    arguments.addAll(['--flavor', flavor]);
  }

  arguments.addAll(['--release', '--dart-define-from-file=${flavorConfig.env}']);

  if (platform != BuildPlatform.web) {
    arguments.addAll(['--obfuscate', '--split-debug-info=${config.debugInfo}']);
  }

  LoggerService.section('Building $buildType ($flavor)');

  await FlutterService(config).runFlutter(arguments);

  final artifactPath = await ArtifactService.resolve(platform: platform, flavor: flavor);

  LoggerService.success('Artifact generated.');

  return BuildResult(artifactPath: artifactPath);
}