Skip to content

Commit e9c242e

Browse files
[tool] Migrate BuildAarCommand to modular dependency injection (#192247)
## Summary Part 14c of the modular dependency injection migration. Migrates `BuildAarCommand` (`flutter build aar`) to modular dependency injection with `ToolContext` and `AndroidContext`, removing direct dependencies on `globals.dart`. Stacked on #192246 Compare: bkonyi/flutter@master...di/14c-build-aar Part of #47161 --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 19946f9 commit e9c242e

5 files changed

Lines changed: 197 additions & 162 deletions

File tree

‎dev/devicelab/bin/tasks/external_textures_integration_test_ios.dart‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ Future<void> main() async {
1818
String deviceId,
1919
) async {
2020
simulatorDeviceId = deviceId;
21-
result = await createExternalTexturesFrameRateIntegrationTest(
22-
deviceIdOverride: deviceId,
23-
).call();
21+
result = await createExternalTexturesFrameRateIntegrationTest(deviceIdOverride: deviceId)
22+
.call();
2423
});
2524
} finally {
2625
if (simulatorDeviceId != null) {

‎packages/flutter_tools/lib/src/commands/build.dart‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'package:meta/meta.dart';
66
import 'package:process/process.dart';
77

88
import '../android/android_builder.dart';
9-
import '../android/android_sdk.dart';
109
import '../android/gradle.dart';
1110
import '../artifacts.dart';
1211
import '../base/file_system.dart';
@@ -60,8 +59,7 @@ class BuildCommand extends FlutterCommand {
6059
:Platform platform,
6160
:ProcessManager processManager,
6261
) = toolContext;
63-
final AppleContext(:Xcode? xcode) = appleContext;
64-
final AndroidContext(:AndroidSdk? androidSdk) = androidContext;
62+
final Xcode xcode = appleContext.xcode;
6563

6664
final codesign = DarwinAddToAppCodesigning.fromContexts(
6765
appleContext: appleContext,
@@ -76,9 +74,10 @@ class BuildCommand extends FlutterCommand {
7674
);
7775
_addSubcommand(
7876
BuildAarCommand(
79-
androidSdk: androidSdk,
80-
fileSystem: fileSystem,
81-
logger: logger,
77+
androidBuilder: effectiveAndroidBuilder,
78+
androidContext: androidContext,
79+
buildSystem: buildSystem,
80+
toolContext: toolContext,
8281
verboseHelp: verboseHelp,
8382
),
8483
);

‎packages/flutter_tools/lib/src/commands/build_aar.dart‎

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:meta/meta.dart';
56
import 'package:unified_analytics/unified_analytics.dart';
67

78
import '../android/android_builder.dart';
@@ -11,18 +12,21 @@ import '../base/common.dart';
1112
import '../base/file_system.dart';
1213
import '../base/os.dart';
1314
import '../build_info.dart';
14-
import '../globals.dart' as globals;
15+
import '../build_system/build_system.dart';
16+
import '../context/android_context.dart';
17+
import '../context/tool_context.dart';
1518
import '../project.dart';
1619
import '../runner/flutter_command.dart';
1720
import 'build.dart';
1821

1922
class BuildAarCommand extends BuildSubCommand {
2023
BuildAarCommand({
21-
required super.logger,
22-
required this._androidSdk,
23-
required this._fileSystem,
24-
required super.verboseHelp,
25-
}) {
24+
required this._androidBuilder,
25+
required this._androidContext,
26+
required this._buildSystem,
27+
required ToolContext toolContext,
28+
super.verboseHelp = false,
29+
}) : super(logger: toolContext.logger, toolContext: toolContext) {
2630
enableUsesPubOption();
2731
registerOptionBundles(const <OptionBundle>[
2832
DartCompileOptionsBundle(),
@@ -70,8 +74,31 @@ class BuildAarCommand extends BuildSubCommand {
7074
allowed: <String>['android-arm', 'android-arm64', 'android-x64'],
7175
help: 'The target platform for which the project is compiled.',
7276
);
73-
final AndroidSdk? _androidSdk;
74-
final FileSystem _fileSystem;
77+
78+
final AndroidBuilder _androidBuilder;
79+
final AndroidContext _androidContext;
80+
final BuildSystem _buildSystem;
81+
82+
/// The [AndroidBuilder] used to build the AAR.
83+
@visibleForTesting
84+
AndroidBuilder get androidBuilder => _androidBuilder;
85+
86+
/// The [AndroidContext] containing Android-specific toolchain dependencies.
87+
@visibleForTesting
88+
AndroidContext get androidContext => _androidContext;
89+
90+
/// The [AndroidSdk] instance, if available.
91+
@visibleForTesting
92+
AndroidSdk? get androidSdk => _androidContext.androidSdk;
93+
94+
/// The [BuildSystem] used for building the project.
95+
@visibleForTesting
96+
BuildSystem get buildSystem => _buildSystem;
97+
98+
@override
99+
ToolContext get toolContext => super.toolContext!;
100+
101+
FileSystem get _fileSystem => toolContext.fs;
75102

76103
@override
77104
final name = 'aar';
@@ -126,7 +153,7 @@ class BuildAarCommand extends BuildSubCommand {
126153

127154
@override
128155
Future<FlutterCommandResult> runCommand() async {
129-
if (_androidSdk == null) {
156+
if (_androidContext.androidSdk == null) {
130157
exitWithNoSdkMessage(analytics: analytics, logger: logger);
131158
}
132159
final androidBuildInfo = <AndroidBuildInfo>{};
@@ -157,7 +184,7 @@ class BuildAarCommand extends BuildSubCommand {
157184
throwToolExit('Please specify a build mode and try again.');
158185
}
159186

160-
await androidBuilder?.buildAar(
187+
await _androidBuilder.buildAar(
161188
project: project,
162189
target: targetFile.path,
163190
androidBuildInfo: androidBuildInfo,
@@ -170,7 +197,7 @@ class BuildAarCommand extends BuildSubCommand {
170197
final buildLabel = impellerEnabled
171198
? 'manifest-aar-impeller-enabled'
172199
: 'manifest-aar-impeller-disabled';
173-
globals.analytics.send(Event.flutterBuildInfo(label: buildLabel, buildType: 'android'));
200+
analytics.send(Event.flutterBuildInfo(label: buildLabel, buildType: 'android'));
174201

175202
return FlutterCommandResult.success();
176203
}
@@ -197,6 +224,6 @@ class BuildAarCommand extends BuildSubCommand {
197224
if (projectRoot == null) {
198225
throwToolExit('${mainFile.parent.path} is not a valid flutter project');
199226
}
200-
return FlutterProject.fromDirectory(_fileSystem.directory(projectRoot));
227+
return toolContext.projectFactory.fromDirectory(_fileSystem.directory(projectRoot));
201228
}
202229
}

‎packages/flutter_tools/test/commands.shard/hermetic/build_aar_test.dart‎

Lines changed: 78 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ import 'package:flutter_tools/src/base/logger.dart';
88
import 'package:flutter_tools/src/base/platform.dart';
99
import 'package:flutter_tools/src/build_system/build_system.dart';
1010
import 'package:flutter_tools/src/cache.dart';
11-
import 'package:flutter_tools/src/commands/build.dart';
11+
import 'package:flutter_tools/src/commands/build_aar.dart';
12+
import 'package:flutter_tools/src/features.dart';
13+
import 'package:flutter_tools/src/project.dart';
1214
import 'package:unified_analytics/unified_analytics.dart';
1315

16+
import '../../src/android_common.dart';
1417
import '../../src/common.dart';
1518
import '../../src/context.dart';
16-
import '../../src/fake_build_command.dart';
19+
1720
import '../../src/fake_process_manager.dart';
1821
import '../../src/fakes.dart';
1922
import '../../src/test_build_system.dart';
@@ -26,6 +29,8 @@ void main() {
2629
late Platform platform;
2730
late Cache cache;
2831
late FakeAnalytics fakeAnalytics;
32+
late FakeToolContext toolContext;
33+
late FakeAndroidContext androidContext;
2934

3035
setUpAll(() {
3136
Cache.disableLocking();
@@ -43,12 +48,19 @@ void main() {
4348
fs: fs,
4449
fakeFlutterVersion: FakeFlutterVersion(),
4550
);
51+
toolContext = FakeToolContext(
52+
cache: cache,
53+
fs: fs,
54+
logger: logger,
55+
platform: platform,
56+
processManager: processManager,
57+
projectFactory: FlutterProjectFactory(fileSystem: fs, logger: logger),
58+
);
59+
androidContext = FakeAndroidContext(androidSdk: FakeAndroidSdk());
4660
});
4761

48-
testUsingContext(
49-
'will not build an AAR for a plugin',
50-
() async {
51-
fs.file('pubspec.yaml').writeAsStringSync('''
62+
testWithoutContext('will not build an AAR for a plugin', () async {
63+
fs.file('pubspec.yaml').writeAsStringSync('''
5264
name: foo_bar
5365
5466
flutter:
@@ -58,114 +70,71 @@ flutter:
5870
null
5971
''');
6072

61-
final BuildCommand command = createFakeBuildCommand(
62-
androidSdk: FakeAndroidSdk(),
63-
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
64-
fileSystem: fs,
65-
logger: logger,
66-
osUtils: FakeOperatingSystemUtils(),
67-
config: FakeConfig(),
68-
platform: FakePlatform(),
69-
fileSystemUtils: FakeFileSystemUtils(),
70-
terminal: FakeTerminal(),
71-
plistParser: FakePlistParser(),
72-
processUtils: FakeProcessUtils(),
73-
processManager: FakeProcessManager.any(),
74-
templateRenderer: FakeTemplateRenderer(),
75-
xcode: FakeXcode(),
76-
artifacts: FakeArtifacts(),
77-
cache: FakeCache(),
78-
flutterVersion: FakeFlutterVersion(),
79-
featureFlags: TestFeatureFlags(),
80-
);
81-
82-
expect(
83-
createTestCommandRunner(command).run(const <String>['build', 'aar', '--no-pub']),
84-
throwsToolExit(message: 'AARs can only be built from modules'),
85-
);
86-
expect(processManager, hasNoRemainingExpectations);
87-
},
88-
overrides: <Type, Generator>{
89-
Cache: () => cache,
90-
FileSystem: () => fs,
91-
Platform: () => platform,
92-
ProcessManager: () => processManager,
93-
},
94-
);
95-
96-
testUsingContext(
97-
'will build an AAR for a module',
98-
() async {
99-
fs.file('pubspec.yaml').writeAsStringSync('''
73+
final command = BuildAarCommand(
74+
androidBuilder: FakeAndroidBuilder(),
75+
androidContext: androidContext,
76+
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
77+
toolContext: toolContext,
78+
);
79+
80+
expect(
81+
createTestCommandRunner(command).run(const <String>['aar', '--no-pub']),
82+
throwsToolExit(message: 'AARs can only be built from modules'),
83+
);
84+
expect(processManager, hasNoRemainingExpectations);
85+
});
86+
87+
testUsingContext('will build an AAR for a module', () async {
88+
fs.file('pubspec.yaml').writeAsStringSync('''
10089
name: foo_bar
10190
10291
flutter:
10392
module:
10493
foo: bar
10594
''');
106-
final Directory dotAndroidDir = fs.directory('.android')..createSync(recursive: true);
107-
dotAndroidDir.childFile('gradlew').createSync();
108-
109-
processManager.addCommands(<FakeCommand>[
110-
const FakeCommand(command: <String>['chmod', '755', 'flutter/bin/cache/artifacts']),
111-
const FakeCommand(command: <String>['which', 'java']),
112-
...<String>['Debug', 'Profile', 'Release'].map(
113-
(String buildMode) => FakeCommand(
114-
command: <Pattern>[
115-
'/.android/gradlew',
116-
'-I=/flutter/packages/flutter_tools/gradle/aar_init_script.gradle',
117-
...List<RegExp>.filled(4, RegExp(r'-P[a-zA-Z-]+=.*')),
118-
'-q',
119-
...List<RegExp>.filled(7, RegExp(r'-P[a-zA-Z-]+=.*')),
120-
'assembleAar$buildMode',
121-
],
122-
onRun: (_) => fs.directory('/build/host/outputs/repo').createSync(recursive: true),
123-
),
124-
),
125-
]);
126-
127-
cache.getArtifactDirectory('gradle_wrapper').createSync(recursive: true);
128-
129-
final BuildCommand command = createFakeBuildCommand(
130-
androidSdk: FakeAndroidSdk(),
131-
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
132-
fileSystem: fs,
133-
logger: logger,
134-
osUtils: FakeOperatingSystemUtils(),
135-
config: FakeConfig(),
136-
platform: FakePlatform(),
137-
fileSystemUtils: FakeFileSystemUtils(),
138-
terminal: FakeTerminal(),
139-
plistParser: FakePlistParser(),
140-
processUtils: FakeProcessUtils(),
141-
processManager: FakeProcessManager.any(),
142-
templateRenderer: FakeTemplateRenderer(),
143-
xcode: FakeXcode(),
144-
artifacts: FakeArtifacts(),
145-
cache: FakeCache(),
146-
flutterVersion: FakeFlutterVersion(),
147-
featureFlags: TestFeatureFlags(),
148-
);
149-
150-
await createTestCommandRunner(command).run(const <String>['build', 'aar', '--no-pub']);
151-
expect(processManager, hasNoRemainingExpectations);
152-
expect(
153-
fakeAnalytics.sentEvents,
154-
contains(
155-
Event.commandUsageValues(
156-
workflow: 'build/aar',
157-
commandHasTerminal: false,
158-
buildAarProjectType: 'module',
159-
buildAarTargetPlatform: 'android-arm,android-arm64,android-x64',
160-
),
95+
final Directory dotAndroidDir = fs.directory('.android')..createSync(recursive: true);
96+
dotAndroidDir.childFile('gradlew').createSync();
97+
98+
final command = BuildAarCommand(
99+
androidBuilder: FakeAndroidBuilder(),
100+
androidContext: androidContext,
101+
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
102+
toolContext: toolContext,
103+
);
104+
105+
await createTestCommandRunner(command, fakeAnalytics).run(const <String>['aar', '--no-pub']);
106+
expect(
107+
fakeAnalytics.sentEvents,
108+
contains(
109+
Event.commandUsageValues(
110+
workflow: 'aar',
111+
commandHasTerminal: false,
112+
buildAarProjectType: 'module',
113+
buildAarTargetPlatform: 'android-arm,android-arm64,android-x64',
161114
),
162-
);
163-
},
164-
overrides: <Type, Generator>{
165-
FileSystem: () => fs,
166-
Platform: () => platform,
167-
ProcessManager: () => processManager,
168-
Analytics: () => fakeAnalytics,
169-
},
170-
);
115+
),
116+
);
117+
}, overrides: <Type, Generator>{FeatureFlags: () => TestFeatureFlags()});
118+
119+
testWithoutContext('throws ToolExit if androidSdk is null', () async {
120+
fs.file('pubspec.yaml').writeAsStringSync('''
121+
name: foo_bar
122+
123+
flutter:
124+
module:
125+
foo: bar
126+
''');
127+
128+
final command = BuildAarCommand(
129+
androidBuilder: FakeAndroidBuilder(),
130+
androidContext: FakeAndroidContext(),
131+
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
132+
toolContext: toolContext,
133+
);
134+
135+
expect(
136+
createTestCommandRunner(command).run(const <String>['aar', '--no-pub']),
137+
throwsToolExit(message: 'No Android SDK found'),
138+
);
139+
});
171140
}

0 commit comments

Comments
 (0)