build_appbundle.dart 6.62 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
  BuildAppBundleCommand({
21
    required super.logger,
22 23
    bool verboseHelp = false,
  }) : super(verboseHelp: verboseHelp) {
24
    addTreeShakeIconsFlag();
25
    usesTargetOption();
26
    addBuildModeFlags(verboseHelp: verboseHelp);
27 28 29 30
    usesFlavorOption();
    usesPubOption();
    usesBuildNumberOption();
    usesBuildNameOption();
31
    addShrinkingFlag(verboseHelp: verboseHelp);
32 33
    addSplitDebugInfoOption();
    addDartObfuscationOption();
34
    usesDartDefineOption();
35
    usesExtraDartFlagOptions(verboseHelp: verboseHelp);
36
    addBundleSkSLPathOption(hide: !verboseHelp);
37 38
    addBuildPerformanceFile(hide: !verboseHelp);
    usesTrackWidgetCreation(verboseHelp: verboseHelp);
39
    addNullSafetyModeOptions(hide: !verboseHelp);
40
    addEnableExperimentation(hide: !verboseHelp);
41
    usesAnalyzeSizeFlag();
42
    addAndroidSpecificBuildOptions(hide: !verboseHelp);
43
    addMultidexOption();
44
    addIgnoreDeprecationOption();
45
    argParser.addMultiOption('target-platform',
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
      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.',
    );
67 68 69 70 71
  }

  @override
  final String name = 'appbundle';

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

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

80
  @override
81 82
  final String description =
      'Build an Android App Bundle file from your app.\n\n'
83 84
      "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 "
85
      'suitable for deploying to app stores. \n app bundle improves your app size';
86

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

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

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

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

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

      validator.handleResults();

      // Delete intermediates libs dir for components to resolve mismatching
      // abis supported by base and dynamic feature modules.
136
      for (final DeferredComponent component in deferredComponents) {
137 138 139 140 141 142 143 144 145 146 147 148 149
        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);
        }
      }
    }

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