build_appbundle.dart 3.58 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/gradle_utils.dart';
10
import '../build_info.dart';
11
import '../cache.dart';
12
import '../globals.dart' as globals;
13
import '../project.dart';
14
import '../reporting/reporting.dart';
15 16 17 18 19
import '../runner/flutter_command.dart' show FlutterCommandResult;
import 'build.dart';

class BuildAppBundleCommand extends BuildSubCommand {
  BuildAppBundleCommand({bool verboseHelp = false}) {
20
    addTreeShakeIconsFlag();
21
    usesTargetOption();
22
    addBuildModeFlags(verboseHelp: verboseHelp);
23 24 25 26
    usesFlavorOption();
    usesPubOption();
    usesBuildNumberOption();
    usesBuildNameOption();
27
    addShrinkingFlag(verboseHelp: verboseHelp);
28 29
    addSplitDebugInfoOption();
    addDartObfuscationOption();
30
    usesDartDefineOption();
31
    usesExtraDartFlagOptions(verboseHelp: verboseHelp);
32
    addBundleSkSLPathOption(hide: !verboseHelp);
33 34
    addBuildPerformanceFile(hide: !verboseHelp);
    usesTrackWidgetCreation(verboseHelp: verboseHelp);
35 36
    addNullSafetyModeOptions(hide: !verboseHelp);
    addEnableExperimentation(hide: !verboseHelp);
37
    usesAnalyzeSizeFlag();
38
    addAndroidSpecificBuildOptions(hide: !verboseHelp);
39
    argParser.addMultiOption('target-platform',
40
        splitCommas: true,
41 42
        defaultsTo: <String>['android-arm', 'android-arm64', 'android-x64'],
        allowed: <String>['android-arm', 'android-arm64', 'android-x64'],
43
        help: 'The target platform for which the app is compiled.',
44
      );
45 46 47 48 49
  }

  @override
  final String name = 'appbundle';

50 51 52 53 54
  @override
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async => <DevelopmentArtifact>{
    DevelopmentArtifact.androidGenSnapshot,
  };

55
  @override
56 57
  final String description =
      'Build an Android App Bundle file from your app.\n\n'
58 59
      "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 "
60
      'suitable for deploying to app stores. \n app bundle improves your app size';
61

62 63 64 65 66
  @override
  Future<Map<CustomDimensions, String>> get usageValues async {
    final Map<CustomDimensions, String> usage = <CustomDimensions, String>{};

    usage[CustomDimensions.commandBuildAppBundleTargetPlatform] =
67
        stringsArg('target-platform').join(',');
68

69
    if (boolArg('release')) {
70
      usage[CustomDimensions.commandBuildAppBundleBuildMode] = 'release';
71
    } else if (boolArg('debug')) {
72
      usage[CustomDimensions.commandBuildAppBundleBuildMode] = 'debug';
73
    } else if (boolArg('profile')) {
74 75 76 77 78 79 80 81
      usage[CustomDimensions.commandBuildAppBundleBuildMode] = 'profile';
    } else {
      // The build defaults to release.
      usage[CustomDimensions.commandBuildAppBundleBuildMode] = 'release';
    }
    return usage;
  }

82 83
  @override
  Future<FlutterCommandResult> runCommand() async {
84
    if (globals.androidSdk == null) {
85 86
      exitWithNoSdkMessage();
    }
87
    final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(await getBuildInfo(),
88
      targetArchs: stringsArg('target-platform').map<AndroidArch>(getAndroidArchForName),
89
    );
90
    validateBuild(androidBuildInfo);
91
    displayNullSafetyMode(androidBuildInfo.buildInfo);
92
    await androidBuilder.buildAab(
93
      project: FlutterProject.current(),
94
      target: targetFile,
95
      androidBuildInfo: androidBuildInfo,
96
    );
97
    return FlutterCommandResult.success();
98 99
  }
}