From 0f1c1818cc2da40399c779f47c7d7e213f8eca2e Mon Sep 17 00:00:00 2001 From: 2BAB Date: Wed, 21 Nov 2018 22:55:54 +0800 Subject: [PATCH] support AGP 3.2.1 which change the approach to get the AndroidBuilder instance --- build.gradle | 4 +- .../xx2bab/scratchpaper/utils/AGPVersion.kt | 61 +++++++++++++++++++ .../scratchpaper/utils/AndroidPluginUtils.kt | 18 ++++-- 3 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 src/main/kotlin/me/xx2bab/scratchpaper/utils/AGPVersion.kt diff --git a/build.gradle b/build.gradle index 30b8085..1729814 100644 --- a/build.gradle +++ b/build.gradle @@ -38,7 +38,7 @@ dependencies { implementation gradleApi() implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" - implementation 'com.android.tools.build:gradle:3.2.0' + implementation 'com.android.tools.build:gradle:3.2.1' implementation 'org.jfree:jfreesvg:3.3' } @@ -57,6 +57,6 @@ if (project.extensions.findByName("buildScan") != null) { // publish group 'me.2bab' -version '2.3.1' +version '2.3.2' apply from: 'bintray.gradle' apply from: 'mavenlocal.gradle' \ No newline at end of file diff --git a/src/main/kotlin/me/xx2bab/scratchpaper/utils/AGPVersion.kt b/src/main/kotlin/me/xx2bab/scratchpaper/utils/AGPVersion.kt new file mode 100644 index 0000000..00a0cd9 --- /dev/null +++ b/src/main/kotlin/me/xx2bab/scratchpaper/utils/AGPVersion.kt @@ -0,0 +1,61 @@ +package me.xx2bab.scratchpaper.utils + + +class AGPVersion(var version: String) : Comparable { + + init { + // Any alpha/beta/rc version we deem it as the formal one + if (version.contains("-")) { + val indexOfDash = version.indexOf("-") + version = version.substring(0, indexOfDash) + } + // should only include + if (!version.matches("[0-9]+(\\.[0-9]+)*".toRegex())) { + throw IllegalArgumentException("Invalid version format") + } + } + + override fun compareTo(other: AGPVersion): Int { + val thisParts = this.get().split("\\.".toRegex()) + val thatParts = other.get().split("\\.".toRegex()) + val length = Math.max(thisParts.size, thatParts.size) + for (i in 0 until length) { + val thisPart = if (i < thisParts.size) + Integer.parseInt(thisParts[i]) + else + 0 + val thatPart = if (i < thatParts.size) + Integer.parseInt(thatParts[i]) + else + 0 + if (thisPart < thatPart) { + return -1 + } + if (thisPart > thatPart) { + return 1 + } + } + return 0 + } + + fun get(): String { + return version + } + + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as AGPVersion + + if (this != other) return false + + return true + } + + override fun hashCode(): Int { + return version.hashCode() + } + +} \ No newline at end of file diff --git a/src/main/kotlin/me/xx2bab/scratchpaper/utils/AndroidPluginUtils.kt b/src/main/kotlin/me/xx2bab/scratchpaper/utils/AndroidPluginUtils.kt index 7115cf1..fba200f 100644 --- a/src/main/kotlin/me/xx2bab/scratchpaper/utils/AndroidPluginUtils.kt +++ b/src/main/kotlin/me/xx2bab/scratchpaper/utils/AndroidPluginUtils.kt @@ -2,19 +2,26 @@ package me.xx2bab.scratchpaper.utils import com.android.build.gradle.AppPlugin import com.android.build.gradle.BasePlugin +import com.android.build.gradle.internal.scope.GlobalScope import com.android.builder.core.AndroidBuilder +import com.android.builder.model.Version import org.gradle.api.Project class AndroidPluginUtils(val project: Project) { @Throws(Exception::class) - fun getAndroidBuilder(): AndroidBuilder? { - val basePlugin = project.plugins.findPlugin(AppPlugin::class.java) - return if (null == basePlugin) { - null + fun getAndroidBuilder(): AndroidBuilder { + val basePlugin = project.plugins.findPlugin(AppPlugin::class.java) as BasePlugin<*> + val currentAGPVersion = AGPVersion(Version.ANDROID_GRADLE_PLUGIN_VERSION) + val androidBuilderApiMinAGPVersion = AGPVersion("3.2.1") + return if (currentAGPVersion < androidBuilderApiMinAGPVersion) { + getField(BasePlugin::class.java, basePlugin, + "androidBuilder") as AndroidBuilder } else { - getField(BasePlugin::class.java, basePlugin, "androidBuilder") as AndroidBuilder + val scope = getField(BasePlugin::class.java, basePlugin, + "globalScope") as GlobalScope + scope.androidBuilder } } @@ -24,4 +31,5 @@ class AndroidPluginUtils(val project: Project) { return field.get(instance) as Any } + } \ No newline at end of file