build_apk.dart 5.05 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
    argParser
38
      ..addFlag('split-per-abi',
39
        negatable: false,
40
        help: 'Whether to split the APKs per ABIs. '
41
              'To learn more, see: https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split',
42
      )
43 44
      ..addMultiOption('target-platform',
        splitCommas: true,
45
        defaultsTo: <String>['android-arm', 'android-arm64', 'android-x64'],
46 47 48
        allowed: <String>['android-arm', 'android-arm64', 'android-x86', 'android-x64'],
        help: 'The target platform for which the app is compiled.',
      );
49
    usesTrackWidgetCreation(verboseHelp: verboseHelp);
50 51
  }

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

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

60
  @override
61
  final String description = 'Build an Android APK file from your app.\n\n'
62 63
    "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 "
64
    'suitable for deploying to app stores.';
65

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

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

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

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

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

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