Skip to content

Commit

Permalink
Merge "addressed comments made post-submit." into studio-main
Browse files Browse the repository at this point in the history
  • Loading branch information
Treehugger Robot authored and Android (Google) Code Review committed Feb 9, 2024
2 parents e775798 + fc3f2aa commit fcc3a9a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
8 changes: 4 additions & 4 deletions recipes/legacyTaskBridging/README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,11 +53,11 @@ class CustomPlugin : Plugin<Project> {
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<AssetCreatorTask>("create${variant.name}Asset")
// create the task that will copy new source files to the asset source folder.
val propertyBasedCopyTaskProvider =
project.tasks.register<PropertyBasedCopy>("create${variant.name}Asset")

assetCreationTask.configure { task: AssetCreatorTask ->
propertyBasedCopyTaskProvider.configure { task: PropertyBasedCopy ->
task.from("src/common")
task.include("**/*asset*.*")
}
Expand All @@ -71,8 +67,8 @@ class CustomPlugin : Plugin<Project> {
// 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
)
}

Expand All @@ -91,13 +87,13 @@ class CustomPlugin : Plugin<Project> {
}

/**
* 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
Expand Down

0 comments on commit fcc3a9a

Please sign in to comment.