build_aar.dart 4.52 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
import '../base/common.dart';
11 12
import '../base/os.dart';
import '../build_info.dart';
13
import '../cache.dart';
14
import '../project.dart';
15
import '../reporting/reporting.dart';
16
import '../runner/flutter_command.dart' show FlutterCommandResult;
17 18 19
import 'build.dart';

class BuildAarCommand extends BuildSubCommand {
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  BuildAarCommand() {
    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.',
      );
37
    usesFlavorOption();
38
    usesBuildNumberOption();
39 40
    usesPubOption();
    argParser
41 42
      ..addMultiOption(
        'target-platform',
43
        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 project is compiled.',
      )
48 49
      ..addOption(
        'output-dir',
50 51 52 53 54 55 56 57
        help: 'The absolute path to the directory where the repository is generated.'
              'By default, this is \'<current-directory>android/build\'. ',
      );
  }

  @override
  final String name = 'aar';

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

63
  @override
64 65
  Future<Map<CustomDimensions, String>> get usageValues async {
    final Map<CustomDimensions, String> usage = <CustomDimensions, String>{};
xster's avatar
xster committed
66 67
    final FlutterProject flutterProject = _getProject();
    if (flutterProject == null) {
68 69
      return usage;
    }
xster's avatar
xster committed
70
    if (flutterProject.manifest.isModule) {
71
      usage[CustomDimensions.commandBuildAarProjectType] = 'module';
xster's avatar
xster committed
72
    } else if (flutterProject.manifest.isPlugin) {
73
      usage[CustomDimensions.commandBuildAarProjectType] = 'plugin';
74
    } else {
75
      usage[CustomDimensions.commandBuildAarProjectType] = 'app';
76
    }
77
    usage[CustomDimensions.commandBuildAarTargetPlatform] = stringsArg('target-platform').join(',');
78 79 80 81 82
    return usage;
  }

  @override
  final String description = 'Build a repository containing an AAR and a POM file.\n\n'
83 84
      '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'
85
      'To learn more about how to use these artifacts, see '
86
      'https://flutter.dev/go/build-aar';
87 88 89

  @override
  Future<FlutterCommandResult> runCommand() async {
90 91 92
    if (androidSdk == null) {
      exitWithNoSdkMessage();
    }
93
    final Set<AndroidBuildInfo> androidBuildInfo = <AndroidBuildInfo>{};
94 95 96 97 98 99 100 101 102

    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';
103

104
    for (final String buildMode in const <String>['debug', 'profile', 'release']) {
105
      if (boolArg(buildMode)) {
106 107 108 109
        androidBuildInfo.add(AndroidBuildInfo(
          BuildInfo(BuildMode.fromName(buildMode), stringArg('flavor')),
          targetArchs: targetArchitectures,
        ));
110 111 112 113 114
      }
    }
    if (androidBuildInfo.isEmpty) {
      throwToolExit('Please specify a build mode and try again.');
    }
115
    await androidBuilder.buildAar(
116 117 118
      project: _getProject(),
      target: '', // Not needed because this command only builds Android's code.
      androidBuildInfo: androidBuildInfo,
119
      outputDirectoryPath: stringArg('output-dir'),
120
      buildNumber: buildNumber,
121
    );
122
    return FlutterCommandResult.success();
123 124
  }

125
  /// Returns the [FlutterProject] which is determined from the remaining command-line
126 127 128 129 130 131 132 133
  /// argument if any or the current working directory.
  FlutterProject _getProject() {
    if (argResults.rest.isEmpty) {
      return FlutterProject.current();
    }
    return FlutterProject.fromPath(findProjectRoot(argResults.rest.first));
  }
}