From 55cc437465fd6060f56482e76bb401d59572a75a Mon Sep 17 00:00:00 2001 From: Konstantin Date: Fri, 16 Feb 2024 18:48:44 +0100 Subject: [PATCH] Fix import android flavors with compose resources (#4319) fixes https://github.com/JetBrains/compose-multiplatform/issues/4318 --- .../compose/resources/ResourcesGenerator.kt | 37 +++++-------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt index 1d1c8081fa..f31fafc867 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesGenerator.kt @@ -21,6 +21,7 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget +import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation import org.jetbrains.kotlin.gradle.plugin.sources.android.androidSourceSetInfoOrNull import org.jetbrains.kotlin.gradle.utils.ObservableSet import java.io.File @@ -75,22 +76,25 @@ private fun Project.configureAndroidComposeResources( val commonResourcesDir = projectDir.resolve("src/${KotlinSourceSet.COMMON_MAIN_SOURCE_SET_NAME}/$COMPOSE_RESOURCES_DIR") //Copy common compose resources except fonts to android resources - val copyCommonAndroidComposeResources = registerTask( + val commonAndroidComposeResourcesDir = layout.buildDirectory.dir("$RES_GEN_DIR/commonAndroidComposeResources") + val copyCommonAndroidComposeResources = registerTask( "copyCommonAndroidComposeResources" ) { - from.set(commonResourcesDir) - outputDirectory.set(layout.buildDirectory.dir("$RES_GEN_DIR/commonAndroidComposeResources")) + includeEmptyDirs = false + from(commonResourcesDir) + exclude("**/font*/*") + into(commonAndroidComposeResourcesDir) } - tasks.configureEachWithType { dependsOn(copyCommonAndroidComposeResources) } //mark all composeResources as Android resources kotlinExtension.targets.withType(KotlinAndroidTarget::class.java).all { androidTarget -> - androidTarget.compilations.all { compilation: KotlinCompilation<*> -> + androidTarget.compilations.all { compilation: KotlinJvmAndroidCompilation -> compilation.defaultSourceSet.androidSourceSetInfoOrNull?.let { kotlinAndroidSourceSet -> androidExtension.sourceSets .matching { it.name == kotlinAndroidSourceSet.androidSourceSetName } .all { androidSourceSet -> - androidSourceSet.resources.srcDir(copyCommonAndroidComposeResources.flatMap { it.outputDirectory.asFile }) + compilation.androidVariant.processJavaResourcesProvider.dependsOn(copyCommonAndroidComposeResources) + androidSourceSet.resources.srcDir(commonAndroidComposeResourcesDir) (compilation.allKotlinSourceSets as? ObservableSet)?.forAll { kotlinSourceSet -> if (kotlinSourceSet.name != KotlinSourceSet.COMMON_MAIN_SOURCE_SET_NAME) { androidSourceSet.resources.srcDir( @@ -166,27 +170,6 @@ private fun Project.configureResourceGenerator(commonComposeResourcesDir: File, } } -internal abstract class CopyCommonAndroidComposeResources : DefaultTask() { - @get:Inject - abstract val fileSystem: FileSystemOperations - - @get:InputFiles - abstract val from: Property - - @get:OutputDirectory - abstract val outputDirectory: DirectoryProperty - - @TaskAction - fun action() { - fileSystem.copy { - it.includeEmptyDirs = false - it.from(from) - it.exclude("**/font*/*") - it.into(outputDirectory) - } - } -} - //Copy task doesn't work with 'variant.sources?.assets?.addGeneratedSourceDirectory' API internal abstract class CopyAndroidFontsToAssetsTask : DefaultTask() { @get:Inject