@@ -4,15 +4,23 @@ import com.android.build.api.variant.AndroidComponentsExtension
44import com.android.build.api.variant.Variant
55import com.android.build.gradle.internal.lint.AndroidLintAnalysisTask
66import com.android.build.gradle.internal.lint.LintModelWriterTask
7+ import org.gradle.api.DefaultTask
78import org.gradle.api.Project
9+ import org.gradle.api.file.DirectoryProperty
810import 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
1118import org.jetbrains.compose.internal.utils.uppercaseFirstChar
1219import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
1320import 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}
0 commit comments