[Gradle] Replace KotlinPlugin.kt#androidPluginVersion with AndroidGradlePluginVersion
^KT-54033 WIP
This commit is contained in:
committed by
Space
parent
7d5b3d3628
commit
958c7e72da
+4
-81
@@ -42,9 +42,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
|||||||
import org.jetbrains.kotlin.gradle.tasks.configuration.*
|
import org.jetbrains.kotlin.gradle.tasks.configuration.*
|
||||||
import org.jetbrains.kotlin.gradle.utils.*
|
import org.jetbrains.kotlin.gradle.utils.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.net.URL
|
|
||||||
import java.util.concurrent.Callable
|
import java.util.concurrent.Callable
|
||||||
import java.util.jar.Manifest
|
|
||||||
|
|
||||||
const val PLUGIN_CLASSPATH_CONFIGURATION_NAME = "kotlinCompilerPluginClasspath"
|
const val PLUGIN_CLASSPATH_CONFIGURATION_NAME = "kotlinCompilerPluginClasspath"
|
||||||
const val NATIVE_COMPILER_PLUGIN_CLASSPATH_CONFIGURATION_NAME = "kotlinNativeCompilerPluginClasspath"
|
const val NATIVE_COMPILER_PLUGIN_CLASSPATH_CONFIGURATION_NAME = "kotlinNativeCompilerPluginClasspath"
|
||||||
@@ -736,12 +734,13 @@ internal open class KotlinAndroidPlugin(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val MINIMAL_SUPPORTED_AGP_VERSION = "3.6.4"
|
private const val MINIMAL_SUPPORTED_AGP_VERSION = "3.6.4"
|
||||||
fun androidTargetHandler(): AndroidProjectHandler {
|
fun androidTargetHandler(): AndroidProjectHandler {
|
||||||
val tasksProvider = KotlinTasksProvider()
|
val tasksProvider = KotlinTasksProvider()
|
||||||
|
val androidGradlePluginVersion = AndroidGradlePluginVersion.current
|
||||||
|
|
||||||
if (androidPluginVersion != null) {
|
if (androidGradlePluginVersion != null) {
|
||||||
if (compareVersionNumbers(androidPluginVersion, MINIMAL_SUPPORTED_AGP_VERSION) < 0) {
|
if (androidGradlePluginVersion < MINIMAL_SUPPORTED_AGP_VERSION) {
|
||||||
throw IllegalStateException(
|
throw IllegalStateException(
|
||||||
"Kotlin: Unsupported version of com.android.tools.build:gradle plugin: " +
|
"Kotlin: Unsupported version of com.android.tools.build:gradle plugin: " +
|
||||||
"version $MINIMAL_SUPPORTED_AGP_VERSION or higher should be used with kotlin-android plugin"
|
"version $MINIMAL_SUPPORTED_AGP_VERSION or higher should be used with kotlin-android plugin"
|
||||||
@@ -805,82 +804,6 @@ private fun SourceSet.clearJavaSrcDirs() {
|
|||||||
java.setSrcDirs(emptyList<File>())
|
java.setSrcDirs(emptyList<File>())
|
||||||
}
|
}
|
||||||
|
|
||||||
//copied from BasePlugin.getLocalVersion
|
|
||||||
internal val androidPluginVersion by lazy {
|
|
||||||
try {
|
|
||||||
val clazz = BasePlugin::class.java
|
|
||||||
val className = clazz.simpleName + ".class"
|
|
||||||
val classPath = clazz.getResource(className).toString()
|
|
||||||
if (!classPath.startsWith("jar")) {
|
|
||||||
// Class not from JAR, unlikely
|
|
||||||
return@lazy null
|
|
||||||
}
|
|
||||||
val manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF"
|
|
||||||
|
|
||||||
val jarConnection = URL(manifestPath).openConnection()
|
|
||||||
jarConnection.useCaches = false
|
|
||||||
val jarInputStream = jarConnection.inputStream
|
|
||||||
val attr = Manifest(jarInputStream).mainAttributes
|
|
||||||
jarInputStream.close()
|
|
||||||
return@lazy attr.getValue("Plugin-Version")
|
|
||||||
} catch (t: Throwable) {
|
|
||||||
return@lazy null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Copied from StringUtil.compareVersionNumbers
|
|
||||||
internal fun compareVersionNumbers(v1: String?, v2: String?): Int {
|
|
||||||
if (v1 == null && v2 == null) {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
if (v1 == null) {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
if (v2 == null) {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
val pattern = "[\\.\\_\\-]".toRegex()
|
|
||||||
val digitsPattern = "\\d+".toRegex()
|
|
||||||
val part1 = v1.split(pattern)
|
|
||||||
val part2 = v2.split(pattern)
|
|
||||||
|
|
||||||
var idx = 0
|
|
||||||
while (idx < part1.size && idx < part2.size) {
|
|
||||||
val p1 = part1[idx]
|
|
||||||
val p2 = part2[idx]
|
|
||||||
|
|
||||||
val cmp: Int
|
|
||||||
if (p1.matches(digitsPattern) && p2.matches(digitsPattern)) {
|
|
||||||
cmp = p1.toInt().compareTo(p2.toInt())
|
|
||||||
} else {
|
|
||||||
cmp = part1[idx].compareTo(part2[idx])
|
|
||||||
}
|
|
||||||
if (cmp != 0) return cmp
|
|
||||||
idx++
|
|
||||||
}
|
|
||||||
|
|
||||||
if (part1.size == part2.size) {
|
|
||||||
return 0
|
|
||||||
} else {
|
|
||||||
val left = part1.size > idx
|
|
||||||
val parts = if (left) part1 else part2
|
|
||||||
|
|
||||||
while (idx < parts.size) {
|
|
||||||
val p = parts[idx]
|
|
||||||
val cmp: Int
|
|
||||||
if (p.matches(digitsPattern)) {
|
|
||||||
cmp = Integer(p).compareTo(0)
|
|
||||||
} else {
|
|
||||||
cmp = 1
|
|
||||||
}
|
|
||||||
if (cmp != 0) return if (left) cmp else -cmp
|
|
||||||
idx++
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun Project.forEachVariant(action: (BaseVariant) -> Unit) {
|
internal fun Project.forEachVariant(action: (BaseVariant) -> Unit) {
|
||||||
val androidExtension = this.extensions.getByName("android")
|
val androidExtension = this.extensions.getByName("android")
|
||||||
when (androidExtension) {
|
when (androidExtension) {
|
||||||
|
|||||||
+1
-13
@@ -46,19 +46,7 @@ class AndroidExtensionsSubpluginIndicator @Inject internal constructor(private v
|
|||||||
|
|
||||||
private fun addAndroidExtensionsRuntime(project: Project) {
|
private fun addAndroidExtensionsRuntime(project: Project) {
|
||||||
val kotlinPluginVersion = project.getKotlinPluginVersion()
|
val kotlinPluginVersion = project.getKotlinPluginVersion()
|
||||||
|
project.configurations.matching { it.name == "implementation" }.all { configuration ->
|
||||||
project.configurations.all { configuration ->
|
|
||||||
val name = configuration.name
|
|
||||||
if (name != "implementation" && name != "compile") return@all
|
|
||||||
|
|
||||||
androidPluginVersion ?: return@all
|
|
||||||
val requiredConfigurationName = when {
|
|
||||||
compareVersionNumbers(androidPluginVersion, "2.5") > 0 -> "implementation"
|
|
||||||
else -> "compile"
|
|
||||||
}
|
|
||||||
|
|
||||||
if (name != requiredConfigurationName) return@all
|
|
||||||
|
|
||||||
configuration.dependencies.add(
|
configuration.dependencies.add(
|
||||||
project.dependencies.create(
|
project.dependencies.create(
|
||||||
"org.jetbrains.kotlin:kotlin-android-extensions-runtime:$kotlinPluginVersion"
|
"org.jetbrains.kotlin:kotlin-android-extensions-runtime:$kotlinPluginVersion"
|
||||||
|
|||||||
Reference in New Issue
Block a user