Skip to content

Commit e53e3d3

Browse files
committed
Add recipe for MultipleArtifactTypeOutOperationRequest.toListenTo()
Bug: n/a Test: n/a Change-Id: I309661b1d3b11a128cfec592342ac99734c0f135
1 parent 3489bc8 commit e53e3d3

File tree

18 files changed

+456
-6
lines changed

18 files changed

+456
-6
lines changed

BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,7 @@ recipe_test(
161161
recipe_test(
162162
name = "registerPreBuild",
163163
)
164+
165+
recipe_test(
166+
name = "listenToMultipleArtifact",
167+
)

recipes/listenToArtifacts/README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@ called when the task creating the artifact runs. This allows tasks that must pro
55
to run without changing what task should be called by the user. In this particular example, the
66
task makes a copy of the APKs while renaming them.
77

8-
98
Custom plugin is defined in [CustomPlugin.kt](build-logic/plugins/src/main/kotlin/CustomPlugin.kt).
109
It registers task [CopyApk.kt](build-logic/plugins/src/main/kotlin/CopyApk.kt) that simply copies
1110
the file somewhere else using each APK's own metadata information.
1211

1312
`SingleArtifact.APK` is an artifact of type `Artifact.ContainsMany`. This is a type of directory-based
14-
artifact that can actually contain many artifacts. Each artifact is associated with specificy meta-data.
13+
artifact that can actually contain many artifacts. Each artifact is associated with specific meta-data.
1514

1615
Reading these artifacts from the directory require usage of `BuiltArtifactsLoader`.
1716

18-
1917
## To Run
2018
Just type `./gradlew app:assembleDebug`
21-
You will be able to find the renamed APKs at `app/build/outputs/renamedintermediates/apk/debug/packageDebug/app-debug.apk`
22-
and `app/build/outputs/apk/debug/app-debug.apk` after copying.
19+
You will be able to find the renamed APK at `app/build/outputs/renamed_apks/debug/debug-Feb2024-12.apk` after copying.

recipes/listenToArtifacts/build-logic/plugins/src/main/kotlin/CustomPlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class CustomPlugin : Plugin<Project> {
7575
// automatically when the normal APK packaging task run.
7676
// So we set the input manually, and the validation task will have to be called
7777
// separately (in a separate Gradle execution or Gradle will detect the
78-
// lack of dependency between the 2 tasks and complain.
78+
// lack of dependency between the 2 tasks and complain).
7979
input.set(project.layout.buildDirectory.dir("outputs/renamed_apks/${variant.name}"))
8080
variantName.set(variant.name)
8181
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# toListenTo recipe for Artifact.Multiple
2+
3+
This sample shows how to react to a change to an artifact of type [`Artifact.Multiple`](https://developer.android.com/reference/tools/gradle-api/current/com/android/build/api/artifact/Artifact.Multiple) that causes the artifact
4+
to be regenerated. The API will make sure that the task is called when the task regenerating the artifact runs. This
5+
allows tasks that must process an artifact to run without changing what task should be called by the user. In this
6+
particular example, the task makes a copy of the multi-dex proguard files while renaming them.
7+
8+
Custom plugin is defined in [CustomPlugin.kt](build-logic/plugins/src/main/kotlin/CustomPlugin.kt).
9+
It registers task [CopyProguardTask.kt](build-logic/plugins/src/main/kotlin/CopyProguardTask.kt) that simply copies
10+
a proguard file somewhere else and renames it.
11+
12+
The API used in this recipe is [`MultipleArtifactTypeOutOperationRequest.toListenTo()`](https://developer.android.com/reference/tools/gradle-api/current/com/android/build/api/artifact/MultipleArtifactTypeOutOperationRequest#toListenTo(com.android.build.api.artifact.Artifact.Multiple)),
13+
and this is wired up to the task with [`TaskBasedOperation.wiredWithMultiple()`](https://developer.android.com/reference/tools/gradle-api/current/com/android/build/api/artifact/TaskBasedOperation#wiredWithMultiple(kotlin.Function1)).
14+
Below is an example usage of the APIs:
15+
16+
```
17+
variant.artifacts.use(copyTask).wiredWithMultiple {
18+
it.proguardFiles
19+
}.toListenTo(MultipleArtifact.MULTIDEX_KEEP_PROGUARD)
20+
```
21+
22+
## To Run
23+
Just type `./gradlew app:assembleDebug`
24+
You will be able to find the renamed metadata file at
25+
`app/build/renamed_intermediates/native_debug_metadata/debug/renamed_test_file.dbg` after copying.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
plugins {
18+
alias(libs.plugins.android.application)
19+
alias(libs.plugins.kotlin.android)
20+
id("android.recipes.custom_plugin")
21+
}
22+
23+
android {
24+
namespace = "com.example.android.recipes.listentomultipleartifact"
25+
compileSdk = $COMPILE_SDK
26+
defaultConfig {
27+
minSdk = $MINIMUM_SDK
28+
targetSdk = $COMPILE_SDK
29+
}
30+
31+
// This is necessary to enable the tasks that use NATIVE_DEBUG_METADATA
32+
buildTypes {
33+
debug {
34+
ndk {
35+
debugSymbolLevel = "FULL"
36+
}
37+
}
38+
}
39+
}
40+
41+
java {
42+
toolchain {
43+
languageVersion.set(JavaLanguageVersion.of(17))
44+
}
45+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test data
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
<!--
4+
Copyright 2024 The Android Open Source Project
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
-->
15+
<application android:label="Minimal">
16+
</application>
17+
</manifest>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Gradle properties are not passed to included builds https://github.com/gradle/gradle/issues/2534
2+
org.gradle.parallel=true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[versions]
2+
androidGradlePlugin = $AGP_VERSION
3+
kotlin = $KOTLIN_VERSION
4+
5+
[libraries]
6+
android-gradlePlugin-api = { group = "com.android.tools.build", name = "gradle-api", version.ref = "androidGradlePlugin" }
7+
8+
[plugins]
9+
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
plugins {
18+
`java-gradle-plugin`
19+
alias(libs.plugins.kotlin.jvm)
20+
}
21+
22+
java {
23+
toolchain {
24+
languageVersion.set(JavaLanguageVersion.of(17))
25+
}
26+
}
27+
28+
dependencies {
29+
compileOnly(libs.android.gradlePlugin.api)
30+
implementation(gradleKotlinDsl())
31+
}
32+
33+
gradlePlugin {
34+
plugins {
35+
create("customPlugin") {
36+
id = "android.recipes.custom_plugin"
37+
implementationClass = "CustomPlugin"
38+
}
39+
}
40+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import org.gradle.api.DefaultTask
18+
import org.gradle.api.file.Directory
19+
import org.gradle.api.file.DirectoryProperty
20+
import org.gradle.api.provider.ListProperty
21+
import org.gradle.api.tasks.InputFiles
22+
import org.gradle.api.tasks.OutputDirectory
23+
import org.gradle.api.tasks.PathSensitive
24+
import org.gradle.api.tasks.PathSensitivity
25+
import org.gradle.api.tasks.TaskAction
26+
import java.io.File
27+
28+
/**
29+
* This task will receive the native debug metadata folder and copy the contents to the output
30+
*/
31+
abstract class CopyNativeDebugMetadataTask : DefaultTask() {
32+
33+
@get:InputFiles
34+
@get:PathSensitive(PathSensitivity.RELATIVE)
35+
abstract val debugMetadataDirectories: ListProperty<Directory>
36+
37+
@get:OutputDirectory
38+
abstract val output: DirectoryProperty
39+
40+
@TaskAction
41+
fun taskAction() {
42+
// delete the previous content. This task does not support incremental mode but could be modified to do so
43+
val outputDirectory = output.get()
44+
val outputFile = outputDirectory.asFile
45+
46+
outputFile.deleteRecursively()
47+
outputFile.mkdirs()
48+
49+
val debugMetadataFile = File(debugMetadataDirectories.get().first().asFile, "test_file.dbg")
50+
debugMetadataFile.copyTo(File(outputFile, "renamed_test_file.dbg"))
51+
}
52+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import com.android.build.api.artifact.MultipleArtifact
18+
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
19+
import com.android.build.gradle.AppPlugin
20+
import org.gradle.api.DefaultTask
21+
import org.gradle.api.Plugin
22+
import org.gradle.api.Project
23+
import org.gradle.api.file.DirectoryProperty
24+
import org.gradle.api.provider.Property
25+
import org.gradle.api.tasks.InputDirectory
26+
import org.gradle.api.tasks.TaskAction
27+
import org.gradle.configurationcache.extensions.capitalized
28+
import org.gradle.kotlin.dsl.register
29+
30+
/**
31+
* This custom plugin will register a callback that is applied to all variants.
32+
*/
33+
class CustomPlugin : Plugin<Project> {
34+
override fun apply(project: Project) {
35+
36+
// Registers a callback on the application of the Android Application plugin.
37+
// This allows the CustomPlugin to work whether it's applied before or after
38+
// the Android Application plugin.
39+
project.plugins.withType(AppPlugin::class.java) {
40+
41+
// Queries for the extension set by the Android Application plugin.
42+
// This is the second of two entry points into the Android Gradle plugin
43+
val androidComponents =
44+
project.extensions.getByType(ApplicationAndroidComponentsExtension::class.java)
45+
// Registers a callback to be called, when a new variant is configured
46+
androidComponents.onVariants { variant ->
47+
48+
// -- Setup --
49+
// the following is done for the sake of the recipe only, in order to add directories to
50+
// MultipleArtifact.NATIVE_DEBUG_METADATA so that there is something to copy
51+
variant.artifacts.addStaticDirectory(
52+
MultipleArtifact.NATIVE_DEBUG_METADATA,
53+
project.layout.projectDirectory.dir("nativeDebugMetadataDir")
54+
)
55+
56+
// create a task that will be responsible for copying and renaming a native debug metadata file
57+
val copyTaskName = "copyNativeDebugMetadataFor${variant.name.capitalized()}"
58+
val copyTask = project.tasks.register<CopyNativeDebugMetadataTask>(copyTaskName) {
59+
// set the output only. the input will be automatically provided via the wiring mechanism
60+
output.set(project.layout.buildDirectory.dir(
61+
"renamed_intermediates/native_debug_metadata/${variant.name}")
62+
)
63+
}
64+
65+
// Wire the task to respond to artifact creation
66+
variant.artifacts.use(copyTask).wiredWithMultiple {
67+
it.debugMetadataDirectories
68+
}.toListenTo(MultipleArtifact.NATIVE_DEBUG_METADATA)
69+
70+
// -- Verification --
71+
// the following is just to validate the recipe and is not actually part of the recipe itself
72+
project.tasks.register<ValidateTask>("validate${variant.name.capitalized()}") {
73+
// The input of the validation task should be the output of the copy task.
74+
// The normal way to do this would be:
75+
// input.set(copyTask.flatMap { it.output }
76+
// However, doing this will force running the copy task when we want it to run
77+
// automatically when the normal AGP tasks run.
78+
// So we set the input manually, and the validation task will have to be called
79+
// separately (in a separate Gradle execution or Gradle will detect the
80+
// lack of dependency between the 2 tasks and complain).
81+
input.set(project.layout.buildDirectory.dir(
82+
"renamed_intermediates/native_debug_metadata/${variant.name}")
83+
)
84+
}
85+
}
86+
}
87+
}
88+
}
89+
90+
/**
91+
* Validation task to verify the behavior of the recipe
92+
*/
93+
abstract class ValidateTask : DefaultTask() {
94+
95+
@get:InputDirectory
96+
abstract val input: DirectoryProperty
97+
98+
@TaskAction
99+
fun taskAction() {
100+
val renamedNativeDebugMetadataFile = input.get().file("renamed_test_file.dbg").asFile
101+
if (!renamedNativeDebugMetadataFile.exists()) {
102+
throw RuntimeException("Expected file missing: $renamedNativeDebugMetadataFile")
103+
}
104+
}
105+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
rootProject.name = "build-logic"
18+
19+
pluginManagement {
20+
repositories {
21+
$AGP_REPOSITORY
22+
$PLUGIN_REPOSITORIES
23+
}
24+
}
25+
26+
dependencyResolutionManagement {
27+
repositories {
28+
$AGP_REPOSITORY
29+
$DEPENDENCY_REPOSITORIES
30+
}
31+
}
32+
33+
include(":plugins")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
plugins {
18+
alias(libs.plugins.android.application) apply false
19+
alias(libs.plugins.kotlin.android) apply false
20+
}

0 commit comments

Comments
 (0)