Browse Source

Configure android variants the correct way instead of the hack with 'dependsOn' (#4264)

pull/4268/head
Konstantin 10 months ago committed by GitHub
parent
commit
2b12d57008
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 31
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/AndroidTargetConfiguration.kt
  2. 3
      gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt

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

@ -2,12 +2,12 @@ package org.jetbrains.compose.resources
import com.android.build.api.variant.AndroidComponentsExtension import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.gradle.BaseExtension import com.android.build.gradle.BaseExtension
import com.android.build.gradle.internal.tasks.AndroidVariantTask
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Provider import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Copy import org.gradle.api.tasks.*
import org.gradle.api.tasks.SourceSet
import org.jetbrains.compose.internal.utils.registerTask import org.jetbrains.compose.internal.utils.registerTask
import org.jetbrains.compose.internal.utils.uppercaseFirstChar
import java.io.File import java.io.File
internal fun Project.configureAndroidResources( internal fun Project.configureAndroidResources(
@ -22,24 +22,31 @@ internal fun Project.configureAndroidResources(
androidMainSourceSet.resources.srcDir(commonResourcesDir) androidMainSourceSet.resources.srcDir(commonResourcesDir)
androidMainSourceSet.assets.srcDir(androidFontsDir) androidMainSourceSet.assets.srcDir(androidFontsDir)
val copyFonts = registerTask<Copy>("copyFontsToAndroidAssets") { androidComponents.onVariants { variant ->
val copyFonts = registerTask<CopyAndroidAssetsTask>(
"copy${variant.name.uppercaseFirstChar()}FontsToAndroidAssets"
) {
includeEmptyDirs = false includeEmptyDirs = false
from(commonResourcesDir) from(commonResourcesDir)
include("**/font*/*") include("**/font*/*")
into(androidFontsDir)
onlyIf { onlyIfProvider.get() } onlyIf { onlyIfProvider.get() }
} }
androidComponents.onVariants { variant ->
variant.sources?.assets?.addGeneratedSourceDirectory( variant.sources?.assets?.addGeneratedSourceDirectory(
taskProvider = copyFonts, taskProvider = copyFonts,
wiredWith = { wiredWith = CopyAndroidAssetsTask::outputDirectory
objects.directoryProperty().fileProvider(
copyFonts.map { t -> t.destinationDir }
) )
} }
)
} }
//fixme: it seems like a problem in AGP, so dirty hack now
//https://github.com/JetBrains/compose-multiplatform/issues/4085 //https://github.com/JetBrains/compose-multiplatform/issues/4085
tasks.matching { it is AndroidVariantTask }.configureEach { it.dependsOn(copyFonts) } private abstract class CopyAndroidAssetsTask : Copy() {
@get:OutputDirectory
abstract val outputDirectory: DirectoryProperty
override fun getDestinationDir(): File =
outputDirectory.get().asFile
override fun setDestinationDir(destination: File) {
outputDirectory.set(destination)
}
} }

3
gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/integration/ResourcesTest.kt

@ -113,7 +113,8 @@ class ResourcesTest : GradlePluginTestBase() {
@Test @Test
fun testCopyFontsInAndroidApp(): Unit = with(testProject("misc/commonResources")) { fun testCopyFontsInAndroidApp(): Unit = with(testProject("misc/commonResources")) {
gradle("build").checks { gradle("build").checks {
check.taskSuccessful(":copyFontsToAndroidAssets") check.taskSuccessful(":copyDebugFontsToAndroidAssets")
check.taskSuccessful(":copyReleaseFontsToAndroidAssets")
} }
} }

Loading…
Cancel
Save