Skip to content

Commit

Permalink
support AGP 3.2.1 which change the approach to get the AndroidBuilder…
Browse files Browse the repository at this point in the history
… instance
  • Loading branch information
2BAB committed Nov 21, 2018
1 parent 78df43f commit 0f1c181
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand All @@ -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'
61 changes: 61 additions & 0 deletions src/main/kotlin/me/xx2bab/scratchpaper/utils/AGPVersion.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package me.xx2bab.scratchpaper.utils


class AGPVersion(var version: String) : Comparable<AGPVersion> {

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()
}

}
18 changes: 13 additions & 5 deletions src/main/kotlin/me/xx2bab/scratchpaper/utils/AndroidPluginUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -24,4 +31,5 @@ class AndroidPluginUtils(val project: Project) {
return field.get(instance) as Any
}


}

0 comments on commit 0f1c181

Please sign in to comment.