build_apk.dart 4.25 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import '../android/android_builder.dart';
6
import '../android/build_validation.dart';
7
import '../android/gradle_utils.dart';
8
import '../build_info.dart';
9
import '../cache.dart';
10
import '../globals.dart' as globals;
11
import '../project.dart';
12
import '../reporting/reporting.dart';
13
import '../runner/flutter_command.dart' show FlutterCommandResult;
14
import 'build.dart';
15

16
class BuildApkCommand extends BuildSubCommand {
17
  BuildApkCommand({bool verboseHelp = false}) : super(verboseHelp: verboseHelp) {
18
    addTreeShakeIconsFlag();
19
    usesTargetOption();
20
    addBuildModeFlags(verboseHelp: verboseHelp);
21
    usesFlavorOption();
22
    usesPubOption();
23 24
    usesBuildNumberOption();
    usesBuildNameOption();
25
    addShrinkingFlag(verboseHelp: verboseHelp);
26
    addSplitDebugInfoOption();
27
    addDartObfuscationOption();
28
    usesDartDefineOption();
29
    usesExtraDartFlagOptions(verboseHelp: verboseHelp);
30
    addBundleSkSLPathOption(hide: !verboseHelp);
31
    addEnableExperimentation(hide: !verboseHelp);
32
    addBuildPerformanceFile(hide: !verboseHelp);
33
    addNullSafetyModeOptions(hide: !verboseHelp);
34
    usesAnalyzeSizeFlag();
35
    addAndroidSpecificBuildOptions(hide: !verboseHelp);
36
    addMultidexOption();
37
    addIgnoreDeprecationOption();
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
      ..addMultiOption('target-platform',
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
  @override
  DeprecationBehavior get deprecationBehavior => boolArg('ignore-deprecation') ? DeprecationBehavior.ignore : DeprecationBehavior.exit;

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

63
  @override
64
  final String description = 'Build an Android APK file from your app.\n\n'
65 66
    "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 "
67
    'suitable for deploying to app stores. If you are deploying the app to the Play Store, '
68
    "it's recommended to use app bundles or split the APK to reduce the APK size. Learn more at:\n\n"
69 70
    ' * https://developer.android.com/guide/app-bundle\n'
    ' * https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split';
71

72
  @override
73 74
  Future<CustomDimensions> get usageValues async {
    String buildMode;
75

76
    if (boolArg('release')) {
77
      buildMode = 'release';
78
    } else if (boolArg('debug')) {
79
      buildMode = 'debug';
80
    } else if (boolArg('profile')) {
81
      buildMode = 'profile';
82 83
    } else {
      // The build defaults to release.
84
      buildMode = 'release';
85
    }
86 87 88 89 90 91

    return CustomDimensions(
      commandBuildApkTargetPlatform: stringsArg('target-platform').join(','),
      commandBuildApkBuildMode: buildMode,
      commandBuildApkSplitPerAbi: boolArg('split-per-abi'),
    );
92 93
  }

94
  @override
95
  Future<FlutterCommandResult> runCommand() async {
96
    if (globals.androidSdk == null) {
97 98
      exitWithNoSdkMessage();
    }
99
    final BuildInfo buildInfo = await getBuildInfo();
Emmanuel Garcia's avatar
Emmanuel Garcia committed
100 101
    final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(
      buildInfo,
102 103
      splitPerAbi: boolArg('split-per-abi'),
      targetArchs: stringsArg('target-platform').map<AndroidArch>(getAndroidArchForName),
104
      multidexEnabled: boolArg('multidex'),
105
    );
106
    validateBuild(androidBuildInfo);
107
    displayNullSafetyMode(androidBuildInfo.buildInfo);
108
    globals.terminal.usesTerminalUi = true;
109
    await androidBuilder?.buildApk(
110
      project: FlutterProject.current(),
111
      target: targetFile,
112
      androidBuildInfo: androidBuildInfo,
113
    );
114
    return FlutterCommandResult.success();
115
  }
116
}