Browse Source

[gradle] Fix android assets configuration with AGP >= 8.1.0.

pull/5006/head
Konstantin Tskhovrebov 5 months ago
parent
commit
3a922a8502
  1. 31
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/AndroidResources.kt

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

@ -1,18 +1,14 @@
package org.jetbrains.compose.resources package org.jetbrains.compose.resources
import com.android.build.api.AndroidPluginVersion
import com.android.build.api.variant.AndroidComponentsExtension import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.gradle.internal.lint.AndroidLintAnalysisTask import com.android.build.gradle.internal.lint.AndroidLintAnalysisTask
import com.android.build.gradle.internal.lint.LintModelWriterTask import com.android.build.gradle.internal.lint.LintModelWriterTask
import org.gradle.api.DefaultTask import org.gradle.api.DefaultTask
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.file.DirectoryProperty import org.gradle.api.file.*
import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileSystemOperations
import org.gradle.api.provider.Property import org.gradle.api.provider.Property
import org.gradle.api.tasks.IgnoreEmptyDirectories import org.gradle.api.tasks.*
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.jetbrains.compose.internal.utils.registerTask import org.jetbrains.compose.internal.utils.registerTask
import org.jetbrains.compose.internal.utils.uppercaseFirstChar import org.jetbrains.compose.internal.utils.uppercaseFirstChar
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
@ -22,6 +18,8 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation
import org.jetbrains.kotlin.gradle.utils.ObservableSet import org.jetbrains.kotlin.gradle.utils.ObservableSet
import javax.inject.Inject import javax.inject.Inject
private val agp_8_1_0 = AndroidPluginVersion(8, 1, 0)
internal fun Project.configureAndroidComposeResources( internal fun Project.configureAndroidComposeResources(
kotlinExtension: KotlinMultiplatformExtension kotlinExtension: KotlinMultiplatformExtension
) { ) {
@ -53,10 +51,10 @@ internal fun Project.configureAndroidComposeResources(
wiredWith = CopyResourcesToAndroidAssetsTask::outputDirectory wiredWith = CopyResourcesToAndroidAssetsTask::outputDirectory
) )
// addGeneratedSourceDirectory doesn't mark the output directory as assets hence AS Compose Preview doesn't work // https://issuetracker.google.com/348208777
val outDir = copyResources.flatMap { it.outputDirectory.asFile }.get() if (androidComponents.pluginVersion >= agp_8_1_0) {
if (outDir.isDirectory) { // addGeneratedSourceDirectory doesn't mark the output directory as assets hence AS Compose Preview doesn't work
addStaticSourceDirectory(outDir.path) addStaticSourceDirectory(copyResources.flatMap { it.outputDirectory.asFile }.get().path)
} }
// addGeneratedSourceDirectory doesn't run the copyResources task during AS Compose Preview build // addGeneratedSourceDirectory doesn't run the copyResources task during AS Compose Preview build
@ -110,17 +108,18 @@ internal fun Project.fixAndroidLintTaskDependencies() {
} }
} }
// https://issuetracker.google.com/348208777
internal fun Project.configureAndroidAssetsForPreview() { internal fun Project.configureAndroidAssetsForPreview() {
val androidComponents = project.extensions.findByType(AndroidComponentsExtension::class.java) ?: return val androidComponents = project.extensions.findByType(AndroidComponentsExtension::class.java) ?: return
androidComponents.onVariants { variant -> androidComponents.onVariants { variant ->
variant.sources.assets?.apply { variant.sources.assets?.apply {
val kgpCopyAssetsTaskName = "${variant.name}AssetsCopyForAGP" val kgpCopyAssetsTaskName = "${variant.name}AssetsCopyForAGP"
// addGeneratedSourceDirectory doesn't mark the output directory as assets hence AS Compose Preview doesn't work if (androidComponents.pluginVersion >= agp_8_1_0) {
tasks.all { task -> // addGeneratedSourceDirectory doesn't mark the output directory as assets hence AS Compose Preview doesn't work
if (task.name == kgpCopyAssetsTaskName) { tasks.all { task ->
task.outputs.files.forEach { file -> if (task.name == kgpCopyAssetsTaskName) {
if (file.isDirectory) { task.outputs.files.forEach { file ->
addStaticSourceDirectory(file.path) addStaticSourceDirectory(file.path)
} }
} }

Loading…
Cancel
Save