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);

  _validateTarget(config, flavor);

  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.add('--release');

  _addEnvironmentArguments(
    arguments: arguments,
    config: config,
    target: flavor,
  );

  if (platform != BuildPlatform.web && config.obfuscate) {
    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,
  );
}