Skip to content

Conversation

@gmackall
Copy link
Member

@gmackall gmackall commented Jun 28, 2023

This migrator will upgrade the minSdkVersion used in the module-level build.gradle file to flutter.minSdkVersion.

The PR also makes a small refactor to AndroidProject to add a getter for the module level build.gradle file, and uses that getter in places where we were getting that file (previously it was being gotten directly via hostAppGradleRoot.childDirectory('app').childFile('build.gradle').

Part of the work for deprecating support for the Jelly Bean android apis.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.


@override
String migrateFileContents(String fileContents) {
return fileContents.replaceAll(minSdk16, flutterMinSdk)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open to thoughts on if we should take a different approach for replacing. This was inspired by the PR that updates the minimum iOS version, but could be replaced with a regex or other method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could rewrite this as a regular expression like 'minSdkVersion [1-9][0-9]+' but that might end up catching future version numbers that we don't want to migrate. It would also match (and thus replace) cases where minSdkVersion is > flutterMinSdk.

I would leave this as is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be reasonable to write this as a regular expression that matches exactly the same set of versions that the three strings match: minSdkVersion 1[678]. I agree it shouldn't use a pattern that matches later versions we don't intend to migrate.


A further step that may be useful, and which a regular expression enables, is to make this a bit more robust against accidentally rewriting matches that might occur in comments or strings. Also against lines like minSdkVersion 1600 which have one of these as a substring but mean something different.

That class of issue is less of a concern for the iOS migration: it's applying to an XML file (so the string to replace has built-in delimiters like <key>/</key>), plus it's an XML file that is made of simple data mostly edited by machine. Here, these Gradle scripts contain a fair bit of logic, and humans do edit them, so comments are natural.

Here's a regexp that takes that further step — it avoids rewriting lines that have anything else on them (except a possible trailing comment), and therefore mean something other than what we're intending to migrate:

final RegExp _oldMinSdkVersionRe = RegExp(r'(?<=^\s*)minSdkVersion 1[678](?=\s*(?://|$))', multiLine: true);

Here's a test that exercises the comments case, and fails with the plain-string-pattern version but passes with that regexp:

      testWithoutContext('avoid rewriting comments', () {
        const String code = '// minSdkVersion 16  // old default\n'
                    '        minSdkVersion 23  // new version';
        project.appGradleFile.writeAsStringSync(sampleModuleGradleBuildFile(code));
        migration.migrate();
        expect(project.appGradleFile.readAsStringSync(), sampleModuleGradleBuildFile(code));
      });

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For additional context (that I should have included in the PR): @reidbaker and I had talked about this offline and decided initially that we were ok taking the simple approach of find and replace, despite it hitting comments, but this argument:

That class of issue is less of a concern for the iOS migration: it's applying to an XML file (so the string to replace has built-in delimiters like /), plus it's an XML file that is made of simple data mostly edited by machine. Here, these Gradle scripts contain a fair bit of logic, and humans do edit them, so comments are natural.

convinces me that taking a regular expression approach similar to the java-gradle migrator is the better way. I'll change to include the regexp and comment-test-case

@gmackall gmackall marked this pull request as ready for review June 28, 2023 18:56

@override
String migrateFileContents(String fileContents) {
return fileContents.replaceAll(minSdk16, flutterMinSdk)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could rewrite this as a regular expression like 'minSdkVersion [1-9][0-9]+' but that might end up catching future version numbers that we don't want to migrate. It would also match (and thus replace) cases where minSdkVersion is > flutterMinSdk.

I would leave this as is.

Copy link
Member

@gnprice gnprice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat! Comments below.


@override
String migrateFileContents(String fileContents) {
return fileContents.replaceAll(minSdk16, flutterMinSdk)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be reasonable to write this as a regular expression that matches exactly the same set of versions that the three strings match: minSdkVersion 1[678]. I agree it shouldn't use a pattern that matches later versions we don't intend to migrate.


A further step that may be useful, and which a regular expression enables, is to make this a bit more robust against accidentally rewriting matches that might occur in comments or strings. Also against lines like minSdkVersion 1600 which have one of these as a substring but mean something different.

That class of issue is less of a concern for the iOS migration: it's applying to an XML file (so the string to replace has built-in delimiters like <key>/</key>), plus it's an XML file that is made of simple data mostly edited by machine. Here, these Gradle scripts contain a fair bit of logic, and humans do edit them, so comments are natural.

Here's a regexp that takes that further step — it avoids rewriting lines that have anything else on them (except a possible trailing comment), and therefore mean something other than what we're intending to migrate:

final RegExp _oldMinSdkVersionRe = RegExp(r'(?<=^\s*)minSdkVersion 1[678](?=\s*(?://|$))', multiLine: true);

Here's a test that exercises the comments case, and fails with the plain-string-pattern version but passes with that regexp:

      testWithoutContext('avoid rewriting comments', () {
        const String code = '// minSdkVersion 16  // old default\n'
                    '        minSdkVersion 23  // new version';
        project.appGradleFile.writeAsStringSync(sampleModuleGradleBuildFile(code));
        migration.migrate();
        expect(project.appGradleFile.readAsStringSync(), sampleModuleGradleBuildFile(code));
      });

Gray Mackall added 2 commits June 29, 2023 14:07
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 16, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 18, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 18, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 18, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 18, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 18, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 18, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 18, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 18, 2023
LouiseHsu pushed a commit to LouiseHsu/flutter that referenced this pull request Jul 31, 2023
….minSdkVersion (flutter#129729)

This migrator will upgrade the minSdkVersion used in the [module-level build.gradle](https://developer.android.com/build#module-level) file to flutter.minSdkVersion. 

The PR also makes a small refactor to `AndroidProject` to add a getter for the module level build.gradle file, and uses that getter in places where we were getting that file (previously it was being gotten directly via `hostAppGradleRoot.childDirectory('app').childFile('build.gradle')`.

Part of the work for deprecating support for the Jelly Bean android apis.
@gmackall gmackall deleted the jelly_deprecation branch January 17, 2024 04:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants