build_apk.dart 4.46 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 9
import '../base/terminal.dart';
import '../build_info.dart';
10
import '../cache.dart';
11
import '../globals.dart';
12
import '../project.dart';
13
import '../reporting/reporting.dart';
14
import '../runner/flutter_command.dart' show FlutterCommandResult;
15
import 'build.dart';
16

17
class BuildApkCommand extends BuildSubCommand {
18
  BuildApkCommand({bool verboseHelp = false}) {
19
    usesTargetOption();
20
    addBuildModeFlags(verboseHelp: verboseHelp);
21
    usesFlavorOption();
22
    usesPubOption();
23 24
    usesBuildNumberOption();
    usesBuildNameOption();
Emmanuel Garcia's avatar
Emmanuel Garcia committed
25
    addShrinkingFlag();
26 27

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

42 43 44
  @override
  final String name = 'apk';

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

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

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

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

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

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

    if (buildInfo.isRelease && !androidBuildInfo.splitPerAbi && androidBuildInfo.targetArchs.length > 1) {
90
      final String targetPlatforms = stringsArg('target-platform').join(', ');
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

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