Skip to content
This repository was archived by the owner on Oct 31, 2022. It is now read-only.

Commit c2ea569

Browse files
committed
AGP detection with Version.ANDROID_GRADLE_PLUGIN_VERSION
* com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION is a more reliable way to detect the AGP version. * Still using reading the jar's META-INF/MANIFEST.MF as a fallback * Lastly a new 3rd fallback to go off the Gradle version - APG version from this just a likely assumption
1 parent 30da7a4 commit c2ea569

File tree

1 file changed

+63
-4
lines changed

1 file changed

+63
-4
lines changed

src/main/groovy/com/onesignal/androidsdk/GradleProjectPlugin.groovy

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,15 +342,74 @@ class GradleProjectPlugin implements Plugin<Project> {
342342
}
343343

344344
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) {
345369
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()
348372
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+
}
351384
null
352385
}
353386

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+
354413
static void warnOnce(WarningType type, String msg) {
355414
if (shownWarnings[type])
356415
return

0 commit comments

Comments
 (0)