build_aar.dart 5.33 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
// @dart = 2.8
6 7
import 'package:meta/meta.dart';

8
import '../android/android_builder.dart';
9
import '../android/gradle_utils.dart';
10
import '../base/common.dart';
11 12

import '../base/file_system.dart';
13 14
import '../base/os.dart';
import '../build_info.dart';
15
import '../cache.dart';
16
import '../globals.dart' as globals;
17
import '../project.dart';
18
import '../reporting/reporting.dart';
19
import '../runner/flutter_command.dart' show FlutterCommandResult;
20 21 22
import 'build.dart';

class BuildAarCommand extends BuildSubCommand {
23
  BuildAarCommand({ @required bool verboseHelp }) : super(verboseHelp: verboseHelp) {
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    argParser
      ..addFlag(
        'debug',
        defaultsTo: true,
        help: 'Build a debug version of the current project.',
      )
      ..addFlag(
        'profile',
        defaultsTo: true,
        help: 'Build a version of the current project specialized for performance profiling.',
      )
      ..addFlag(
        'release',
        defaultsTo: true,
        help: 'Build a release version of the current project.',
      );
40
    addTreeShakeIconsFlag();
41
    usesFlavorOption();
42
    usesBuildNumberOption();
43
    usesPubOption();
44 45
    addSplitDebugInfoOption();
    addDartObfuscationOption();
46
    usesDartDefineOption();
47
    usesExtraDartFlagOptions(verboseHelp: verboseHelp);
48
    usesTrackWidgetCreation(verboseHelp: false);
49 50
    addNullSafetyModeOptions(hide: !verboseHelp);
    addEnableExperimentation(hide: !verboseHelp);
51
    addAndroidSpecificBuildOptions(hide: !verboseHelp);
52
    argParser
53 54
      ..addMultiOption(
        'target-platform',
55
        splitCommas: true,
56
        defaultsTo: <String>['android-arm', 'android-arm64', 'android-x64'],
57 58 59
        allowed: <String>['android-arm', 'android-arm64', 'android-x86', 'android-x64'],
        help: 'The target platform for which the project is compiled.',
      )
60 61
      ..addOption(
        'output-dir',
62
        help: 'The absolute path to the directory where the repository is generated. '
63
              'By default, this is "<current-directory>android/build".',
64 65 66 67 68 69
      );
  }

  @override
  final String name = 'aar';

70 71 72
  @override
  bool get reportNullSafety => false;

73 74 75 76 77
  @override
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async => <DevelopmentArtifact>{
    DevelopmentArtifact.androidGenSnapshot,
  };

78
  @override
79
  Future<CustomDimensions> get usageValues async {
xster's avatar
xster committed
80 81
    final FlutterProject flutterProject = _getProject();
    if (flutterProject == null) {
82
      return const CustomDimensions();
83
    }
84 85

    String projectType;
xster's avatar
xster committed
86
    if (flutterProject.manifest.isModule) {
87
      projectType = 'module';
xster's avatar
xster committed
88
    } else if (flutterProject.manifest.isPlugin) {
89
      projectType = 'plugin';
90
    } else {
91
      projectType = 'app';
92
    }
93 94 95 96 97

    return CustomDimensions(
      commandBuildAarProjectType: projectType,
      commandBuildAarTargetPlatform: stringsArg('target-platform').join(','),
    );
98 99 100 101
  }

  @override
  final String description = 'Build a repository containing an AAR and a POM file.\n\n'
102 103
      'By default, AARs are built for `release`, `debug` and `profile`.\n'
      'The POM file is used to include the dependencies that the AAR was compiled against.\n'
104
      'To learn more about how to use these artifacts, see '
105 106 107
      'https://flutter.dev/go/build-aar\n'
      'Note: this command builds applications assuming that the entrypoint is lib/main.dart. '
      'This cannot currently be configured.';
108 109 110

  @override
  Future<FlutterCommandResult> runCommand() async {
111
    if (globals.androidSdk == null) {
112 113
      exitWithNoSdkMessage();
    }
114
    final Set<AndroidBuildInfo> androidBuildInfo = <AndroidBuildInfo>{};
115 116 117 118 119 120 121 122 123

    final Iterable<AndroidArch> targetArchitectures =
        stringsArg('target-platform').map<AndroidArch>(getAndroidArchForName);

    final String buildNumber = argParser.options.containsKey('build-number')
      && stringArg('build-number') != null
      && stringArg('build-number').isNotEmpty
      ? stringArg('build-number')
      : '1.0';
124

125
    final File targetFile = globals.fs.file(globals.fs.path.join('lib', 'main.dart'));
126
    for (final String buildMode in const <String>['debug', 'profile', 'release']) {
127
      if (boolArg(buildMode)) {
128 129
        androidBuildInfo.add(
          AndroidBuildInfo(
130 131 132 133
            await getBuildInfo(
              forcedBuildMode: BuildMode.fromName(buildMode),
              forcedTargetFile: targetFile,
            ),
134 135 136
            targetArchs: targetArchitectures,
          )
        );
137 138 139 140 141
      }
    }
    if (androidBuildInfo.isEmpty) {
      throwToolExit('Please specify a build mode and try again.');
    }
142

143
    displayNullSafetyMode(androidBuildInfo.first.buildInfo);
144
    await androidBuilder.buildAar(
145
      project: _getProject(),
146
      target: targetFile.path,
147
      androidBuildInfo: androidBuildInfo,
148
      outputDirectoryPath: stringArg('output-dir'),
149
      buildNumber: buildNumber,
150
    );
151
    return FlutterCommandResult.success();
152 153
  }

154
  /// Returns the [FlutterProject] which is determined from the remaining command-line
155 156 157 158 159
  /// argument if any or the current working directory.
  FlutterProject _getProject() {
    if (argResults.rest.isEmpty) {
      return FlutterProject.current();
    }
160
    return FlutterProject.fromDirectory(globals.fs.directory(findProjectRoot(globals.fs, argResults.rest.first)));
161 162
  }
}