Skip to content

Commit 2eee054

Browse files
authored
[tool] Fix android tests using outdated regexs to modify templates (#159396)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> **Description** While exploring some semi-related stuff, found these 2 tests using outdated regex which does not work because AGP version in modern templates is set in `settings.gradle.kts` and in form of `com.android.application` instead of `com.android.tools.build:gradle`. Apart from that, in `android_plugin_example_app_build_test.dart` deleted all lines regarding version change (instead of comply with new way of declaring plugin) because for a long time it's didn't work anyway: `replaceAll` haven't find any matches and test ran on latest AGP from template. More than that, attempt to adapt this test to modern AGP setup failed because build is not working with AGP < 8 (I lost logs with actual error for this case, but I believe I can reproduce if anyone wants) in `native_assets_agp_version_test`: - Fixed version to comply with AGP versioning format, which is `major.minor.patch`. - Updated regex and version changing logic to work with `com.android.application` format and `settings.gradle.kts` file. I believe that version updating is desired behavior here, unlike in `android_plugin_example_app_build_test.dart`. - Updated `kts` syntax for declaring flavors in `build.gradle.kts` and updated regex-based updating of this file (didn't work previously because there wasn't actual writing to file) didn't list any issues because there're not any regarding these tests (or maybe I just failed to find). In any case, I think that this doesn't require issue because fix is kinda trivial and motivation is clear. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [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]. - [ ] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 0104a81 commit 2eee054

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

packages/flutter_tools/test/integration.shard/android_plugin_example_app_build_test.dart

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,6 @@ void main() {
4545
final Directory exampleAppDir =
4646
tempDir.childDirectory(testName).childDirectory('example');
4747

48-
final File buildGradleFile = exampleAppDir.childDirectory('android').childFile('build.gradle.kts');
49-
expect(buildGradleFile, exists);
50-
51-
final String buildGradle = buildGradleFile.readAsStringSync();
52-
final RegExp androidPluginRegExp =
53-
RegExp(r'com\.android\.tools\.build:gradle:(\d+\.\d+\.\d+)');
54-
55-
// Use AGP 7.2.0
56-
final String newBuildGradle = buildGradle.replaceAll(
57-
androidPluginRegExp, 'com.android.tools.build:gradle:7.2.0');
58-
buildGradleFile.writeAsStringSync(newBuildGradle);
59-
60-
// Run flutter build apk using AGP 7.2.0
6148
result = processManager.runSync(<String>[
6249
flutterBin,
6350
...getLocalEngineArguments(),

packages/flutter_tools/test/integration.shard/isolated/native_assets_agp_version_test.dart

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const String packageName = 'package_with_native_assets';
1616

1717
/// The AGP versions to run these tests against.
1818
final List<String> agpVersions = <String>[
19-
'8.4'
19+
'8.4.0'
2020
];
2121

2222
/// The build modes to target for each flutter command that supports passing
@@ -51,50 +51,59 @@ for (final String agpVersion in agpVersions) {
5151
final Directory packageDirectory = await createTestProject(packageName, tempDirectory);
5252
final Directory exampleDirectory = packageDirectory.childDirectory('example');
5353

54-
File buildGradleFile = exampleDirectory.childDirectory('android').childFile('build.gradle');
55-
if (!buildGradleFile.existsSync()) {
56-
buildGradleFile = exampleDirectory.childDirectory('android').childFile('build.gradle.kts');
57-
}
58-
5954
File appBuildGradleFile = exampleDirectory.childDirectory('android').childDirectory('app').childFile('build.gradle');
6055
if (!appBuildGradleFile.existsSync()) {
6156
appBuildGradleFile = exampleDirectory.childDirectory('android').childDirectory('app').childFile('build.gradle.kts');
6257
}
6358

64-
expect(buildGradleFile, exists);
59+
final File settingsGradleFile = exampleDirectory.childDirectory('android').childFile('settings.gradle.kts');
60+
6561
expect(appBuildGradleFile, exists);
62+
expect(settingsGradleFile, exists);
6663

6764
// Use expected AGP version.
68-
final String buildGradle = buildGradleFile.readAsStringSync();
65+
final String settingsGradle = settingsGradleFile.readAsStringSync();
66+
6967
final RegExp androidPluginRegExp =
70-
RegExp(r'com\.android\.tools\.build:gradle:(\d+\.\d+\.\d+)');
71-
final String newBuildGradle = buildGradle.replaceAll(
72-
androidPluginRegExp, 'com.android.tools.build:gradle:$agpVersion');
73-
buildGradleFile.writeAsStringSync(newBuildGradle);
68+
RegExp(r'id\("com\.android\.application"\)\s+version\s+"([^"]+)"\s+apply\s+false');
69+
expect(androidPluginRegExp.firstMatch(settingsGradle), isNotNull);
70+
71+
final String newSettingsGradle = settingsGradle.replaceAll(
72+
androidPluginRegExp, 'id("com.android.application") version "$agpVersion" apply false');
73+
settingsGradleFile.writeAsStringSync(newSettingsGradle);
7474

7575
// Use Android app with multiple flavors.
7676
final String appBuildGradle = appBuildGradleFile.readAsStringSync().replaceAll('\r\n', '\n');
7777
final RegExp buildTypesBlockRegExp = RegExp(r'buildTypes {\n[ \t]+release {((.|\n)*)\n[ \t]+}\n[ \t]+}');
78-
final String buildTypesBlock = buildTypesBlockRegExp.firstMatch(appBuildGradle)!.toString();
78+
final String buildTypesBlock = buildTypesBlockRegExp.firstMatch(appBuildGradle)![0]!;
7979
final String appBuildGradleSegmentDefiningFlavors = '''
8080
$buildTypesBlock
8181
82-
flavorDimensions "mode"
82+
flavorDimensions += "mode"
8383
8484
productFlavors {
85-
flavorOne {}
86-
flavorTwo {}
87-
flavorThree {}
85+
create("flavorOne") {
86+
dimension = "mode"
87+
}
88+
create("flavorTwo") {
89+
dimension = "mode"
90+
}
91+
create("flavorThree") {
92+
dimension = "mode"
93+
}
8894
}
8995
''';
90-
appBuildGradle.replaceFirst(
96+
final String newAppBuildGradle = appBuildGradle.replaceFirst(
9197
buildTypesBlockRegExp, appBuildGradleSegmentDefiningFlavors);
98+
appBuildGradleFile.writeAsStringSync(newAppBuildGradle);
9299

93100
final ProcessResult result = processManager.runSync(
94101
<String>[
95102
flutterBin,
96103
'build',
97104
'apk',
105+
'--flavor',
106+
'flavorOne',
98107
'--$buildMode',
99108
],
100109
workingDirectory: exampleDirectory.path,

0 commit comments

Comments
 (0)