diff --git a/recipes/legacyTaskBridging/README.md b/recipes/legacyTaskBridging/README.md index c57caf4d..323d9092 100644 --- a/recipes/legacyTaskBridging/README.md +++ b/recipes/legacyTaskBridging/README.md @@ -1,6 +1,6 @@ # Bridging a Legacy Task with new Variant API Property<> instances -This recipe shows how you can adapt an existing `Task` using intrinsic file types like `File` in order to be +This recipe shows how you can adapt a built-in Gradle `Task` using intrinsic file types like `File` in order to be compatible with the new Variant API. The new variant API requires using instances of `Property<>` when wiring things up in order to carry task dependency within those property objects. This is not easy when you want to use an old task expressing its input or output using `File` for instance. @@ -40,7 +40,7 @@ The easiest way to do that is to subclass the original Task and override the rig output values to properties. In our case, the subclass is simply : ``` -abstract class AssetCreatorTask: Copy() { +abstract class PropertyBasedCopy: Copy() { @get:OutputDirectory abstract val outputDirectory: DirectoryProperty @@ -58,8 +58,8 @@ Once the `TaskProvider` is created, you need to use `SourceDirectories.addGenera output as a new source folder. ``` variant.sources.assets?.addGeneratedSourceDirectory( - assetCreationTask, - AssetCreatorTask::outputDirectory) + propertyBasedCopyTaskProvider, + PropertyBasedCopy::outputDirectory) ``` ### Run the example diff --git a/recipes/legacyTaskBridging/build-logic/plugins/src/main/kotlin/CustomPlugin.kt b/recipes/legacyTaskBridging/build-logic/plugins/src/main/kotlin/CustomPlugin.kt index 6426a984..cf913677 100644 --- a/recipes/legacyTaskBridging/build-logic/plugins/src/main/kotlin/CustomPlugin.kt +++ b/recipes/legacyTaskBridging/build-logic/plugins/src/main/kotlin/CustomPlugin.kt @@ -15,17 +15,13 @@ */ import com.android.build.api.variant.ApplicationAndroidComponentsExtension -import com.android.build.api.variant.BuiltArtifactsLoader -import com.android.build.api.artifact.MultipleArtifact import com.android.build.api.artifact.SingleArtifact import com.android.build.gradle.AppPlugin import java.io.File -import java.util.jar.JarFile import org.gradle.api.DefaultTask import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.file.DirectoryProperty -import org.gradle.api.provider.Property import org.gradle.api.tasks.Copy import org.gradle.api.tasks.InputDirectory import org.gradle.api.tasks.Optional @@ -57,11 +53,11 @@ class CustomPlugin : Plugin { androidComponents.onVariants { variant -> variant.sources.assets ?.let { - // create the task that will add new source files to the asset source folder. - val assetCreationTask = - project.tasks.register("create${variant.name}Asset") + // create the task that will copy new source files to the asset source folder. + val propertyBasedCopyTaskProvider = + project.tasks.register("create${variant.name}Asset") - assetCreationTask.configure { task: AssetCreatorTask -> + propertyBasedCopyTaskProvider.configure { task: PropertyBasedCopy -> task.from("src/common") task.include("**/*asset*.*") } @@ -71,8 +67,8 @@ class CustomPlugin : Plugin { // The task will execute only when the `assets` source folders are looked // up at execution time (during asset merging basically). it.addGeneratedSourceDirectory( - assetCreationTask, - AssetCreatorTask::outputDirectory + propertyBasedCopyTaskProvider, + PropertyBasedCopy::outputDirectory ) } @@ -91,13 +87,13 @@ class CustomPlugin : Plugin { } /** - * This task is creating an asset that will be used as a source asset file. + * This task is copying files. * * It is based on the Gradle's [org.gradle.api.tasks.Copy] task and bridge the * `destinationDir` output to a `DirectoryProperty` that can be used with the * Variant APIs. */ -abstract class AssetCreatorTask: Copy() { +abstract class PropertyBasedCopy: Copy() { @get:OutputDirectory abstract val outputDirectory: DirectoryProperty