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

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

  @override
  final String name = 'appbundle';

47 48 49 50 51
  @override
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async => <DevelopmentArtifact>{
    DevelopmentArtifact.androidGenSnapshot,
  };

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

59 60 61 62 63
  @override
  Future<Map<CustomDimensions, String>> get usageValues async {
    final Map<CustomDimensions, String> usage = <CustomDimensions, String>{};

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

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

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