build_appbundle.dart 6.6 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import '../android/android_builder.dart';
6
import '../android/build_validation.dart';
7
import '../android/deferred_components_prebuild_validator.dart';
8
import '../android/gradle_utils.dart';
9 10
import '../base/deferred_component.dart';
import '../base/file_system.dart';
11
import '../build_info.dart';
12
import '../cache.dart';
13
import '../globals.dart' as globals;
14
import '../project.dart';
15
import '../reporting/reporting.dart';
16 17 18 19
import '../runner/flutter_command.dart' show FlutterCommandResult;
import 'build.dart';

class BuildAppBundleCommand extends BuildSubCommand {
20 21 22
  BuildAppBundleCommand({
    bool verboseHelp = false,
  }) : super(verboseHelp: verboseHelp) {
23
    addTreeShakeIconsFlag();
24
    usesTargetOption();
25
    addBuildModeFlags(verboseHelp: verboseHelp);
26 27 28 29
    usesFlavorOption();
    usesPubOption();
    usesBuildNumberOption();
    usesBuildNameOption();
30
    addShrinkingFlag(verboseHelp: verboseHelp);
31 32
    addSplitDebugInfoOption();
    addDartObfuscationOption();
33
    usesDartDefineOption();
34
    usesExtraDartFlagOptions(verboseHelp: verboseHelp);
35
    addBundleSkSLPathOption(hide: !verboseHelp);
36 37
    addBuildPerformanceFile(hide: !verboseHelp);
    usesTrackWidgetCreation(verboseHelp: verboseHelp);
38 39
    addNullSafetyModeOptions(hide: !verboseHelp);
    addEnableExperimentation(hide: !verboseHelp);
40
    usesAnalyzeSizeFlag();
41
    addAndroidSpecificBuildOptions(hide: !verboseHelp);
42
    addMultidexOption();
43
    addIgnoreDeprecationOption();
44
    argParser.addMultiOption('target-platform',
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
      defaultsTo: <String>['android-arm', 'android-arm64', 'android-x64'],
      allowed: <String>['android-arm', 'android-arm64', 'android-x64'],
      help: 'The target platform for which the app is compiled.',
    );
    argParser.addFlag('deferred-components',
      defaultsTo: true,
      help: 'Setting to false disables building with deferred components. All deferred code '
            'will be compiled into the base app, and assets act as if they were defined under'
            ' the regular assets section in pubspec.yaml. This flag has no effect on '
            'non-deferred components apps.',
    );
    argParser.addFlag('validate-deferred-components',
      defaultsTo: true,
      help: 'When enabled, deferred component apps will fail to build if setup problems are '
            'detected that would prevent deferred components from functioning properly. The '
            'tooling also provides guidance on how to set up the project files to pass this '
            'verification. Disabling setup verification will always attempt to fully build '
            'the app regardless of any problems detected. Builds that are part of CI testing '
            'and advanced users with custom deferred components implementations should disable '
            'setup verification. This flag has no effect on non-deferred components apps.',
    );
66 67 68 69 70
  }

  @override
  final String name = 'appbundle';

71 72 73
  @override
  DeprecationBehavior get deprecationBehavior => boolArg('ignore-deprecation') ? DeprecationBehavior.ignore : DeprecationBehavior.exit;

74 75 76 77 78
  @override
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async => <DevelopmentArtifact>{
    DevelopmentArtifact.androidGenSnapshot,
  };

79
  @override
80 81
  final String description =
      'Build an Android App Bundle file from your app.\n\n'
82 83
      "This command can build debug and release versions of an app bundle for your application. 'debug' builds support "
      "debugging and a quick development cycle. 'release' builds don't support debugging and are "
84
      'suitable for deploying to app stores. \n app bundle improves your app size';
85

86
  @override
87 88
  Future<CustomDimensions> get usageValues async {
    String buildMode;
89

90
    if (boolArg('release')) {
91
      buildMode = 'release';
92
    } else if (boolArg('debug')) {
93
      buildMode = 'debug';
94
    } else if (boolArg('profile')) {
95
      buildMode = 'profile';
96 97
    } else {
      // The build defaults to release.
98
      buildMode = 'release';
99
    }
100 101 102 103 104

    return CustomDimensions(
      commandBuildAppBundleTargetPlatform: stringsArg('target-platform').join(','),
      commandBuildAppBundleBuildMode: buildMode,
    );
105 106
  }

107 108
  @override
  Future<FlutterCommandResult> runCommand() async {
109
    if (globals.androidSdk == null) {
110 111
      exitWithNoSdkMessage();
    }
112

113
    final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(await getBuildInfo(),
114
      targetArchs: stringsArg('target-platform').map<AndroidArch>(getAndroidArchForName),
115
      multidexEnabled: boolArg('multidex'),
116
    );
117 118
    // Do all setup verification that doesn't involve loading units. Checks that
    // require generated loading units are done after gen_snapshot in assemble.
119 120
    final List<DeferredComponent>? deferredComponents = FlutterProject.current().manifest.deferredComponents;
    if (deferredComponents != null && boolArg('deferred-components') && boolArg('validate-deferred-components') && !boolArg('debug')) {
121 122 123
      final DeferredComponentsPrebuildValidator validator = DeferredComponentsPrebuildValidator(
        FlutterProject.current().directory,
        globals.logger,
124
        globals.platform,
125 126 127
        title: 'Deferred components prebuild validation',
      );
      validator.clearOutputDir();
128 129
      await validator.checkAndroidDynamicFeature(deferredComponents);
      validator.checkAndroidResourcesStrings(deferredComponents);
130 131 132 133 134

      validator.handleResults();

      // Delete intermediates libs dir for components to resolve mismatching
      // abis supported by base and dynamic feature modules.
135
      for (final DeferredComponent component in deferredComponents) {
136 137 138 139 140 141 142 143 144 145 146 147 148
        final Directory deferredLibsIntermediate = FlutterProject.current().directory
          .childDirectory('build')
          .childDirectory(component.name)
          .childDirectory('intermediates')
          .childDirectory('flutter')
          .childDirectory(androidBuildInfo.buildInfo.mode.name)
          .childDirectory('deferred_libs');
        if (deferredLibsIntermediate.existsSync()) {
          deferredLibsIntermediate.deleteSync(recursive: true);
        }
      }
    }

149
    validateBuild(androidBuildInfo);
150
    displayNullSafetyMode(androidBuildInfo.buildInfo);
151
    globals.terminal.usesTerminalUi = true;
152
    await androidBuilder?.buildAab(
153
      project: FlutterProject.current(),
154
      target: targetFile,
155
      androidBuildInfo: androidBuildInfo,
156 157
      validateDeferredComponents: boolArg('validate-deferred-components'),
      deferredComponentsEnabled: boolArg('deferred-components') && !boolArg('debug'),
158
    );
159
    return FlutterCommandResult.success();
160 161
  }
}