build_apk.dart 4.67 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 9
import '../android/android_sdk.dart';
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
    usesTargetOption();
22
    addBuildModeFlags(verboseHelp: verboseHelp);
23
    usesFlavorOption();
24
    usesPubOption();
25 26
    usesBuildNumberOption();
    usesBuildNameOption();
Emmanuel Garcia's avatar
Emmanuel Garcia committed
27
    addShrinkingFlag();
28 29

    argParser
30
      ..addFlag('split-per-abi',
31
        negatable: false,
32
        help: 'Whether to split the APKs per ABIs. '
33
              'To learn more, see: https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split',
34
      )
35 36
      ..addMultiOption('target-platform',
        splitCommas: true,
37
        defaultsTo: <String>['android-arm', 'android-arm64', 'android-x64'],
38 39 40
        allowed: <String>['android-arm', 'android-arm64', 'android-x86', 'android-x64'],
        help: 'The target platform for which the app is compiled.',
      );
41
    usesTrackWidgetCreation(verboseHelp: verboseHelp);
42 43
  }

44 45 46
  @override
  final String name = 'apk';

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

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

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

    usage[CustomDimensions.commandBuildApkTargetPlatform] =
63
        stringsArg('target-platform').join(',');
64
    usage[CustomDimensions.commandBuildApkSplitPerAbi] =
65
        boolArg('split-per-abi').toString();
66

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

80
  @override
81
  Future<FlutterCommandResult> runCommand() async {
82 83 84
    if (androidSdk == null) {
      exitWithNoSdkMessage();
    }
85
    final BuildInfo buildInfo = getBuildInfo();
Emmanuel Garcia's avatar
Emmanuel Garcia committed
86 87
    final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(
      buildInfo,
88 89 90
      splitPerAbi: boolArg('split-per-abi'),
      targetArchs: stringsArg('target-platform').map<AndroidArch>(getAndroidArchForName),
      shrink: boolArg('shrink'),
91 92 93
    );

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

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