@@ -350,15 +350,74 @@ class GradleProjectPlugin implements Plugin<Project> {
350
350
}
351
351
352
352
static String getAGPVersion (Plugin plugin ) {
353
+ def pluginVersion = getAGPVersionFromAndroidClass()
354
+ if (pluginVersion)
355
+ return pluginVersion
356
+
357
+ pluginVersion = getAGPVersionFromJarManifest(plugin)
358
+ if (pluginVersion)
359
+ return pluginVersion
360
+
361
+ getAGPVersionByGradleVersion()
362
+ }
363
+
364
+ static String getAGPVersionFromAndroidClass () {
365
+ try {
366
+ return com.android.Version . ANDROID_GRADLE_PLUGIN_VERSION
367
+ } catch (NoClassDefFoundError ignore) {
368
+ // Class may not be loaded, pre-AGP 3.0.0 version, or AGP made a breaking change
369
+ if (project)
370
+ project. logger. info(" getAGPVersionFromAndroidVersionClass: com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION is not defined" )
371
+ }
372
+ null
373
+ }
374
+
375
+ // Only use as a fallback, use getAGPVersionFromAndroidClass() instead if it's available
376
+ static String getAGPVersionFromJarManifest (Plugin plugin ) {
353
377
try {
354
- def cl = plugin. class. classLoader as URLClassLoader
355
- def inputStream = cl . findResource(' META-INF/MANIFEST.MF' ). openStream()
378
+ def classLoader = plugin. class. classLoader as URLClassLoader
379
+ def inputStream = classLoader . findResource(' META-INF/MANIFEST.MF' ). openStream()
356
380
def manifest = new Manifest (inputStream)
357
- return manifest. mainAttributes. getValue(' Plugin-Version' )
358
- } catch (ignore) {}
381
+ // In some cases 'Plugin-Version' will be "unspecified" as a value.
382
+ // - Seen this value with some Cordova projects for an unknown reason.
383
+ def pluginVersionManifestValue = manifest. mainAttributes. getValue(' Plugin-Version' )
384
+ if (isValidVersionNumber(pluginVersionManifestValue))
385
+ return pluginVersionManifestValue
386
+ if (project)
387
+ project. logger. warn(" Error 'Plugin-Version' of '$pluginVersionManifestValue ' for '$plugin ' is not a valid version number" )
388
+ } catch (ignore) {
389
+ if (project)
390
+ project. logger. warn(" Error reading 'META-INF/MANIFEST.MF' for '$plugin '" )
391
+ }
359
392
null
360
393
}
361
394
395
+ // Only use as a fallback, use getAGPVersionFromAndroidClass() instead if it's available
396
+ static String getAGPVersionByGradleVersion () {
397
+ if (! project)
398
+ return null
399
+
400
+ // Making assumption that if they are using an older version of Gradle they are using an older version of AGP
401
+ if (compareVersions(project. gradle. gradleVersion, ' 4.1.0' ) == -1 )
402
+ return ' 2.99.99'
403
+ else
404
+ return null
405
+
406
+ // List of min Gradle versions for each APG version
407
+ // https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
408
+
409
+ // NOTE: Attempted to use the following to detect AGP 2.2.3 but in all attempts below the "Version" class was not found.
410
+ // com.android.build.gradle.internal.Version.ANDROID_GRADLE_PLUGIN_VERSION
411
+ // com.android.builder.model.Version.ANDROID_GRADLE_PLUGIN_VERSION
412
+ // com.android.builder.Version.getAndroidGradlePluginVersion()
413
+ }
414
+
415
+ static boolean isValidVersionNumber (String version ) {
416
+ def versionParser = new VersionParser ()
417
+ def parsedVersion = versionParser. transform(version)
418
+ parsedVersion. numericParts. length > 0 && parsedVersion. numericParts[0 ] != null
419
+ }
420
+
362
421
static void warnOnce (WarningType type , String msg ) {
363
422
if (shownWarnings[type])
364
423
return
0 commit comments