Skip to content

Commit 229b39e

Browse files
authored
[flutter_tools] Fix so that the value set by --dart-define-from-file can be passed to Gradle (#114297)
* Add dartDefineConfigJsonMap to the checklist * Fix typo * Align formatting with other code * Fix toGradleConfig method
1 parent 338841a commit 229b39e

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

packages/flutter_tools/lib/src/build_info.dart

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,18 +333,16 @@ class BuildInfo {
333333
for (String projectArg in androidProjectArgs)
334334
'-P$projectArg',
335335
];
336-
if(dartDefineConfigJsonMap != null) {
337-
final List<String> items = <String>[];
338-
for (final String gradleConf in result) {
339-
final String key = gradleConf.split('=')[0].substring(2);
340-
if (dartDefineConfigJsonMap!.containsKey(key)) {
336+
if (dartDefineConfigJsonMap != null) {
337+
final Iterable<String> gradleConfKeys = result.map((final String gradleConf) => gradleConf.split('=')[0].substring(2));
338+
dartDefineConfigJsonMap!.forEach((String key, Object value) {
339+
if (gradleConfKeys.contains(key)) {
341340
globals.printWarning(
342341
'The key: [$key] already exists, you cannot use gradle variables that have been used by the system!');
343342
} else {
344-
items.add('-P$key=${dartDefineConfigJsonMap?[key]}');
343+
result.add('-P$key=$value');
345344
}
346-
}
347-
result.addAll(items);
345+
});
348346
}
349347
return result;
350348
}

packages/flutter_tools/test/general.shard/build_info_test.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ void main() {
230230
treeShakeIcons: true,
231231
trackWidgetCreation: true,
232232
dartDefines: <String>['foo=2', 'bar=2'],
233+
dartDefineConfigJsonMap: <String, Object>{'baz': '2'},
233234
dartObfuscation: true,
234235
splitDebugInfoPath: 'foo/',
235236
extraFrontEndOptions: <String>['--enable-experiment=non-nullable', 'bar'],
@@ -252,6 +253,7 @@ void main() {
252253
'-Pcode-size-directory=foo/code-size',
253254
'-Pfoo=bar',
254255
'-Pfizz=bazz',
256+
'-Pbaz=2',
255257
]);
256258
});
257259

@@ -287,7 +289,7 @@ void main() {
287289
treeShakeIcons: true,
288290
trackWidgetCreation: true,
289291
dartDefines: <String>['foo=2', 'bar=2'],
290-
dartDefineConfigJsonMap: <String,Object>{ 'DART_DEFINES' : 'Define a variable, but it occupies the variable name of the system'},
292+
dartDefineConfigJsonMap: <String, Object>{'DART_DEFINES': 'Define a variable, but it occupies the variable name of the system'},
291293
dartObfuscation: true,
292294
);
293295
buildInfo.toEnvironmentConfig();
@@ -313,11 +315,11 @@ void main() {
313315
treeShakeIcons: true,
314316
trackWidgetCreation: true,
315317
dartDefines: <String>['foo=2', 'bar=2'],
316-
dartDefineConfigJsonMap: <String,Object>{ 'dart-defines' : 'Define a variable, but it occupies the variable name of the system'},
318+
dartDefineConfigJsonMap: <String, Object>{'dart-defines': 'Define a variable, but it occupies the variable name of the system'},
317319
dartObfuscation: true,
318320
);
319321
buildInfo.toGradleConfig();
320-
expect(testLogger.warningText, contains('he key: [dart-defines] already exists, you cannot use gradle variables that have been used by the system'));
322+
expect(testLogger.warningText, contains('The key: [dart-defines] already exists, you cannot use gradle variables that have been used by the system'));
321323
});
322324

323325
testUsingContext('toGradleConfig repeated variable with not set', () async {
@@ -326,23 +328,23 @@ void main() {
326328
treeShakeIcons: true,
327329
trackWidgetCreation: true,
328330
dartDefines: <String>['dart-defines=Define a variable, but it occupies the variable name of the system'],
329-
dartDefineConfigJsonMap: <String,Object>{ 'dart-defines' : 'Define a variable, but it occupies the variable name of the system'},
331+
dartDefineConfigJsonMap: <String, Object>{'dart-defines': 'Define a variable, but it occupies the variable name of the system'},
330332
dartObfuscation: true,
331333
);
332334
buildInfo.toGradleConfig();
333-
expect(testLogger.warningText, contains('he key: [dart-defines] already exists, you cannot use gradle variables that have been used by the system'));
335+
expect(testLogger.warningText, contains('The key: [dart-defines] already exists, you cannot use gradle variables that have been used by the system'));
334336
});
335337

336338
testUsingContext('toGradleConfig with androidProjectArgs override gradle project variant', () async {
337339
const BuildInfo buildInfo = BuildInfo(BuildMode.debug, '',
338340
treeShakeIcons: true,
339341
trackWidgetCreation: true,
340342
androidProjectArgs: <String>['applicationId=com.google'],
341-
dartDefineConfigJsonMap: <String,Object>{ 'applicationId' : 'override applicationId'},
343+
dartDefineConfigJsonMap: <String, Object>{'applicationId': 'override applicationId'},
342344
dartObfuscation: true,
343345
);
344346
buildInfo.toGradleConfig();
345-
expect(testLogger.warningText, contains('The key: [applicationId] already exists, you cannot use gradle variables that have been used'));
347+
expect(testLogger.warningText, contains('The key: [applicationId] already exists, you cannot use gradle variables that have been used by the system'));
346348
});
347349

348350
});

0 commit comments

Comments
 (0)