Browse Source

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

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

21
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
) )
// https://issuetracker.google.com/348208777
if (androidComponents.pluginVersion >= agp_8_1_0) {
// addGeneratedSourceDirectory doesn't mark the output directory as assets hence AS Compose Preview doesn't work // addGeneratedSourceDirectory doesn't mark the output directory as assets hence AS Compose Preview doesn't work
val outDir = copyResources.flatMap { it.outputDirectory.asFile }.get() addStaticSourceDirectory(copyResources.flatMap { it.outputDirectory.asFile }.get().path)
if (outDir.isDirectory) {
addStaticSourceDirectory(outDir.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"
if (androidComponents.pluginVersion >= agp_8_1_0) {
// addGeneratedSourceDirectory doesn't mark the output directory as assets hence AS Compose Preview doesn't work // addGeneratedSourceDirectory doesn't mark the output directory as assets hence AS Compose Preview doesn't work
tasks.all { task -> tasks.all { task ->
if (task.name == kgpCopyAssetsTaskName) { if (task.name == kgpCopyAssetsTaskName) {
task.outputs.files.forEach { file -> task.outputs.files.forEach { file ->
if (file.isDirectory) {
addStaticSourceDirectory(file.path) addStaticSourceDirectory(file.path)
} }
} }

Loading…
Cancel
Save