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

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

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

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

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

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

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

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

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

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