Skip to content

Commit

Permalink
[gradle] Apply AGP lint task fix to the KMP resources too. (#4784)
Browse files Browse the repository at this point in the history
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 #4739

## Release Notes
### Fixes - Resources
- _(prerelease fix)_ Fix AGP lint tasks dependency issues
  • Loading branch information
terrakok committed May 8, 2024
1 parent b884678 commit 4cbc7a7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}
Expand Up @@ -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"
Expand All @@ -47,6 +46,7 @@ private fun Project.onKgpApplied(config: Provider<ResourcesExtension>, kgp: Kotl

if (kmpResourcesAreAvailable) {
configureKmpResources(kotlinExtension, extraProperties.get(KMP_RES_EXT)!!, config)
onAgpApplied { fixAndroidLintTaskDependencies() }
} else {
if (!disableMultimoduleResources) {
if (!hasKmpResources) logger.info(
Expand All @@ -66,35 +66,24 @@ private fun Project.onKgpApplied(config: Provider<ResourcesExtension>, 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<ResourcesExtension>) {
val kotlinExtension = project.extensions.getByType(KotlinProjectExtension::class.java)
val main = SourceSet.MAIN_SOURCE_SET_NAME
Expand Down

0 comments on commit 4cbc7a7

Please sign in to comment.