Skip to content

Commit 55dc3f3

Browse files
gmackallGray Mackall
authored and
Gray Mackall
committed
[Reland] Add migrator to upgrade gradle version when conflict with Android Studio bundled Java version is detected (flutter#125836)
This is an attempt to reland flutter#124085. Differences from this attempt and the last: 1. Adds a check for null android studio versions and a test for this case. 2. Wraps the migrate code in a try-catch [per the suggestion here](https://github.com/flutter/flutter/pull/125728/files#r1181747899). Old PR description: This PR adds an android project migrator that checks the version of android studio and the version of gradle for conflicts, and upgrades to 7.4 if a conflict is detected. For more detail about the particular conflict, see flutter#122376. The PR also upgrades older gradle versions being used in integration testing to 7.4. Fixes/related to: flutter#122376 and flutter#123636
1 parent 2f111fd commit 55dc3f3

File tree

23 files changed

+583
-40
lines changed

23 files changed

+583
-40
lines changed

dev/integration_tests/android_custom_host_app/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip

dev/integration_tests/android_embedding_v2_smoke_test/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip

dev/integration_tests/android_host_app_v2_embedding/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip

dev/integration_tests/android_semantics_testing/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip

dev/integration_tests/android_views/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip

dev/integration_tests/external_ui/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip

dev/integration_tests/gradle_deprecated_settings/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip

dev/integration_tests/hybrid_android_views/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip

dev/integration_tests/module_host_with_custom_build_v2_embedding/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip

dev/integration_tests/non_nullable/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip

dev/integration_tests/platform_interaction/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip

dev/integration_tests/release_smoke_test/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip

dev/manual_tests/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip

dev/tracing_tests/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ import '../project.dart';
3131
import '../reporting/reporting.dart';
3232
import 'android_builder.dart';
3333
import 'android_sdk.dart';
34+
import 'android_studio.dart';
3435
import 'gradle_errors.dart';
3536
import 'gradle_utils.dart';
37+
import 'migrations/android_studio_java_gradle_conflict_migration.dart';
3638
import 'migrations/top_level_gradle_build_file_migration.dart';
3739
import 'multidex.dart';
3840

@@ -137,13 +139,16 @@ class AndroidGradleBuilder implements AndroidBuilder {
137139
required Usage usage,
138140
required GradleUtils gradleUtils,
139141
required Platform platform,
142+
required AndroidStudio? androidStudio,
140143
}) : _logger = logger,
141144
_fileSystem = fileSystem,
142145
_artifacts = artifacts,
143146
_usage = usage,
144147
_gradleUtils = gradleUtils,
148+
_androidStudio = androidStudio,
145149
_fileSystemUtils = FileSystemUtils(fileSystem: fileSystem, platform: platform),
146-
_processUtils = ProcessUtils(logger: logger, processManager: processManager);
150+
_processUtils = ProcessUtils(logger: logger, processManager: processManager),
151+
_platform = platform;
147152

148153
final Logger _logger;
149154
final ProcessUtils _processUtils;
@@ -152,6 +157,8 @@ class AndroidGradleBuilder implements AndroidBuilder {
152157
final Usage _usage;
153158
final GradleUtils _gradleUtils;
154159
final FileSystemUtils _fileSystemUtils;
160+
final AndroidStudio? _androidStudio;
161+
final Platform _platform;
155162

156163
/// Builds the AAR and POM files for the current Flutter module or plugin.
157164
@override
@@ -256,6 +263,15 @@ class AndroidGradleBuilder implements AndroidBuilder {
256263

257264
final List<ProjectMigrator> migrators = <ProjectMigrator>[
258265
TopLevelGradleBuildFileMigration(project.android, _logger),
266+
AndroidStudioJavaGradleConflictMigration(_logger,
267+
project: project.android,
268+
androidStudio: _androidStudio,
269+
fileSystem: _fileSystem,
270+
processUtils: _processUtils,
271+
platform: _platform,
272+
os: globals.os,
273+
androidSdk: globals.androidSdk)
274+
,
259275
];
260276

261277
final ProjectMigration migration = ProjectMigration(migrators);

packages/flutter_tools/lib/src/android/gradle_utils.dart

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ const String maxKnownAgpVersion = '8.1';
6060
final RegExp _androidGradlePluginRegExp =
6161
RegExp(r'com\.android\.tools\.build:gradle:(\d+\.\d+\.\d+)');
6262

63+
// Expected content format (with lines above and below).
64+
// Version can have 2 or 3 numbers.
65+
// 'distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip'
66+
// '^\s*' protects against commented out lines.
67+
final RegExp distributionUrlRegex =
68+
RegExp(r'^\s*distributionUrl\s*=\s*.*\.zip', multiLine: true);
69+
70+
// Modified version of the gradle distribution url match designed to only match
71+
// gradle.org urls so that we can guarantee any modifications to the url
72+
// still points to a hosted zip.
73+
final RegExp gradleOrgVersionMatch =
74+
RegExp(
75+
r'^\s*distributionUrl\s*=\s*https\\://services\.gradle\.org/distributions/gradle-((?:\d|\.)+)-(.*)\.zip',
76+
multiLine: true
77+
);
78+
6379
// From https://docs.gradle.org/current/userguide/command_line_interface.html#command_line_interface
6480
const String gradleVersionFlag = r'--version';
6581

@@ -171,41 +187,49 @@ String getGradleVersionForAndroidPlugin(Directory directory, Logger logger) {
171187
return getGradleVersionFor(androidPluginVersion ?? 'unknown');
172188
}
173189

190+
/// Returns the gradle file from the top level directory.
191+
/// The returned file is not guaranteed to be present.
192+
File getGradleWrapperFile(Directory directory) {
193+
return directory.childDirectory(gradleDirectoryName)
194+
.childDirectory(gradleWrapperDirectoryName)
195+
.childFile(gradleWrapperPropertiesFilename);
196+
}
197+
198+
/// Parses the gradle wrapper distribution url to return a string containing
199+
/// the version number.
200+
///
201+
/// Expected input is of the form '...gradle-7.4.2-all.zip', and the output
202+
/// would be of the form '7.4.2'.
203+
String? parseGradleVersionFromDistributionUrl(String? distributionUrl) {
204+
if (distributionUrl == null) {
205+
return null;
206+
}
207+
final List<String> zipParts = distributionUrl.split('-');
208+
if (zipParts.length < 2) {
209+
return null;
210+
}
211+
return zipParts[1];
212+
}
213+
174214
/// Returns either the gradle-wrapper.properties value from the passed in
175215
/// [directory] or if not present the version available in local path.
176216
///
177217
/// If gradle version is not found null is returned.
178-
/// [directory] should be and android directory with a build.gradle file.
218+
/// [directory] should be an android directory with a build.gradle file.
179219
Future<String?> getGradleVersion(
180220
Directory directory, Logger logger, ProcessManager processManager) async {
181-
final File propertiesFile = directory
182-
.childDirectory(gradleDirectoryName)
183-
.childDirectory(gradleWrapperDirectoryName)
184-
.childFile(gradleWrapperPropertiesFilename);
221+
final File propertiesFile = getGradleWrapperFile(directory);
185222

186223
if (propertiesFile.existsSync()) {
187224
final String wrapperFileContent = propertiesFile.readAsStringSync();
188225

189-
// Expected content format (with lines above and below).
190-
// Version can have 2 or 3 numbers.
191-
// 'distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip'
192-
final RegExp distributionUrlRegex =
193-
RegExp(r'distributionUrl\s?=\s?.*\.zip');
194-
195226
final RegExpMatch? distributionUrl =
196227
distributionUrlRegex.firstMatch(wrapperFileContent);
197228
if (distributionUrl != null) {
198-
// Expected content: 'gradle-7.4.2-all.zip'
199-
final String? gradleZip = distributionUrl.group(0);
200-
if (gradleZip != null) {
201-
final List<String> zipParts = gradleZip.split('-');
202-
if (zipParts.length > 2) {
203-
final String gradleVersion = zipParts[1];
204-
return gradleVersion;
205-
} else {
206-
// Did not find gradle zip url. Likely this is a bug in our parsing.
207-
logger.printWarning(_formatParseWarning(wrapperFileContent));
208-
}
229+
final String? gradleVersion =
230+
parseGradleVersionFromDistributionUrl(distributionUrl.group(0));
231+
if (gradleVersion != null) {
232+
return gradleVersion;
209233
} else {
210234
// Did not find gradle zip url. Likely this is a bug in our parsing.
211235
logger.printWarning(_formatParseWarning(wrapperFileContent));
@@ -249,7 +273,7 @@ OS: Mac OS X 13.2.1 aarch64
249273
final RegExpMatch? version =
250274
gradleVersionRegex.firstMatch(gradleVersionVerbose);
251275
if (version == null) {
252-
// Most likley a bug in our parse implementation/regex.
276+
// Most likely a bug in our parse implementation/regex.
253277
logger.printWarning(_formatParseWarning(gradleVersionVerbose));
254278
return null;
255279
}
@@ -301,7 +325,7 @@ String _formatParseWarning(String content) {
301325
//
302326
// Source of truth found here:
303327
// https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
304-
// AGP has a minimim version of gradle required but no max starting at
328+
// AGP has a minimum version of gradle required but no max starting at
305329
// AGP version 2.3.0+.
306330
bool validateGradleAndAgp(Logger logger,
307331
{required String? gradleV, required String? agpV}) {

0 commit comments

Comments
 (0)