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

Commit 32bcc6f

Browse files
authored
Merge pull request #146 from OneSignal/fix/agp_version_detection
AGP detection with Version.ANDROID_GRADLE_PLUGIN_VERSION
2 parents 9b96c1a + c2ea569 commit 32bcc6f

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

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

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

352352
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) {
353377
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()
356380
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+
}
359392
null
360393
}
361394

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

src/test/groovy/com/onesignal/androidsdk/MainTest.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,11 +1295,11 @@ class MainTest extends Specification {
12951295
when:
12961296
def results = runGradleProject([
12971297
compileLines: """
1298-
compile 'com.android.support:appcompat-v7:26.0.0'
1299-
compile 'com.google.firebase:firebase-messaging:15.0.2'
1300-
compile 'com.google.firebase:firebase-core:16.0.0'
1298+
implementation 'com.google.android.gms:play-services-base:15.0.1'
1299+
implementation 'com.google.android.gms:play-services-basement:17.4.0'
13011300
""",
1302-
skipGradleVersion: GRADLE_OLDEST_VERSION
1301+
skipGradleVersion: GRADLE_OLDEST_VERSION,
1302+
'android.useAndroidX': true,
13031303
])
13041304

13051305
then:

0 commit comments

Comments
 (0)