build_aar.dart 3.09 KB
Newer Older
1 2 3 4 5 6
// Copyright 2019 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 10
import '../base/os.dart';
import '../build_info.dart';
import '../project.dart';
11
import '../reporting/reporting.dart';
12
import '../runner/flutter_command.dart' show FlutterCommandResult;
13 14 15 16 17 18 19 20 21 22
import 'build.dart';

class BuildAarCommand extends BuildSubCommand {
  BuildAarCommand({bool verboseHelp = false}) {
    addBuildModeFlags(verboseHelp: verboseHelp);
    usesFlavorOption();
    usesPubOption();
    argParser
      ..addMultiOption('target-platform',
        splitCommas: true,
23
        defaultsTo: <String>['android-arm', 'android-arm64', 'android-x64'],
24 25 26 27 28 29 30 31 32 33 34 35 36
        allowed: <String>['android-arm', 'android-arm64', 'android-x86', 'android-x64'],
        help: 'The target platform for which the project is compiled.',
      )
      ..addOption('output-dir',
        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';

  @override
37 38
  Future<Map<CustomDimensions, String>> get usageValues async {
    final Map<CustomDimensions, String> usage = <CustomDimensions, String>{};
39 40 41 42 43
    final FlutterProject futterProject = _getProject();
    if (futterProject == null) {
      return usage;
    }
    if (futterProject.manifest.isModule) {
44
      usage[CustomDimensions.commandBuildAarProjectType] = 'module';
45
    } else if (futterProject.manifest.isPlugin) {
46
      usage[CustomDimensions.commandBuildAarProjectType] = 'plugin';
47
    } else {
48
      usage[CustomDimensions.commandBuildAarProjectType] = 'app';
49
    }
50
    usage[CustomDimensions.commandBuildAarTargetPlatform] =
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        (argResults['target-platform'] as List<String>).join(',');
    return usage;
  }

  @override
  final String description = 'Build a repository containing an AAR and a POM file.\n\n'
      'The POM file is used to include the dependencies that the AAR was compiled against.\n\n'
      'To learn more about how to use these artifacts, see '
      'https://docs.gradle.org/current/userguide/repository_types.html#sub:maven_local';

  @override
  Future<FlutterCommandResult> runCommand() async {
    final BuildInfo buildInfo = getBuildInfo();
    final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(buildInfo,
        targetArchs: argResults['target-platform'].map<AndroidArch>(getAndroidArchForName));

67
    await androidBuilder.buildAar(
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
      project: _getProject(),
      target: '', // Not needed because this command only builds Android's code.
      androidBuildInfo: androidBuildInfo,
      outputDir: argResults['output-dir'],
    );
    return null;
  }

  /// Returns the [FlutterProject] which is determinated from the remaining command-line
  /// argument if any or the current working directory.
  FlutterProject _getProject() {
    if (argResults.rest.isEmpty) {
      return FlutterProject.current();
    }
    return FlutterProject.fromPath(findProjectRoot(argResults.rest.first));
  }
}