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