@@ -71,19 +71,31 @@ const String maxKnownAgpVersion = '8.3';
71
71
// compatible Java version.
72
72
const String oldestDocumentedJavaAgpCompatibilityVersion = '4.2' ;
73
73
74
+ // Constant used in [_buildAndroidGradlePluginRegExp] and
75
+ // [_settingsAndroidGradlePluginRegExp] to identify the version section.
76
+ const String _versionGroupName = 'version' ;
77
+
78
+ // AGP can be defined in build.gradle
74
79
// Expected content:
75
80
// "classpath 'com.android.tools.build:gradle:7.3.0'"
76
- // Parentheticals are use to group which helps with version extraction.
77
- // "...build:gradle:(...)" where group(1) should be the version string.
78
- final RegExp _androidGradlePluginRegExp =
79
- RegExp (r'com\.android\.tools\.build:gradle:(\d+\.\d+\.\d+)' );
81
+ // ?<version> is used to name the version group which helps with extraction.
82
+ final RegExp _buildAndroidGradlePluginRegExp =
83
+ RegExp (r'com\.android\.tools\.build:gradle:(?<version>\d+\.\d+\.\d+)' );
84
+
85
+ // AGP can be defined in settings.gradle.
86
+ // Expected content:
87
+ // "id "com.android.application" version "{{agpVersion}}""
88
+ // ?<version> is used to name the version group which helps with extraction.
89
+ final RegExp _settingsAndroidGradlePluginRegExp = RegExp (
90
+ r'^\s+id\s+"com.android.application"\s+version\s+"(?<version>\d+\.\d+\.\d+)"' ,
91
+ multiLine: true );
80
92
81
93
// Expected content format (with lines above and below).
82
94
// Version can have 2 or 3 numbers.
83
95
// 'distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip'
84
96
// '^\s*' protects against commented out lines.
85
97
final RegExp distributionUrlRegex =
86
- RegExp (r'^\s*distributionUrl\s*=\s*.*\.zip' , multiLine: true );
98
+ RegExp (r'^\s*distributionUrl\s*=\s*.*\.zip' , multiLine: true );
87
99
88
100
// Modified version of the gradle distribution url match designed to only match
89
101
// gradle.org urls so that we can guarantee any modifications to the url
@@ -199,7 +211,7 @@ String getGradleVersionForAndroidPlugin(Directory directory, Logger logger) {
199
211
return templateDefaultGradleVersion;
200
212
}
201
213
final String buildFileContent = buildFile.readAsStringSync ();
202
- final Iterable <Match > pluginMatches = _androidGradlePluginRegExp .allMatches (buildFileContent);
214
+ final Iterable <Match > pluginMatches = _buildAndroidGradlePluginRegExp .allMatches (buildFileContent);
203
215
if (pluginMatches.isEmpty) {
204
216
logger.printTrace ("$buildFile doesn't provide an AGP version, assuming Gradle version: $templateDefaultGradleVersion " );
205
217
return templateDefaultGradleVersion;
@@ -309,24 +321,44 @@ OS: Mac OS X 13.2.1 aarch64
309
321
/// Returns the Android Gradle Plugin (AGP) version that the current project
310
322
/// depends on when found, null otherwise.
311
323
///
312
- /// The Android plugin version is specified in the [build.gradle] file within
313
- /// the project's Android directory ([androidDirectory] ).
324
+ /// The Android plugin version is specified in the [build.gradle] or
325
+ /// [settings.gradle] file within the project's
326
+ /// Android directory ([androidDirectory] ).
314
327
String ? getAgpVersion (Directory androidDirectory, Logger logger) {
315
328
final File buildFile = androidDirectory.childFile ('build.gradle' );
316
329
if (! buildFile.existsSync ()) {
317
330
logger.printTrace ('Can not find build.gradle in $androidDirectory ' );
318
331
return null ;
319
332
}
320
333
final String buildFileContent = buildFile.readAsStringSync ();
321
- final Iterable <Match > pluginMatches =
322
- _androidGradlePluginRegExp.allMatches (buildFileContent);
323
- if (pluginMatches.isEmpty) {
324
- logger.printTrace ("$buildFile doesn't provide an AGP version" );
334
+ final RegExpMatch ? buildMatch =
335
+ _buildAndroidGradlePluginRegExp.firstMatch (buildFileContent);
336
+ if (buildMatch != null ) {
337
+ final String ? androidPluginVersion =
338
+ buildMatch.namedGroup (_versionGroupName);
339
+ logger.printTrace ('$buildFile provides AGP version: $androidPluginVersion ' );
340
+ return androidPluginVersion;
341
+ }
342
+ logger.printTrace (
343
+ "$buildFile doesn't provide an AGP version. Checking settings." );
344
+ final File settingsFile = androidDirectory.childFile ('settings.gradle' );
345
+ if (! settingsFile.existsSync ()) {
346
+ logger.printTrace ('$settingsFile does not exist.' );
325
347
return null ;
326
348
}
327
- final String ? androidPluginVersion = pluginMatches.first.group (1 );
328
- logger.printTrace ('$buildFile provides AGP version: $androidPluginVersion ' );
329
- return androidPluginVersion;
349
+ final String settingsFileContent = settingsFile.readAsStringSync ();
350
+ final RegExpMatch ? settingsMatch =
351
+ _settingsAndroidGradlePluginRegExp.firstMatch (settingsFileContent);
352
+
353
+ if (settingsMatch != null ) {
354
+ final String ? androidPluginVersion =
355
+ settingsMatch.namedGroup (_versionGroupName);
356
+ logger.printTrace (
357
+ '$settingsFile provides AGP version: $androidPluginVersion ' );
358
+ return androidPluginVersion;
359
+ }
360
+ logger.printTrace ("$settingsFile doesn't provide an AGP version." );
361
+ return null ;
330
362
}
331
363
332
364
String _formatParseWarning (String content) {
0 commit comments