diff --git a/BUILD b/BUILD index e34639c3..0d2afdde 100644 --- a/BUILD +++ b/BUILD @@ -161,3 +161,7 @@ recipe_test( recipe_test( name = "registerPreBuild", ) + +recipe_test( + name = "listenToMultipleArtifact", +) diff --git a/recipes/listenToArtifacts/README.md b/recipes/listenToArtifacts/README.md index 34eb16ba..313847db 100644 --- a/recipes/listenToArtifacts/README.md +++ b/recipes/listenToArtifacts/README.md @@ -5,18 +5,15 @@ called when the task creating the artifact runs. This allows tasks that must pro to run without changing what task should be called by the user. In this particular example, the task makes a copy of the APKs while renaming them. - Custom plugin is defined in [CustomPlugin.kt](build-logic/plugins/src/main/kotlin/CustomPlugin.kt). It registers task [CopyApk.kt](build-logic/plugins/src/main/kotlin/CopyApk.kt) that simply copies the file somewhere else using each APK's own metadata information. `SingleArtifact.APK` is an artifact of type `Artifact.ContainsMany`. This is a type of directory-based -artifact that can actually contain many artifacts. Each artifact is associated with specificy meta-data. +artifact that can actually contain many artifacts. Each artifact is associated with specific meta-data. Reading these artifacts from the directory require usage of `BuiltArtifactsLoader`. - ## To Run Just type `./gradlew app:assembleDebug` -You will be able to find the renamed APKs at `app/build/outputs/renamedintermediates/apk/debug/packageDebug/app-debug.apk` -and `app/build/outputs/apk/debug/app-debug.apk` after copying. +You will be able to find the renamed APK at `app/build/outputs/renamed_apks/debug/debug-Feb2024-12.apk` after copying. diff --git a/recipes/listenToArtifacts/build-logic/plugins/src/main/kotlin/CustomPlugin.kt b/recipes/listenToArtifacts/build-logic/plugins/src/main/kotlin/CustomPlugin.kt index 88bcd7ce..44a0ca38 100644 --- a/recipes/listenToArtifacts/build-logic/plugins/src/main/kotlin/CustomPlugin.kt +++ b/recipes/listenToArtifacts/build-logic/plugins/src/main/kotlin/CustomPlugin.kt @@ -75,7 +75,7 @@ class CustomPlugin : Plugin { // automatically when the normal APK packaging task run. // So we set the input manually, and the validation task will have to be called // separately (in a separate Gradle execution or Gradle will detect the - // lack of dependency between the 2 tasks and complain. + // lack of dependency between the 2 tasks and complain). input.set(project.layout.buildDirectory.dir("outputs/renamed_apks/${variant.name}")) variantName.set(variant.name) } diff --git a/recipes/listenToMultipleArtifact/README.md b/recipes/listenToMultipleArtifact/README.md new file mode 100644 index 00000000..4a999f3a --- /dev/null +++ b/recipes/listenToMultipleArtifact/README.md @@ -0,0 +1,25 @@ +# toListenTo recipe for Artifact.Multiple + +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 +to be regenerated. The API will make sure that the task is called when the task regenerating the artifact runs. This +allows tasks that must process an artifact to run without changing what task should be called by the user. In this +particular example, the task makes a copy of the multi-dex proguard files while renaming them. + +Custom plugin is defined in [CustomPlugin.kt](build-logic/plugins/src/main/kotlin/CustomPlugin.kt). +It registers task [CopyProguardTask.kt](build-logic/plugins/src/main/kotlin/CopyProguardTask.kt) that simply copies +a proguard file somewhere else and renames it. + +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)), +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)). +Below is an example usage of the APIs: + +``` +variant.artifacts.use(copyTask).wiredWithMultiple { + it.proguardFiles +}.toListenTo(MultipleArtifact.MULTIDEX_KEEP_PROGUARD) +``` + +## To Run +Just type `./gradlew app:assembleDebug` +You will be able to find the renamed metadata file at +`app/build/renamed_intermediates/native_debug_metadata/debug/renamed_test_file.dbg` after copying. diff --git a/recipes/listenToMultipleArtifact/app/build.gradle.kts b/recipes/listenToMultipleArtifact/app/build.gradle.kts new file mode 100644 index 00000000..233752c3 --- /dev/null +++ b/recipes/listenToMultipleArtifact/app/build.gradle.kts @@ -0,0 +1,45 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +plugins { + alias(libs.plugins.android.application) + alias(libs.plugins.kotlin.android) + id("android.recipes.custom_plugin") +} + +android { + namespace = "com.example.android.recipes.listentomultipleartifact" + compileSdk = $COMPILE_SDK + defaultConfig { + minSdk = $MINIMUM_SDK + targetSdk = $COMPILE_SDK + } + + // This is necessary to enable the tasks that use NATIVE_DEBUG_METADATA + buildTypes { + debug { + ndk { + debugSymbolLevel = "FULL" + } + } + } +} + +java { + toolchain { + languageVersion.set(JavaLanguageVersion.of(17)) + } +} diff --git a/recipes/listenToMultipleArtifact/app/nativeDebugMetadataDir/test_file.dbg b/recipes/listenToMultipleArtifact/app/nativeDebugMetadataDir/test_file.dbg new file mode 100644 index 00000000..0aa6fb54 --- /dev/null +++ b/recipes/listenToMultipleArtifact/app/nativeDebugMetadataDir/test_file.dbg @@ -0,0 +1 @@ +test data \ No newline at end of file diff --git a/recipes/listenToMultipleArtifact/app/src/main/AndroidManifest.xml b/recipes/listenToMultipleArtifact/app/src/main/AndroidManifest.xml new file mode 100644 index 00000000..8b7fed3d --- /dev/null +++ b/recipes/listenToMultipleArtifact/app/src/main/AndroidManifest.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/recipes/listenToMultipleArtifact/build-logic/gradle.properties b/recipes/listenToMultipleArtifact/build-logic/gradle.properties new file mode 100644 index 00000000..3dcf88f0 --- /dev/null +++ b/recipes/listenToMultipleArtifact/build-logic/gradle.properties @@ -0,0 +1,2 @@ +# Gradle properties are not passed to included builds https://github.com/gradle/gradle/issues/2534 +org.gradle.parallel=true diff --git a/recipes/listenToMultipleArtifact/build-logic/gradle/libs.versions.toml b/recipes/listenToMultipleArtifact/build-logic/gradle/libs.versions.toml new file mode 100644 index 00000000..d362ae08 --- /dev/null +++ b/recipes/listenToMultipleArtifact/build-logic/gradle/libs.versions.toml @@ -0,0 +1,9 @@ +[versions] +androidGradlePlugin = $AGP_VERSION +kotlin = $KOTLIN_VERSION + +[libraries] +android-gradlePlugin-api = { group = "com.android.tools.build", name = "gradle-api", version.ref = "androidGradlePlugin" } + +[plugins] +kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } diff --git a/recipes/listenToMultipleArtifact/build-logic/plugins/build.gradle.kts b/recipes/listenToMultipleArtifact/build-logic/plugins/build.gradle.kts new file mode 100644 index 00000000..4c8f1a08 --- /dev/null +++ b/recipes/listenToMultipleArtifact/build-logic/plugins/build.gradle.kts @@ -0,0 +1,40 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +plugins { + `java-gradle-plugin` + alias(libs.plugins.kotlin.jvm) +} + +java { + toolchain { + languageVersion.set(JavaLanguageVersion.of(17)) + } +} + +dependencies { + compileOnly(libs.android.gradlePlugin.api) + implementation(gradleKotlinDsl()) +} + +gradlePlugin { + plugins { + create("customPlugin") { + id = "android.recipes.custom_plugin" + implementationClass = "CustomPlugin" + } + } +} diff --git a/recipes/listenToMultipleArtifact/build-logic/plugins/src/main/kotlin/CopyNativeDebugMetadataTask.kt b/recipes/listenToMultipleArtifact/build-logic/plugins/src/main/kotlin/CopyNativeDebugMetadataTask.kt new file mode 100644 index 00000000..dbb3de46 --- /dev/null +++ b/recipes/listenToMultipleArtifact/build-logic/plugins/src/main/kotlin/CopyNativeDebugMetadataTask.kt @@ -0,0 +1,52 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.gradle.api.DefaultTask +import org.gradle.api.file.Directory +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.provider.ListProperty +import org.gradle.api.tasks.InputFiles +import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.PathSensitive +import org.gradle.api.tasks.PathSensitivity +import org.gradle.api.tasks.TaskAction +import java.io.File + +/** + * This task will receive the native debug metadata folder and copy the contents to the output + */ +abstract class CopyNativeDebugMetadataTask : DefaultTask() { + + @get:InputFiles + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val debugMetadataDirectories: ListProperty + + @get:OutputDirectory + abstract val output: DirectoryProperty + + @TaskAction + fun taskAction() { + // delete the previous content. This task does not support incremental mode but could be modified to do so + val outputDirectory = output.get() + val outputFile = outputDirectory.asFile + + outputFile.deleteRecursively() + outputFile.mkdirs() + + val debugMetadataFile = File(debugMetadataDirectories.get().first().asFile, "test_file.dbg") + debugMetadataFile.copyTo(File(outputFile, "renamed_test_file.dbg")) + } +} diff --git a/recipes/listenToMultipleArtifact/build-logic/plugins/src/main/kotlin/CustomPlugin.kt b/recipes/listenToMultipleArtifact/build-logic/plugins/src/main/kotlin/CustomPlugin.kt new file mode 100644 index 00000000..52e01365 --- /dev/null +++ b/recipes/listenToMultipleArtifact/build-logic/plugins/src/main/kotlin/CustomPlugin.kt @@ -0,0 +1,105 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import com.android.build.api.artifact.MultipleArtifact +import com.android.build.api.variant.ApplicationAndroidComponentsExtension +import com.android.build.gradle.AppPlugin +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.InputDirectory +import org.gradle.api.tasks.TaskAction +import org.gradle.configurationcache.extensions.capitalized +import org.gradle.kotlin.dsl.register + +/** + * This custom plugin will register a callback that is applied to all variants. + */ +class CustomPlugin : Plugin { + override fun apply(project: Project) { + + // Registers a callback on the application of the Android Application plugin. + // This allows the CustomPlugin to work whether it's applied before or after + // the Android Application plugin. + project.plugins.withType(AppPlugin::class.java) { + + // Queries for the extension set by the Android Application plugin. + // This is the second of two entry points into the Android Gradle plugin + val androidComponents = + project.extensions.getByType(ApplicationAndroidComponentsExtension::class.java) + // Registers a callback to be called, when a new variant is configured + androidComponents.onVariants { variant -> + + // -- Setup -- + // the following is done for the sake of the recipe only, in order to add directories to + // MultipleArtifact.NATIVE_DEBUG_METADATA so that there is something to copy + variant.artifacts.addStaticDirectory( + MultipleArtifact.NATIVE_DEBUG_METADATA, + project.layout.projectDirectory.dir("nativeDebugMetadataDir") + ) + + // create a task that will be responsible for copying and renaming a native debug metadata file + val copyTaskName = "copyNativeDebugMetadataFor${variant.name.capitalized()}" + val copyTask = project.tasks.register(copyTaskName) { + // set the output only. the input will be automatically provided via the wiring mechanism + output.set(project.layout.buildDirectory.dir( + "renamed_intermediates/native_debug_metadata/${variant.name}") + ) + } + + // Wire the task to respond to artifact creation + variant.artifacts.use(copyTask).wiredWithMultiple { + it.debugMetadataDirectories + }.toListenTo(MultipleArtifact.NATIVE_DEBUG_METADATA) + + // -- Verification -- + // the following is just to validate the recipe and is not actually part of the recipe itself + project.tasks.register("validate${variant.name.capitalized()}") { + // The input of the validation task should be the output of the copy task. + // The normal way to do this would be: + // input.set(copyTask.flatMap { it.output } + // However, doing this will force running the copy task when we want it to run + // automatically when the normal AGP tasks run. + // So we set the input manually, and the validation task will have to be called + // separately (in a separate Gradle execution or Gradle will detect the + // lack of dependency between the 2 tasks and complain). + input.set(project.layout.buildDirectory.dir( + "renamed_intermediates/native_debug_metadata/${variant.name}") + ) + } + } + } + } +} + +/** + * Validation task to verify the behavior of the recipe + */ +abstract class ValidateTask : DefaultTask() { + + @get:InputDirectory + abstract val input: DirectoryProperty + + @TaskAction + fun taskAction() { + val renamedNativeDebugMetadataFile = input.get().file("renamed_test_file.dbg").asFile + if (!renamedNativeDebugMetadataFile.exists()) { + throw RuntimeException("Expected file missing: $renamedNativeDebugMetadataFile") + } + } +} diff --git a/recipes/listenToMultipleArtifact/build-logic/settings.gradle.kts b/recipes/listenToMultipleArtifact/build-logic/settings.gradle.kts new file mode 100644 index 00000000..e2e5e9e5 --- /dev/null +++ b/recipes/listenToMultipleArtifact/build-logic/settings.gradle.kts @@ -0,0 +1,33 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +rootProject.name = "build-logic" + +pluginManagement { + repositories { + $AGP_REPOSITORY + $PLUGIN_REPOSITORIES + } +} + +dependencyResolutionManagement { + repositories { + $AGP_REPOSITORY + $DEPENDENCY_REPOSITORIES + } +} + +include(":plugins") diff --git a/recipes/listenToMultipleArtifact/build.gradle.kts b/recipes/listenToMultipleArtifact/build.gradle.kts new file mode 100644 index 00000000..68e631f2 --- /dev/null +++ b/recipes/listenToMultipleArtifact/build.gradle.kts @@ -0,0 +1,20 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +plugins { + alias(libs.plugins.android.application) apply false + alias(libs.plugins.kotlin.android) apply false +} diff --git a/recipes/listenToMultipleArtifact/gradle.properties b/recipes/listenToMultipleArtifact/gradle.properties new file mode 100644 index 00000000..55cce922 --- /dev/null +++ b/recipes/listenToMultipleArtifact/gradle.properties @@ -0,0 +1,23 @@ +# Project-wide Gradle settings. +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +org.gradle.parallel=true +# AndroidX package structure to make it clearer which packages are bundled with the +# Android operating system, and which are packaged with your app's APK +# https://developer.android.com/topic/libraries/support-library/androidx-rn +android.useAndroidX=true +# Kotlin code style for this project: "official" or "obsolete": +kotlin.code.style=official +# Enables namespacing of each library's R class so that its R class includes only the +# resources declared in the library itself and none from the library's dependencies, +# thereby reducing the size of the R class for that library +android.nonTransitiveRClass=true diff --git a/recipes/listenToMultipleArtifact/gradle/libs.versions.toml b/recipes/listenToMultipleArtifact/gradle/libs.versions.toml new file mode 100644 index 00000000..8c672bba --- /dev/null +++ b/recipes/listenToMultipleArtifact/gradle/libs.versions.toml @@ -0,0 +1,9 @@ +[versions] +androidGradlePlugin = $AGP_VERSION +kotlin = $KOTLIN_VERSION + +[plugins] +android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" } +kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } + + diff --git a/recipes/listenToMultipleArtifact/recipe_metadata.toml b/recipes/listenToMultipleArtifact/recipe_metadata.toml new file mode 100644 index 00000000..cbbf3a8d --- /dev/null +++ b/recipes/listenToMultipleArtifact/recipe_metadata.toml @@ -0,0 +1,33 @@ +# optional (if present and non-blank) name to use in the index +indexName = "" +# optional (if present and non-blank) folder name to use when converting recipe in RELEASE mode +destinationFolder = "" + +description =""" + Recipe that listens to the creation of native debug metadata to copy it somewhere else + """ + +[agpVersion] +min = "8.4.0-alpha10" + +# Relevant Gradle tasks to run per recipe +[gradleTasks] +tasks = [ + "assembleDebug", +] +validationTasks = [ + "validateDebug" +] + +# All the relevant metadata fields to create an index based on language/API/etc' +[indexMetadata] +index = [ + "Themes/Artifact API", + "APIs/AndroidComponentsExtension.onVariants()", + "Call chains/androidComponents.onVariants {}", + "Call chains/variant.artifacts.use().wiredWith().toListenTo()", + "APIs/Artifacts.use()", + "APIs/TaskBasedOperation.wiredWithMultiple()", + "APIs/MultipleArtifactTypeOutOperationRequest.toListenTo()", + "APIs/MultipleArtifact.NATIVE_DEBUG_METADATA", +] diff --git a/recipes/listenToMultipleArtifact/settings.gradle.kts b/recipes/listenToMultipleArtifact/settings.gradle.kts new file mode 100644 index 00000000..0229656b --- /dev/null +++ b/recipes/listenToMultipleArtifact/settings.gradle.kts @@ -0,0 +1,35 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +rootProject.name = "listenToMultipleArtifact" + +pluginManagement { + includeBuild("build-logic") + repositories { + $AGP_REPOSITORY + $PLUGIN_REPOSITORIES + } +} + +dependencyResolutionManagement { + repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) + repositories { + $AGP_REPOSITORY + $DEPENDENCY_REPOSITORIES + } +} + +include(":app")