From 4cbc7a7c47abd9d7b7e8d5b4afcea67ee704b78a Mon Sep 17 00:00:00 2001 From: Konstantin Date: Wed, 8 May 2024 22:24:57 +0200 Subject: [PATCH] [gradle] Apply AGP lint task fix to the KMP resources too. (#4784) We had a fix for the AGP lint tasks applied only for the old Compose resources behavior (non multimodule). The PR applies the logic for both cases. AGP issue: https://issuetracker.google.com/issues/333831734 Fixes https://github.com/JetBrains/compose-multiplatform/issues/4739 ## Release Notes ### Fixes - Resources - _(prerelease fix)_ Fix AGP lint tasks dependency issues --- .../compose/resources/AndroidResources.kt | 20 ++++++++++ .../compose/resources/ComposeResources.kt | 37 +++++++------------ 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/AndroidResources.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/AndroidResources.kt index c4c7d79e69..f1d9f2855b 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/AndroidResources.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/AndroidResources.kt @@ -2,6 +2,8 @@ package org.jetbrains.compose.resources import com.android.build.api.variant.AndroidComponentsExtension import com.android.build.gradle.BaseExtension +import com.android.build.gradle.internal.lint.AndroidLintAnalysisTask +import com.android.build.gradle.internal.lint.LintModelWriterTask import org.gradle.api.DefaultTask import org.gradle.api.Project import org.gradle.api.file.DirectoryProperty @@ -102,4 +104,22 @@ internal abstract class CopyAndroidFontsToAssetsTask : DefaultTask() { it.into(outputDirectory) } } +} + +/* + There is a dirty fix for the problem: + + Reason: Task ':generateDemoDebugUnitTestLintModel' uses this output of task ':generateResourceAccessorsForAndroidUnitTest' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. + + Possible solutions: + 1. Declare task ':generateResourceAccessorsForAndroidUnitTest' as an input of ':generateDemoDebugUnitTestLintModel'. + 2. Declare an explicit dependency on ':generateResourceAccessorsForAndroidUnitTest' from ':generateDemoDebugUnitTestLintModel' using Task#dependsOn. + 3. Declare an explicit dependency on ':generateResourceAccessorsForAndroidUnitTest' from ':generateDemoDebugUnitTestLintModel' using Task#mustRunAfter. + */ +internal fun Project.fixAndroidLintTaskDependencies() { + tasks.matching { + it is AndroidLintAnalysisTask || it is LintModelWriterTask + }.configureEach { + it.mustRunAfter(tasks.withType(GenerateResourceAccessorsTask::class.java)) + } } \ No newline at end of file diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ComposeResources.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ComposeResources.kt index 3c973879a5..4b67866036 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ComposeResources.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ComposeResources.kt @@ -20,7 +20,6 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet import org.jetbrains.kotlin.gradle.plugin.extraProperties import java.io.File - internal const val COMPOSE_RESOURCES_DIR = "composeResources" internal const val RES_GEN_DIR = "generated/compose/resourceGenerator" private const val KMP_RES_EXT = "multiplatformResourcesPublication" @@ -47,6 +46,7 @@ private fun Project.onKgpApplied(config: Provider, kgp: Kotl if (kmpResourcesAreAvailable) { configureKmpResources(kotlinExtension, extraProperties.get(KMP_RES_EXT)!!, config) + onAgpApplied { fixAndroidLintTaskDependencies() } } else { if (!disableMultimoduleResources) { if (!hasKmpResources) logger.info( @@ -66,35 +66,24 @@ private fun Project.onKgpApplied(config: Provider, kgp: Kotl val commonMain = KotlinSourceSet.COMMON_MAIN_SOURCE_SET_NAME configureComposeResources(kotlinExtension, commonMain, config) - //when applied AGP then configure android resources - androidPluginIds.forEach { pluginId -> - plugins.withId(pluginId) { - val androidExtension = project.extensions.getByType(BaseExtension::class.java) - configureAndroidComposeResources(kotlinExtension, androidExtension) - - - /* - There is a dirty fix for the problem: - - Reason: Task ':generateDemoDebugUnitTestLintModel' uses this output of task ':generateResourceAccessorsForAndroidUnitTest' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. - - Possible solutions: - 1. Declare task ':generateResourceAccessorsForAndroidUnitTest' as an input of ':generateDemoDebugUnitTestLintModel'. - 2. Declare an explicit dependency on ':generateResourceAccessorsForAndroidUnitTest' from ':generateDemoDebugUnitTestLintModel' using Task#dependsOn. - 3. Declare an explicit dependency on ':generateResourceAccessorsForAndroidUnitTest' from ':generateDemoDebugUnitTestLintModel' using Task#mustRunAfter. - */ - tasks.matching { - it is AndroidLintAnalysisTask || it is LintModelWriterTask - }.configureEach { - it.mustRunAfter(tasks.withType(GenerateResourceAccessorsTask::class.java)) - } - } + onAgpApplied { androidExtension -> + configureAndroidComposeResources(kotlinExtension, androidExtension) + fixAndroidLintTaskDependencies() } } configureSyncIosComposeResources(kotlinExtension) } +private fun Project.onAgpApplied(block: (androidExtension: BaseExtension) -> Unit) { + androidPluginIds.forEach { pluginId -> + plugins.withId(pluginId) { + val androidExtension = project.extensions.getByType(BaseExtension::class.java) + block(androidExtension) + } + } +} + private fun Project.onKotlinJvmApplied(config: Provider) { val kotlinExtension = project.extensions.getByType(KotlinProjectExtension::class.java) val main = SourceSet.MAIN_SOURCE_SET_NAME