Skip to content

Commit

Permalink
[gradle] Fix assets packing in the AS previews.
Browse files Browse the repository at this point in the history
  • Loading branch information
terrakok committed Jun 19, 2024
1 parent ca25916 commit 6140e1b
Showing 1 changed file with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import org.gradle.api.tasks.IgnoreEmptyDirectories
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.jetbrains.compose.internal.ideaIsInSyncProvider
import org.jetbrains.compose.internal.utils.registerTask
import org.jetbrains.compose.internal.utils.uppercaseFirstChar
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
Expand All @@ -38,12 +37,6 @@ internal fun Project.configureAndroidComposeResources(
(compilation.allKotlinSourceSets as? ObservableSet<KotlinSourceSet>)?.forAll { kotlinSourceSet ->
val preparedComposeResources = getPreparedComposeResourcesDir(kotlinSourceSet)
variantResources.from(preparedComposeResources)

// variant.sources?.assets?.addGeneratedSourceDirectory DOESN'T WORK for the AS Compose preview
// But we know that resources will be prepared during IDE sync, and we can use addStaticSourceDirectory here
if (ideaIsInSyncProvider().get()) {
variant.sources?.assets?.addStaticSourceDirectory(preparedComposeResources.get().path)
}
}
}
}
Expand All @@ -54,10 +47,22 @@ internal fun Project.configureAndroidComposeResources(
) {
from.set(variantResources)
}
variant.sources?.assets?.addGeneratedSourceDirectory(
taskProvider = copyResources,
wiredWith = CopyResourcesToAndroidAssetsTask::outputDirectory
)
variant.sources.assets?.apply {
addGeneratedSourceDirectory(
taskProvider = copyResources,
wiredWith = CopyResourcesToAndroidAssetsTask::outputDirectory
)

// addGeneratedSourceDirectory doesn't mark the output directory as assets hence AS Compose Preview doesn't work
addStaticSourceDirectory(copyResources.flatMap { it.outputDirectory.asFile }.get().path)

// addGeneratedSourceDirectory doesn't run the copyResources task during AS Compose Preview build
tasks.matching {
it.name == "compile${variant.name.uppercaseFirstChar()}Sources"
}.configureEach { task ->
task.dependsOn(copyResources)
}
}
}
}

Expand Down

0 comments on commit 6140e1b

Please sign in to comment.