Skip to content

Commit 89af7cd

Browse files
Parse AndroidX property in gradle.properties (flutter#188372)
Parses `android.useAndroidX` from `gradle.properties` instead of using a raw substring match. This avoids false positives from commented-out lines such as `#android.useAndroidX=true`, and false negatives for valid Gradle syntax such as `android.useAndroidX = true`. This PR intentionally keeps the change scoped to property parsing and does not change the broader behavior for projects that omit the property entirely. Part of flutter#188306 ## Tests ```console ./bin/flutter test packages/flutter_tools/test/general.shard/android/gradle_test.dart ``` ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`), or this change does not require documentation updates. - [x] I added new tests to check the change I am making. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported, or this change is not a breaking change. - [x] All existing and new tests are passing. --------- Co-authored-by: jesswrd <[email protected]>
1 parent 974dc3b commit 89af7cd

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

‎packages/flutter_tools/lib/src/android/gradle.dart‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,25 @@ bool isAppUsingAndroidX(Directory androidDirectory) {
11281128
if (!properties.existsSync()) {
11291129
return false;
11301130
}
1131-
return properties.readAsStringSync().contains('android.useAndroidX=true');
1131+
bool? usesAndroidX;
1132+
final androidXRegExp = RegExp(r'^android\.useAndroidX(?:\s*[=:]\s*|\s+)(\S+)');
1133+
for (final String rawLine in properties.readAsLinesSync()) {
1134+
final String line = rawLine.trimLeft();
1135+
if (line.isEmpty || line.startsWith('#') || line.startsWith('!')) {
1136+
continue;
1137+
}
1138+
final RegExpMatch? match = androidXRegExp.firstMatch(line);
1139+
if (match == null) {
1140+
continue;
1141+
}
1142+
final String value = match.group(1)!.toLowerCase();
1143+
if (value == 'true') {
1144+
usesAndroidX = true;
1145+
} else if (value == 'false') {
1146+
usesAndroidX = false;
1147+
}
1148+
}
1149+
return usesAndroidX ?? false;
11321150
}
11331151

11341152
/// Returns the APK files for a given [FlutterProject] and [AndroidBuildInfo].

‎packages/flutter_tools/test/general.shard/android/gradle_test.dart‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,44 @@ flutter:
631631
ProcessManager: () => FakeProcessManager.any(),
632632
},
633633
);
634+
635+
testUsingContext(
636+
'returns false for commented-out AndroidX properties',
637+
() async {
638+
final Directory androidDirectory = globals.fs.systemTempDirectory.createTempSync(
639+
'flutter_android.',
640+
);
641+
642+
androidDirectory
643+
.childFile('gradle.properties')
644+
.writeAsStringSync('#android.useAndroidX=true');
645+
646+
expect(isAppUsingAndroidX(androidDirectory), isFalse);
647+
},
648+
overrides: <Type, Generator>{
649+
FileSystem: () => fs,
650+
ProcessManager: () => FakeProcessManager.any(),
651+
},
652+
);
653+
654+
testUsingContext(
655+
'returns true when AndroidX property has spaces around the separator',
656+
() async {
657+
final Directory androidDirectory = globals.fs.systemTempDirectory.createTempSync(
658+
'flutter_android.',
659+
);
660+
661+
androidDirectory
662+
.childFile('gradle.properties')
663+
.writeAsStringSync('android.useAndroidX = true');
664+
665+
expect(isAppUsingAndroidX(androidDirectory), isTrue);
666+
},
667+
overrides: <Type, Generator>{
668+
FileSystem: () => fs,
669+
ProcessManager: () => FakeProcessManager.any(),
670+
},
671+
);
634672
});
635673

636674
group('printHowToConsumeAar', () {

0 commit comments

Comments
 (0)