Last active
April 22, 2018 13:15
-
-
Save tomkoptel/7fecd134eb4b119221ad30a1d50b34cd to your computer and use it in GitHub Desktop.
A shortcut API to validate Android Gradle plugin version validity.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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