build_appbundle.dart 2.68 KB
Newer Older
1 2 3 4 5 6
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

7
import '../android/android_builder.dart';
8
import '../build_info.dart';
9
import '../project.dart';
10
import '../reporting/reporting.dart';
11 12 13 14 15 16 17 18 19 20 21
import '../runner/flutter_command.dart' show FlutterCommandResult;
import 'build.dart';

class BuildAppBundleCommand extends BuildSubCommand {
  BuildAppBundleCommand({bool verboseHelp = false}) {
    usesTargetOption();
    addBuildModeFlags();
    usesFlavorOption();
    usesPubOption();
    usesBuildNumberOption();
    usesBuildNameOption();
Emmanuel Garcia's avatar
Emmanuel Garcia committed
22
    addShrinkingFlag();
23 24 25

    argParser
      ..addFlag('track-widget-creation', negatable: false, hide: !verboseHelp)
26 27 28
      ..addMultiOption('target-platform',
        splitCommas: true,
        defaultsTo: <String>['android-arm', 'android-arm64'],
29
        allowed: <String>['android-arm', 'android-arm64'],
30
        help: 'The target platform for which the app is compiled.',
31
      );
32 33 34 35 36 37
  }

  @override
  final String name = 'appbundle';

  @override
38 39 40 41 42
  final String description =
      'Build an Android App Bundle file from your app.\n\n'
      '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 '
      'suitable for deploying to app stores. \n app bundle improves your app size';
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  @override
  Future<Map<CustomDimensions, String>> get usageValues async {
    final Map<CustomDimensions, String> usage = <CustomDimensions, String>{};

    usage[CustomDimensions.commandBuildAppBundleTargetPlatform] =
        (argResults['target-platform'] as List<String>).join(',');

    if (argResults['release']) {
      usage[CustomDimensions.commandBuildAppBundleBuildMode] = 'release';
    } else if (argResults['debug']) {
      usage[CustomDimensions.commandBuildAppBundleBuildMode] = 'debug';
    } else if (argResults['profile']) {
      usage[CustomDimensions.commandBuildAppBundleBuildMode] = 'profile';
    } else {
      // The build defaults to release.
      usage[CustomDimensions.commandBuildAppBundleBuildMode] = 'release';
    }
    return usage;
  }

64 65
  @override
  Future<FlutterCommandResult> runCommand() async {
66
    final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(getBuildInfo(),
67
      targetArchs: argResults['target-platform'].map<AndroidArch>(getAndroidArchForName),
Emmanuel Garcia's avatar
Emmanuel Garcia committed
68
      shrink: argResults['shrink'],
69
    );
70
    await androidBuilder.buildAab(
71
      project: FlutterProject.current(),
72
      target: targetFile,
73
      androidBuildInfo: androidBuildInfo,
74 75 76 77
    );
    return null;
  }
}