Rework AndroidSubplugin for the new subplugins API
This commit is contained in:
+40
-45
@@ -13,15 +13,14 @@ import com.android.build.gradle.api.TestVariant
|
|||||||
import com.android.build.gradle.internal.variant.TestVariantData
|
import com.android.build.gradle.internal.variant.TestVariantData
|
||||||
import org.gradle.api.Plugin
|
import org.gradle.api.Plugin
|
||||||
import org.gradle.api.Project
|
import org.gradle.api.Project
|
||||||
import org.gradle.api.UnknownDomainObjectException
|
|
||||||
import org.gradle.api.file.FileCollection
|
import org.gradle.api.file.FileCollection
|
||||||
|
import org.gradle.api.provider.Provider
|
||||||
import org.gradle.api.tasks.PathSensitivity
|
import org.gradle.api.tasks.PathSensitivity
|
||||||
import org.gradle.api.tasks.compile.AbstractCompile
|
import org.gradle.api.tasks.TaskProvider
|
||||||
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
|
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
|
||||||
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
|
|
||||||
import org.jetbrains.kotlin.gradle.model.builder.KotlinAndroidExtensionModelBuilder
|
import org.jetbrains.kotlin.gradle.model.builder.KotlinAndroidExtensionModelBuilder
|
||||||
import org.jetbrains.kotlin.gradle.plugin.*
|
import org.jetbrains.kotlin.gradle.plugin.*
|
||||||
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget
|
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation
|
||||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
import org.w3c.dom.Document
|
import org.w3c.dom.Document
|
||||||
import java.io.File
|
import java.io.File
|
||||||
@@ -36,6 +35,7 @@ class AndroidExtensionsSubpluginIndicator @Inject internal constructor(private v
|
|||||||
project.extensions.create("androidExtensions", AndroidExtensionsExtension::class.java)
|
project.extensions.create("androidExtensions", AndroidExtensionsExtension::class.java)
|
||||||
addAndroidExtensionsRuntime(project)
|
addAndroidExtensionsRuntime(project)
|
||||||
registry.register(KotlinAndroidExtensionModelBuilder())
|
registry.register(KotlinAndroidExtensionModelBuilder())
|
||||||
|
project.plugins.apply(AndroidSubplugin::class.java)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun addAndroidExtensionsRuntime(project: Project) {
|
private fun addAndroidExtensionsRuntime(project: Project) {
|
||||||
@@ -65,41 +65,35 @@ class AndroidExtensionsSubpluginIndicator @Inject internal constructor(private v
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AndroidSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
class AndroidSubplugin : KotlinCompilerPluginSupportPlugin {
|
||||||
override fun isApplicable(project: Project, task: AbstractCompile): Boolean {
|
override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean {
|
||||||
if (task !is KotlinCompile) return false
|
if (kotlinCompilation !is KotlinJvmAndroidCompilation)
|
||||||
try {
|
|
||||||
project.extensions.getByName("android") as? BaseExtension ?: return false
|
|
||||||
} catch (e: UnknownDomainObjectException) {
|
|
||||||
return false
|
return false
|
||||||
}
|
|
||||||
if (project.plugins.findPlugin(AndroidExtensionsSubpluginIndicator::class.java) == null) {
|
val project = kotlinCompilation.target.project
|
||||||
|
|
||||||
|
if (project.extensions.findByName("android") !is BaseExtension)
|
||||||
|
return false
|
||||||
|
|
||||||
|
if (project.plugins.findPlugin(AndroidExtensionsSubpluginIndicator::class.java) == null)
|
||||||
return false
|
return false
|
||||||
}
|
|
||||||
project.multiplatformExtensionOrNull?.let { kotlin ->
|
|
||||||
return kotlin.targets.any { target ->
|
|
||||||
target is KotlinAndroidTarget && target.compilations.any { it.compileKotlinTaskName == task.name }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun apply(
|
override fun applyToCompilation(
|
||||||
project: Project,
|
kotlinCompilation: KotlinCompilation<*>
|
||||||
kotlinCompile: KotlinCompile,
|
): Provider<List<SubpluginOption>> {
|
||||||
javaCompile: AbstractCompile?,
|
kotlinCompilation as KotlinJvmAndroidCompilation
|
||||||
variantData: Any?,
|
val project = kotlinCompilation.target.project
|
||||||
androidProjectHandler: Any?,
|
|
||||||
kotlinCompilation: KotlinCompilation<*>?
|
val androidExtension = project.extensions.getByName("android") as BaseExtension
|
||||||
): List<SubpluginOption> {
|
|
||||||
val androidExtension = project.extensions.getByName("android") as? BaseExtension ?: return emptyList()
|
|
||||||
val androidExtensionsExtension = project.extensions.getByType(AndroidExtensionsExtension::class.java)
|
val androidExtensionsExtension = project.extensions.getByType(AndroidExtensionsExtension::class.java)
|
||||||
|
|
||||||
if (androidExtensionsExtension.isExperimental) {
|
if (androidExtensionsExtension.isExperimental) {
|
||||||
return applyExperimental(
|
return applyExperimental(
|
||||||
kotlinCompile, androidExtension, androidExtensionsExtension,
|
kotlinCompilation.compileKotlinTaskProvider, androidExtension, androidExtensionsExtension,
|
||||||
project, variantData, androidProjectHandler
|
project, kotlinCompilation.androidVariant
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +124,9 @@ class AndroidSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
FilesSubpluginOption("resDirs", project.files(Callable { sourceSet.res.srcDirs }))
|
FilesSubpluginOption("resDirs", project.files(Callable { sourceSet.res.srcDirs }))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
kotlinCompile.inputs.files(getLayoutDirectories(project, sourceSet.res.srcDirs)).withPathSensitivity(PathSensitivity.RELATIVE)
|
kotlinCompilation.compileKotlinTaskProvider.configure {
|
||||||
|
it.inputs.files(getLayoutDirectories(project, sourceSet.res.srcDirs)).withPathSensitivity(PathSensitivity.RELATIVE)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addVariant(mainSourceSet)
|
addVariant(mainSourceSet)
|
||||||
@@ -142,7 +138,7 @@ class AndroidSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
addVariant(sourceSet)
|
addVariant(sourceSet)
|
||||||
}
|
}
|
||||||
|
|
||||||
return wrapPluginOptions(pluginOptions, "configuration")
|
return project.provider { wrapPluginOptions(pluginOptions, "configuration") }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getLayoutDirectories(project: Project, resDirectories: Iterable<File>): FileCollection {
|
private fun getLayoutDirectories(project: Project, resDirectories: Iterable<File>): FileCollection {
|
||||||
@@ -156,19 +152,17 @@ class AndroidSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun applyExperimental(
|
private fun applyExperimental(
|
||||||
kotlinCompile: KotlinCompile,
|
kotlinCompile: TaskProvider<out KotlinCompile>,
|
||||||
androidExtension: BaseExtension,
|
androidExtension: BaseExtension,
|
||||||
androidExtensionsExtension: AndroidExtensionsExtension,
|
androidExtensionsExtension: AndroidExtensionsExtension,
|
||||||
project: Project,
|
project: Project,
|
||||||
variantData: Any?,
|
variantData: Any?
|
||||||
androidProjectHandler: Any?
|
): Provider<List<SubpluginOption>> {
|
||||||
): List<SubpluginOption> {
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
androidProjectHandler as? AbstractAndroidProjectHandler ?: return emptyList()
|
|
||||||
|
|
||||||
val pluginOptions = arrayListOf<SubpluginOption>()
|
val pluginOptions = arrayListOf<SubpluginOption>()
|
||||||
pluginOptions += SubpluginOption("features",
|
pluginOptions += SubpluginOption(
|
||||||
AndroidExtensionsFeature.parseFeatures(androidExtensionsExtension.features).joinToString(",") { it.featureName })
|
"features",
|
||||||
|
AndroidExtensionsFeature.parseFeatures(androidExtensionsExtension.features).joinToString(",") { it.featureName }
|
||||||
|
)
|
||||||
|
|
||||||
pluginOptions += SubpluginOption("experimental", "true")
|
pluginOptions += SubpluginOption("experimental", "true")
|
||||||
pluginOptions += SubpluginOption(
|
pluginOptions += SubpluginOption(
|
||||||
@@ -195,7 +189,9 @@ class AndroidSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
kotlinCompile.inputs.files(getLayoutDirectories(project, resDirectories)).withPathSensitivity(PathSensitivity.RELATIVE)
|
kotlinCompile.configure {
|
||||||
|
it.inputs.files(getLayoutDirectories(project, resDirectories)).withPathSensitivity(PathSensitivity.RELATIVE)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addSourceSetAsVariant(name: String) {
|
fun addSourceSetAsVariant(name: String) {
|
||||||
@@ -208,9 +204,9 @@ class AndroidSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
|
|
||||||
val resDirectoriesForAllVariants = mutableListOf<FileCollection>()
|
val resDirectoriesForAllVariants = mutableListOf<FileCollection>()
|
||||||
|
|
||||||
project.forEachVariant { variant ->
|
forEachVariant(project) { variant ->
|
||||||
if (getTestedVariantData(variant) != null) return@forEachVariant
|
if (getTestedVariantData(variant) != null) return@forEachVariant
|
||||||
resDirectoriesForAllVariants += androidProjectHandler.getResDirectories(variant)
|
resDirectoriesForAllVariants += variant.getResDirectories()
|
||||||
}
|
}
|
||||||
|
|
||||||
val commonResDirectories = getCommonResDirectories(project, resDirectoriesForAllVariants)
|
val commonResDirectories = getCommonResDirectories(project, resDirectoriesForAllVariants)
|
||||||
@@ -229,11 +225,10 @@ class AndroidSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return wrapPluginOptions(pluginOptions, "configuration")
|
return project.provider { wrapPluginOptions(pluginOptions, "configuration") }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getVariantComponentNames(flavorData: Any?): VariantComponentNames? = when (flavorData) {
|
private fun getVariantComponentNames(flavorData: Any?): VariantComponentNames? = when (flavorData) {
|
||||||
is KaptVariantData<*> -> getVariantComponentNames(flavorData.variantData)
|
|
||||||
is TestVariantData -> getVariantComponentNames(flavorData.testedVariantData)
|
is TestVariantData -> getVariantComponentNames(flavorData.testedVariantData)
|
||||||
is TestVariant -> getVariantComponentNames(flavorData.testedVariant)
|
is TestVariant -> getVariantComponentNames(flavorData.testedVariant)
|
||||||
is BaseVariant -> VariantComponentNames(flavorData.name, flavorData.flavorName, flavorData.buildType.name)
|
is BaseVariant -> VariantComponentNames(flavorData.name, flavorData.flavorName, flavorData.buildType.name)
|
||||||
|
|||||||
-1
@@ -1,2 +1 @@
|
|||||||
org.jetbrains.kotlin.gradle.internal.AndroidSubplugin
|
|
||||||
org.jetbrains.kotlin.gradle.scripting.internal.ScriptingKotlinGradleSubplugin
|
org.jetbrains.kotlin.gradle.scripting.internal.ScriptingKotlinGradleSubplugin
|
||||||
Reference in New Issue
Block a user