-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Add an android migrator to upgrade minSdkVersions 16,17,18 to flutter.minSdkVersion #129729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…nd implementing some test cases
|
|
||
| @override | ||
| String migrateFileContents(String fileContents) { | ||
| return fileContents.replaceAll(minSdk16, flutterMinSdk) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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));
});There was a problem hiding this comment.
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
|
|
||
| @override | ||
| String migrateFileContents(String fileContents) { | ||
| return fileContents.replaceAll(minSdk16, flutterMinSdk) |
There was a problem hiding this comment.
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.
packages/flutter_tools/lib/src/android/migrations/min_sdk_version_migration.dart
Outdated
Show resolved
Hide resolved
gnprice
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat! Comments below.
packages/flutter_tools/test/general.shard/android/android_project_migration_test.dart
Outdated
Show resolved
Hide resolved
packages/flutter_tools/test/general.shard/android/android_project_migration_test.dart
Show resolved
Hide resolved
|
|
||
| @override | ||
| String migrateFileContents(String fileContents) { | ||
| return fileContents.replaceAll(minSdk16, flutterMinSdk) |
There was a problem hiding this comment.
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));
});
packages/flutter_tools/test/general.shard/android/android_project_migration_test.dart
Outdated
Show resolved
Hide resolved
…itch to using regexp for replacement, add test for case of commented minSdkVersion
… flutter.minSdkVersion (flutter/flutter#129729)
… flutter.minSdkVersion (flutter/flutter#129729)
… flutter.minSdkVersion (flutter/flutter#129729)
… flutter.minSdkVersion (flutter/flutter#129729)
… flutter.minSdkVersion (flutter/flutter#129729)
… flutter.minSdkVersion (flutter/flutter#129729)
… flutter.minSdkVersion (flutter/flutter#129729)
… flutter.minSdkVersion (flutter/flutter#129729)
… flutter.minSdkVersion (flutter/flutter#129729)
… flutter.minSdkVersion (flutter/flutter#129729)
… flutter.minSdkVersion (flutter/flutter#129729)
… flutter.minSdkVersion (flutter/flutter#129729)
… flutter.minSdkVersion (flutter/flutter#129729)
… flutter.minSdkVersion (flutter/flutter#129729)
… flutter.minSdkVersion (flutter/flutter#129729)
… flutter.minSdkVersion (flutter/flutter#129729)
… flutter.minSdkVersion (flutter/flutter#129729)
….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.
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
AndroidProjectto 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 viahostAppGradleRoot.childDirectory('app').childFile('build.gradle').Part of the work for deprecating support for the Jelly Bean android apis.
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.