build_appbundle.dart 6.53 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 6
// @dart = 2.8

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

class BuildAppBundleCommand extends BuildSubCommand {
22 23 24
  BuildAppBundleCommand({
    bool verboseHelp = false,
  }) : super(verboseHelp: verboseHelp) {
25
    addTreeShakeIconsFlag();
26
    usesTargetOption();
27
    addBuildModeFlags(verboseHelp: verboseHelp);
28 29 30 31
    usesFlavorOption();
    usesPubOption();
    usesBuildNumberOption();
    usesBuildNameOption();
32
    addShrinkingFlag(verboseHelp: verboseHelp);
33 34
    addSplitDebugInfoOption();
    addDartObfuscationOption();
35
    usesDartDefineOption();
36
    usesExtraDartFlagOptions(verboseHelp: verboseHelp);
37
    addBundleSkSLPathOption(hide: !verboseHelp);
38 39
    addBuildPerformanceFile(hide: !verboseHelp);
    usesTrackWidgetCreation(verboseHelp: verboseHelp);
40 41
    addNullSafetyModeOptions(hide: !verboseHelp);
    addEnableExperimentation(hide: !verboseHelp);
42
    usesAnalyzeSizeFlag();
43
    addAndroidSpecificBuildOptions(hide: !verboseHelp);
44
    addMultidexOption();
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 67 68 69
      splitCommas: true,
      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',
      negatable: true,
      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',
      negatable: true,
      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.',
    );
70 71 72 73 74
  }

  @override
  final String name = 'appbundle';

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 120 121 122 123
    // Do all setup verification that doesn't involve loading units. Checks that
    // require generated loading units are done after gen_snapshot in assemble.
    if (FlutterProject.current().manifest.deferredComponents != null && boolArg('deferred-components') && boolArg('validate-deferred-components') && !boolArg('debug')) {
      final DeferredComponentsPrebuildValidator validator = DeferredComponentsPrebuildValidator(
        FlutterProject.current().directory,
        globals.logger,
124
        globals.platform,
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        title: 'Deferred components prebuild validation',
      );
      validator.clearOutputDir();
      await validator.checkAndroidDynamicFeature(FlutterProject.current().manifest.deferredComponents);
      validator.checkAndroidResourcesStrings(FlutterProject.current().manifest.deferredComponents);

      validator.handleResults();

      // Delete intermediates libs dir for components to resolve mismatching
      // abis supported by base and dynamic feature modules.
      for (final DeferredComponent component in FlutterProject.current().manifest.deferredComponents) {
        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
  }
}