build_apk.dart 5.08 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// 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 '../android/build_validation.dart';
9
import '../android/gradle_utils.dart';
10 11
import '../base/terminal.dart';
import '../build_info.dart';
12
import '../cache.dart';
13
import '../globals.dart' as globals;
14
import '../project.dart';
15
import '../reporting/reporting.dart';
16
import '../runner/flutter_command.dart' show FlutterCommandResult;
17
import 'build.dart';
18

19
class BuildApkCommand extends BuildSubCommand {
20
  BuildApkCommand({bool verboseHelp = false}) {
21
    addTreeShakeIconsFlag();
22
    usesTargetOption();
23
    addBuildModeFlags(verboseHelp: verboseHelp);
24
    usesFlavorOption();
25
    usesPubOption();
26 27
    usesBuildNumberOption();
    usesBuildNameOption();
Emmanuel Garcia's avatar
Emmanuel Garcia committed
28
    addShrinkingFlag();
29
    addSplitDebugInfoOption();
30
    addDartObfuscationOption();
31
    usesDartDefineOption();
32
    usesExtraFrontendOptions();
33
    addBundleSkSLPathOption(hide: !verboseHelp);
34
    addEnableExperimentation(hide: !verboseHelp);
35
    addBuildPerformanceFile(hide: !verboseHelp);
36
    addNullSafetyModeOptions(hide: !verboseHelp);
37
    usesAnalyzeSizeFlag();
38
    argParser
39
      ..addFlag('split-per-abi',
40
        negatable: false,
41
        help: 'Whether to split the APKs per ABIs. '
42
              'To learn more, see: https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split',
43
      )
44 45
      ..addMultiOption('target-platform',
        splitCommas: true,
46
        defaultsTo: <String>['android-arm', 'android-arm64', 'android-x64'],
47 48 49
        allowed: <String>['android-arm', 'android-arm64', 'android-x86', 'android-x64'],
        help: 'The target platform for which the app is compiled.',
      );
50
    usesTrackWidgetCreation(verboseHelp: verboseHelp);
51 52
  }

53 54 55
  @override
  final String name = 'apk';

56 57 58 59 60
  @override
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async => <DevelopmentArtifact>{
    DevelopmentArtifact.androidGenSnapshot,
  };

61
  @override
62
  final String description = 'Build an Android APK file from your app.\n\n'
63 64
    "This command can build debug and release versions of your application. 'debug' builds support "
    "debugging and a quick development cycle. 'release' builds don't support debugging and are "
65
    'suitable for deploying to app stores.';
66

67 68 69 70 71
  @override
  Future<Map<CustomDimensions, String>> get usageValues async {
    final Map<CustomDimensions, String> usage = <CustomDimensions, String>{};

    usage[CustomDimensions.commandBuildApkTargetPlatform] =
72
        stringsArg('target-platform').join(',');
73
    usage[CustomDimensions.commandBuildApkSplitPerAbi] =
74
        boolArg('split-per-abi').toString();
75

76
    if (boolArg('release')) {
77
      usage[CustomDimensions.commandBuildApkBuildMode] = 'release';
78
    } else if (boolArg('debug')) {
79
      usage[CustomDimensions.commandBuildApkBuildMode] = 'debug';
80
    } else if (boolArg('profile')) {
81 82 83 84 85 86 87 88
      usage[CustomDimensions.commandBuildApkBuildMode] = 'profile';
    } else {
      // The build defaults to release.
      usage[CustomDimensions.commandBuildApkBuildMode] = 'release';
    }
    return usage;
  }

89
  @override
90
  Future<FlutterCommandResult> runCommand() async {
91
    if (globals.androidSdk == null) {
92 93
      exitWithNoSdkMessage();
    }
94
    final BuildInfo buildInfo = getBuildInfo();
Emmanuel Garcia's avatar
Emmanuel Garcia committed
95 96
    final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(
      buildInfo,
97 98 99
      splitPerAbi: boolArg('split-per-abi'),
      targetArchs: stringsArg('target-platform').map<AndroidArch>(getAndroidArchForName),
      shrink: boolArg('shrink'),
100
    );
101
    validateBuild(androidBuildInfo);
102 103

    if (buildInfo.isRelease && !androidBuildInfo.splitPerAbi && androidBuildInfo.targetArchs.length > 1) {
104
      final String targetPlatforms = stringsArg('target-platform').join(', ');
105

106
      globals.printStatus('You are building a fat APK that includes binaries for '
107
                  '$targetPlatforms.', emphasis: true, color: TerminalColor.green);
108
      globals.printStatus('If you are deploying the app to the Play Store, '
109
                  "it's recommended to use app bundles or split the APK to reduce the APK size.", emphasis: true);
110 111
      globals.printStatus('To generate an app bundle, run:', emphasis: true, indent: 4);
      globals.printStatus('flutter build appbundle '
112
                  '--target-platform ${targetPlatforms.replaceAll(' ', '')}',indent: 8);
113 114 115
      globals.printStatus('Learn more on: https://developer.android.com/guide/app-bundle',indent: 8);
      globals.printStatus('To split the APKs per ABI, run:', emphasis: true, indent: 4);
      globals.printStatus('flutter build apk '
116 117
                  '--target-platform ${targetPlatforms.replaceAll(' ', '')} '
                  '--split-per-abi', indent: 8);
118
      globals.printStatus('Learn more on:  https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split',indent: 8);
119
    }
120
    await androidBuilder.buildApk(
121
      project: FlutterProject.current(),
122
      target: targetFile,
123
      androidBuildInfo: androidBuildInfo,
124
    );
125
    return FlutterCommandResult.success();
126
  }
127
}