1// Copyright 2019 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import 'dart:async'; 6 7import '../android/aar.dart'; 8import '../base/context.dart'; 9import '../base/os.dart'; 10import '../build_info.dart'; 11import '../project.dart'; 12import '../reporting/reporting.dart'; 13import '../runner/flutter_command.dart' show DevelopmentArtifact, FlutterCommandResult; 14import 'build.dart'; 15 16/// The AAR builder in the current context. 17AarBuilder get aarBuilder => context.get<AarBuilder>() ?? AarBuilderImpl(); 18 19class BuildAarCommand extends BuildSubCommand { 20 BuildAarCommand({bool verboseHelp = false}) { 21 addBuildModeFlags(verboseHelp: verboseHelp); 22 usesFlavorOption(); 23 usesPubOption(); 24 argParser 25 ..addMultiOption('target-platform', 26 splitCommas: true, 27 defaultsTo: <String>['android-arm', 'android-arm64'], 28 allowed: <String>['android-arm', 'android-arm64', 'android-x86', 'android-x64'], 29 help: 'The target platform for which the project is compiled.', 30 ) 31 ..addOption('output-dir', 32 help: 'The absolute path to the directory where the repository is generated.' 33 'By default, this is \'<current-directory>android/build\'. ', 34 ); 35 } 36 37 @override 38 final String name = 'aar'; 39 40 @override 41 Future<Map<CustomDimensions, String>> get usageValues async { 42 final Map<CustomDimensions, String> usage = <CustomDimensions, String>{}; 43 final FlutterProject futterProject = _getProject(); 44 if (futterProject == null) { 45 return usage; 46 } 47 if (futterProject.manifest.isModule) { 48 usage[CustomDimensions.commandBuildAarProjectType] = 'module'; 49 } else if (futterProject.manifest.isPlugin) { 50 usage[CustomDimensions.commandBuildAarProjectType] = 'plugin'; 51 } else { 52 usage[CustomDimensions.commandBuildAarProjectType] = 'app'; 53 } 54 usage[CustomDimensions.commandBuildAarTargetPlatform] = 55 (argResults['target-platform'] as List<String>).join(','); 56 return usage; 57 } 58 59 @override 60 Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{ 61 DevelopmentArtifact.universal, 62 DevelopmentArtifact.android, 63 }; 64 65 @override 66 final String description = 'Build a repository containing an AAR and a POM file.\n\n' 67 'The POM file is used to include the dependencies that the AAR was compiled against.\n\n' 68 'To learn more about how to use these artifacts, see ' 69 'https://docs.gradle.org/current/userguide/repository_types.html#sub:maven_local'; 70 71 @override 72 Future<FlutterCommandResult> runCommand() async { 73 final BuildInfo buildInfo = getBuildInfo(); 74 final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(buildInfo, 75 targetArchs: argResults['target-platform'].map<AndroidArch>(getAndroidArchForName)); 76 77 await aarBuilder.build( 78 project: _getProject(), 79 target: '', // Not needed because this command only builds Android's code. 80 androidBuildInfo: androidBuildInfo, 81 outputDir: argResults['output-dir'], 82 ); 83 return null; 84 } 85 86 /// Returns the [FlutterProject] which is determinated from the remaining command-line 87 /// argument if any or the current working directory. 88 FlutterProject _getProject() { 89 if (argResults.rest.isEmpty) { 90 return FlutterProject.current(); 91 } 92 return FlutterProject.fromPath(findProjectRoot(argResults.rest.first)); 93 } 94} 95