@ -4,15 +4,23 @@ import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.api.variant.Variant
import com.android.build.gradle.internal.lint.AndroidLintAnalysisTask
import com.android.build.gradle.internal.lint.LintModelWriterTask
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.Copy
import org.jetbrains.compose.internal.utils.dir
import org.gradle.api.file.FileSystemOperations
import org.gradle.api.provider.Property
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.utils.registerTask
import org.jetbrains.compose.internal.utils.uppercaseFirstChar
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget
import javax.inject.Inject
internal fun Project . getAndroidVariantComposeResources (
private fun Project . getAndroidVariantComposeResources (
kotlinExtension : KotlinMultiplatformExtension ,
variant : Variant
) : FileCollection = project . files ( {
@ -27,46 +35,27 @@ internal fun Project.getAndroidVariantComposeResources(
}
} )
internal fun Project . getKgpAndroidVariantComposeResources (
variant : Variant
) : FileCollection = project . files ( {
val taskName = " ${variant.name} AssetsCopyForAGP "
if ( tasks . names . contains ( taskName ) ) tasks . named ( taskName ) . map { it . outputs . files }
else files ( )
} )
internal fun Project . configureAndroidComposeResources (
getVariantComposeResources : ( Variant ) -> FileCollection
) {
internal fun Project . configureAndroidComposeResources ( ) {
//copy all compose resources to android assets
val androidComponents = project . extensions . findByType ( AndroidComponentsExtension :: class . java ) ?: return
val kotlinExtension = project . extensions . findByType ( KotlinMultiplatformExtension :: class . java ) ?: return
androidComponents . onVariants { variant ->
val camelVariantName = variant . name . uppercaseFirstChar ( )
val variantAssets = getVariantComposeResources ( variant )
val variantAssetsDir = layout . buildDirectory . dir ( RES _GEN _DIR ) . dir ( variant . name + " AndroidAssets " )
val variantAssets = getAndroidVariantComposeResources ( kotlinExtension , variant )
val copyVariantAssets = tasks . register (
" copy ${camelVariantName} ComposeResourcesToAndroidAssets " ,
Copy :: class . java
) { task ->
task . from ( variantAssets )
task . into ( variantAssetsDir )
val copyVariantAssets = registerTask < CopyResourcesToAndroidAssetsTask > (
" copy ${camelVariantName} ComposeResourcesToAndroidAssets "
) {
from . set ( variantAssets )
}
//https://issuetracker.google.com/348208777
val staticDir = variantAssetsDir . get ( ) . asFile
staticDir . mkdirs ( )
variant . sources . assets ?. addStaticSourceDirectory ( staticDir . path )
val agpTaskNames = listOf (
//fix agp task dependencies for build and allTests tasks
" merge ${camelVariantName} Assets " ,
" package ${camelVariantName} Assets " ,
//fix agp task dependencies for AndroidStudio preview
" compile ${camelVariantName} Sources " ,
variant . sources . assets ?. addGeneratedSourceDirectory (
copyVariantAssets ,
CopyResourcesToAndroidAssetsTask :: outputDirectory
)
tasks . configureEach { task ->
if ( task . name in agpTaskNames ) {
//fix agp task dependencies for AndroidStudio preview
if ( task . name == " compile ${camelVariantName} Sources " ) {
task . dependsOn ( copyVariantAssets )
}
//fix linter task dependencies for `build` task
@ -77,6 +66,28 @@ internal fun Project.configureAndroidComposeResources(
}
}
//Copy task doesn't work with 'variant.sources?.assets?.addGeneratedSourceDirectory' API
internal abstract class CopyResourcesToAndroidAssetsTask : DefaultTask ( ) {
@get : Inject
abstract val fileSystem : FileSystemOperations
@get : InputFiles
@get : IgnoreEmptyDirectories
abstract val from : Property < FileCollection >
@get : OutputDirectory
abstract val outputDirectory : DirectoryProperty
@TaskAction
fun action ( ) {
fileSystem . copy {
it . includeEmptyDirs = false
it . from ( from )
it . into ( outputDirectory )
}
}
}
/ *
There is a dirty fix for the problem :
@ -93,4 +104,15 @@ internal fun Project.fixAndroidLintTaskDependencies() {
} . configureEach {
it . mustRunAfter ( tasks . withType ( GenerateResourceAccessorsTask :: class . java ) )
}
}
internal fun Project . fixKgpAndroidPreviewTaskDependencies ( ) {
val androidComponents = project . extensions . findByType ( AndroidComponentsExtension :: class . java ) ?: return
androidComponents . onVariants { variant ->
tasks . configureEach { task ->
if ( task . name == " compile ${variant.name.uppercaseFirstChar()} Sources " ) {
task . dependsOn ( " ${variant.name} AssetsCopyForAGP " )
}
}
}
}