Skip to content

Instantly share code, notes, and snippets.

@tomkoptel
Last active April 22, 2018 13:15
Show Gist options
  • Save tomkoptel/7fecd134eb4b119221ad30a1d50b34cd to your computer and use it in GitHub Desktop.
Save tomkoptel/7fecd134eb4b119221ad30a1d50b34cd to your computer and use it in GitHub Desktop.
A shortcut API to validate Android Gradle plugin version validity.
package com.sample.coolplugin
import org.gradle.api.Plugin
import org.gradle.api.Project
/**
* Verifies if the plugin supports Android Gradle plugin of version 3.0 or 3.1.
*/
internal fun Plugin<Project>.validateAndroidPluginVersion3or31(errorMessage: String) {
var gradlePluginVersion: String? = null
var exception: Exception? = null
try {
gradlePluginVersion = Class.forName("com.android.builder.Version")
.getDeclaredField("ANDROID_GRADLE_PLUGIN_VERSION")
.get(this)
.toString()
} catch (e: Exception) {
exception = e
}
try {
gradlePluginVersion = Class.forName("com.android.builder.model.Version")
.getDeclaredField("ANDROID_GRADLE_PLUGIN_VERSION")
.get(this)
.toString()
} catch (e: Exception) {
exception = e
}
if (gradlePluginVersion == null && exception != null) {
throw IllegalStateException(errorMessage, exception)
} else if (gradlePluginVersion == null) {
throw IllegalStateException(errorMessage)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment