Skip to content

Commit 48b64fd

Browse files
authored
[gradle] Restore correct android assets registration after AGP fix. (#5118)
After the fix there will be a correct configuration cache usage. Fixes https://youtrack.jetbrains.com/issue/CMP-5674 ## Release Notes ### Fixes - Gradle Plugin - _(prerelease fix)_ Fix broken configuration cache due Android Studio + AGP issues. Now Android Studio previews require latest AGP versions (8.5.2, 8.6.0-rc01, 8.7.0-alpha04): https://issuetracker.google.com/issues/348208777
1 parent 481bb53 commit 48b64fd

File tree

2 files changed

+58
-44
lines changed

2 files changed

+58
-44
lines changed

gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/AndroidResources.kt

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,23 @@ import com.android.build.api.variant.AndroidComponentsExtension
44
import com.android.build.api.variant.Variant
55
import com.android.build.gradle.internal.lint.AndroidLintAnalysisTask
66
import com.android.build.gradle.internal.lint.LintModelWriterTask
7+
import org.gradle.api.DefaultTask
78
import org.gradle.api.Project
9+
import org.gradle.api.file.DirectoryProperty
810
import org.gradle.api.file.FileCollection
9-
import org.gradle.api.tasks.Copy
10-
import org.jetbrains.compose.internal.utils.dir
11+
import org.gradle.api.file.FileSystemOperations
12+
import org.gradle.api.provider.Property
13+
import org.gradle.api.tasks.IgnoreEmptyDirectories
14+
import org.gradle.api.tasks.InputFiles
15+
import org.gradle.api.tasks.OutputDirectory
16+
import org.gradle.api.tasks.TaskAction
17+
import org.jetbrains.compose.internal.utils.registerTask
1118
import org.jetbrains.compose.internal.utils.uppercaseFirstChar
1219
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
1320
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget
21+
import javax.inject.Inject
1422

15-
internal fun Project.getAndroidVariantComposeResources(
23+
private fun Project.getAndroidVariantComposeResources(
1624
kotlinExtension: KotlinMultiplatformExtension,
1725
variant: Variant
1826
): FileCollection = project.files({
@@ -27,46 +35,27 @@ internal fun Project.getAndroidVariantComposeResources(
2735
}
2836
})
2937

30-
internal fun Project.getKgpAndroidVariantComposeResources(
31-
variant: Variant
32-
): FileCollection = project.files({
33-
val taskName = "${variant.name}AssetsCopyForAGP"
34-
if (tasks.names.contains(taskName)) tasks.named(taskName).map { it.outputs.files }
35-
else files()
36-
})
37-
38-
internal fun Project.configureAndroidComposeResources(
39-
getVariantComposeResources: (Variant) -> FileCollection
40-
) {
38+
internal fun Project.configureAndroidComposeResources() {
4139
//copy all compose resources to android assets
4240
val androidComponents = project.extensions.findByType(AndroidComponentsExtension::class.java) ?: return
41+
val kotlinExtension = project.extensions.findByType(KotlinMultiplatformExtension::class.java) ?: return
4342
androidComponents.onVariants { variant ->
4443
val camelVariantName = variant.name.uppercaseFirstChar()
45-
val variantAssets = getVariantComposeResources(variant)
46-
val variantAssetsDir = layout.buildDirectory.dir(RES_GEN_DIR).dir(variant.name + "AndroidAssets")
44+
val variantAssets = getAndroidVariantComposeResources(kotlinExtension, variant)
4745

48-
val copyVariantAssets = tasks.register(
49-
"copy${camelVariantName}ComposeResourcesToAndroidAssets",
50-
Copy::class.java
51-
) { task ->
52-
task.from(variantAssets)
53-
task.into(variantAssetsDir)
46+
val copyVariantAssets = registerTask<CopyResourcesToAndroidAssetsTask>(
47+
"copy${camelVariantName}ComposeResourcesToAndroidAssets"
48+
) {
49+
from.set(variantAssets)
5450
}
5551

56-
//https://issuetracker.google.com/348208777
57-
val staticDir = variantAssetsDir.get().asFile
58-
staticDir.mkdirs()
59-
variant.sources.assets?.addStaticSourceDirectory(staticDir.path)
60-
61-
val agpTaskNames = listOf(
62-
//fix agp task dependencies for build and allTests tasks
63-
"merge${camelVariantName}Assets",
64-
"package${camelVariantName}Assets",
65-
//fix agp task dependencies for AndroidStudio preview
66-
"compile${camelVariantName}Sources",
52+
variant.sources.assets?.addGeneratedSourceDirectory(
53+
copyVariantAssets,
54+
CopyResourcesToAndroidAssetsTask::outputDirectory
6755
)
6856
tasks.configureEach { task ->
69-
if (task.name in agpTaskNames) {
57+
//fix agp task dependencies for AndroidStudio preview
58+
if (task.name == "compile${camelVariantName}Sources") {
7059
task.dependsOn(copyVariantAssets)
7160
}
7261
//fix linter task dependencies for `build` task
@@ -77,6 +66,28 @@ internal fun Project.configureAndroidComposeResources(
7766
}
7867
}
7968

69+
//Copy task doesn't work with 'variant.sources?.assets?.addGeneratedSourceDirectory' API
70+
internal abstract class CopyResourcesToAndroidAssetsTask : DefaultTask() {
71+
@get:Inject
72+
abstract val fileSystem: FileSystemOperations
73+
74+
@get:InputFiles
75+
@get:IgnoreEmptyDirectories
76+
abstract val from: Property<FileCollection>
77+
78+
@get:OutputDirectory
79+
abstract val outputDirectory: DirectoryProperty
80+
81+
@TaskAction
82+
fun action() {
83+
fileSystem.copy {
84+
it.includeEmptyDirs = false
85+
it.from(from)
86+
it.into(outputDirectory)
87+
}
88+
}
89+
}
90+
8091
/*
8192
There is a dirty fix for the problem:
8293
@@ -93,4 +104,15 @@ internal fun Project.fixAndroidLintTaskDependencies() {
93104
}.configureEach {
94105
it.mustRunAfter(tasks.withType(GenerateResourceAccessorsTask::class.java))
95106
}
107+
}
108+
109+
internal fun Project.fixKgpAndroidPreviewTaskDependencies() {
110+
val androidComponents = project.extensions.findByType(AndroidComponentsExtension::class.java) ?: return
111+
androidComponents.onVariants { variant ->
112+
tasks.configureEach { task ->
113+
if (task.name == "compile${variant.name.uppercaseFirstChar()}Sources") {
114+
task.dependsOn("${variant.name}AssetsCopyForAGP")
115+
}
116+
}
117+
}
96118
}

gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ComposeResources.kt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,7 @@ private fun Project.onKgpApplied(config: Provider<ResourcesExtension>, kgp: Kotl
4040
if (kmpResourcesAreAvailable) {
4141
configureKmpResources(kotlinExtension, extraProperties.get(KMP_RES_EXT)!!, config)
4242
onAgpApplied {
43-
//workaround to fix AndroidStudio preview works with compose resources:
44-
//it copies android assets and mark it as a static assets dir
45-
//yes, the same resources will be registered in AGP twice: from KGP and here as static dirs
46-
//but it works fine and there are no problems during merge android assets
47-
configureAndroidComposeResources { variant ->
48-
getKgpAndroidVariantComposeResources(variant)
49-
}
43+
fixKgpAndroidPreviewTaskDependencies()
5044
fixAndroidLintTaskDependencies()
5145
}
5246
} else {
@@ -69,9 +63,7 @@ private fun Project.onKgpApplied(config: Provider<ResourcesExtension>, kgp: Kotl
6963
configureComposeResources(kotlinExtension, commonMain, config)
7064

7165
onAgpApplied {
72-
configureAndroidComposeResources { variant ->
73-
getAndroidVariantComposeResources(kotlinExtension, variant)
74-
}
66+
configureAndroidComposeResources()
7567
fixAndroidLintTaskDependencies()
7668
}
7769
}

0 commit comments

Comments
 (0)