-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add workerEnabledTransformation recipe
Fixes: NA Test: NA Change-Id: Ib5c5c7439eb56989d1101b56404ad4611ff33a2b
- Loading branch information
1 parent
546f3c7
commit 4ae632b
Showing
15 changed files
with
429 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,3 +90,7 @@ recipe_test( | |
recipe_test( | ||
name = "transformManifest", | ||
) | ||
|
||
recipe_test( | ||
name = "workerEnabledTransformation", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Worker Enable Transformation recipe | ||
|
||
This sample shows how to transform the artifact that has artifact directory type. | ||
It copies the build APK to the specified directory. | ||
|
||
Custom plugin is defined in [CustomPlugin.kt](build-logic/plugins/src/main/kotlin/CustomPlugin.kt). | ||
It registers task [CopyApksTask.kt](build-logic/plugins/src/main/kotlin/CopyApksTask.kt) that creates asynchronous | ||
`WorkAction` that copies APK file. | ||
|
||
WorkAction is part of Gradle [Worker API](https://docs.gradle.org/current/userguide/worker_api.html) | ||
- a standard way of developing parallel tasks. | ||
It enables the division of a task action into smaller, independent units of work. | ||
These units can then be executed simultaneously and independently, maximizing resource utilization | ||
and accelerating build completion. Android Gradle Plugin made it simpler to use Gradle workers API with | ||
[ArtifactTransformationRequest](https://developer.android.com/reference/tools/gradle-api/8.2/com/android/build/api/artifact/ArtifactTransformationRequest). | ||
|
||
## To Run | ||
Just type `./gradlew copyDebugApks` | ||
you will be able to find two APKs: before copying its | ||
`app/build/intermediates/apk/debug/packageDebug/app-debug.apk` | ||
and `app/build/outputs/apk/debug/app-debug.apk` after copying. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2022 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.workerEnabledTransformation") | ||
} | ||
|
||
android { | ||
namespace = "com.example.android.recipes.workerEnabledTransformation" | ||
compileSdk = $COMPILE_SDK | ||
defaultConfig { | ||
minSdk = $MINIMUM_SDK | ||
targetSdk = $COMPILE_SDK | ||
} | ||
} | ||
|
||
java { | ||
toolchain { | ||
languageVersion.set(JavaLanguageVersion.of(17)) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
recipes/workerEnabledTransformation/app/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<!-- | ||
Copyright 2022 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 | ||
http://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. | ||
--> | ||
<application android:label="Minimal"> | ||
</application> | ||
</manifest> |
2 changes: 2 additions & 0 deletions
2
recipes/workerEnabledTransformation/build-logic/gradle.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Gradle properties are not passed to included builds https://github.com/gradle/gradle/issues/2534 | ||
org.gradle.parallel=true |
9 changes: 9 additions & 0 deletions
9
recipes/workerEnabledTransformation/build-logic/gradle/libs.versions.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" } |
40 changes: 40 additions & 0 deletions
40
recipes/workerEnabledTransformation/build-logic/plugins/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2022 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("WorkerEnabledTransformation") { | ||
id = "android.recipes.workerEnabledTransformation" | ||
implementationClass = "CustomPlugin" | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
recipes/workerEnabledTransformation/build-logic/plugins/src/main/kotlin/CopyApksTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (C) 2023 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 | ||
* | ||
* http://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 java.io.Serializable | ||
import java.io.File | ||
import javax.inject.Inject | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.file.RegularFileProperty | ||
import org.gradle.api.file.Directory | ||
import org.gradle.api.file.DirectoryProperty | ||
import org.gradle.api.tasks.InputFiles | ||
import org.gradle.api.tasks.Internal | ||
import org.gradle.api.tasks.OutputDirectory | ||
import org.gradle.api.tasks.TaskAction | ||
import org.gradle.api.provider.Property | ||
import org.gradle.workers.WorkParameters | ||
import org.gradle.workers.WorkerExecutor | ||
import org.gradle.workers.WorkAction | ||
import com.android.build.api.artifact.Artifact | ||
import com.android.build.api.artifact.ArtifactTransformationRequest | ||
import com.android.build.api.variant.BuiltArtifact | ||
|
||
interface WorkItemParameters: WorkParameters, Serializable { | ||
val inputApkFile: RegularFileProperty | ||
val outputApkFile: RegularFileProperty | ||
} | ||
|
||
// This is implementation of [WorkAction] that represents a unit of work. | ||
// A work action should be an abstract class implementing the execute() method. | ||
// In our example `WorkAction` just copies APK from `inputApkFile` to `outputApkFile` location. | ||
// You can check [Gradle docs](https://docs.gradle.org/current/javadoc/org/gradle/workers/WorkAction.html) | ||
// for more information. | ||
abstract class WorkItem @Inject constructor(private val workItemParameters: WorkItemParameters) | ||
: WorkAction<WorkItemParameters> { | ||
override fun execute() { | ||
// business logic that gradle executes - copying artifact | ||
workItemParameters.outputApkFile.get().asFile.delete() | ||
workItemParameters.inputApkFile.asFile.get().copyTo( | ||
workItemParameters.outputApkFile.get().asFile) | ||
} | ||
} | ||
|
||
abstract class CopyApksTask @Inject constructor(private val workers: WorkerExecutor): DefaultTask() { | ||
|
||
@get:InputFiles | ||
abstract val apkFolder: DirectoryProperty | ||
|
||
@get:OutputDirectory | ||
abstract val outFolder: DirectoryProperty | ||
|
||
@get:Internal | ||
abstract val transformationRequest: Property<ArtifactTransformationRequest<CopyApksTask>> | ||
|
||
@TaskAction | ||
fun taskAction() { | ||
// Submits [WorkItem] to process each input of [BuiltArtifact]. | ||
// `WorkItemParameters` serves as a configuration for `WorkItem` | ||
// and needs to be initialized. `workers` is injected as constructor parameter by | ||
// Gradle. We submit the [WorkItem] to the [transformationRequest] that | ||
// will manage the transformation registration and submission to the [workers] engine. | ||
transformationRequest.get().submit( | ||
this, | ||
workers.noIsolation(), | ||
WorkItem::class.java) { | ||
builtArtifact: BuiltArtifact, | ||
outputLocation: Directory, | ||
param: WorkItemParameters -> | ||
// This lambda expression configures instance of [WorkItemParameters] for [BuiltArtifact]. | ||
// After this lambda `WorkItemParameters` has all indromation for `WorkItem` | ||
val inputFile = File(builtArtifact.outputFile) | ||
param.inputApkFile.set(inputFile) | ||
param.outputApkFile.set(File(outputLocation.asFile, inputFile.name)) | ||
param.outputApkFile.get().asFile | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
recipes/workerEnabledTransformation/build-logic/plugins/src/main/kotlin/CustomPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2022 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.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.tasks.OutputDirectory | ||
import org.gradle.api.tasks.TaskAction | ||
import org.gradle.kotlin.dsl.register | ||
import com.android.build.api.artifact.SingleArtifact | ||
|
||
/** | ||
* This custom plugin will register a callback that is applied to all variants. | ||
*/ | ||
class CustomPlugin : Plugin<Project> { | ||
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 -> | ||
|
||
// registering copy<Variant>Apk task and getting provider for it | ||
val copyApksProvider = project.tasks.register("copy${variant.name}Apks", CopyApksTask::class.java) | ||
|
||
// Adds the task as APK transformer. This automatically creates | ||
// a dependency of `copyApks` task to the last transformer of | ||
// SingleArtifact.APK. This also creates transformationRequest of type | ||
// [com.android.build.api.artifact.ArtifactTransformationRequest]. It allows to | ||
// submit WorkAction to Gradle's [WorkQueue] to parallelize | ||
// the transformations. | ||
val transformationRequest = variant.artifacts.use(copyApksProvider) | ||
.wiredWithDirectories( | ||
CopyApksTask::apkFolder, | ||
CopyApksTask::outFolder) | ||
.toTransformMany(SingleArtifact.APK) | ||
|
||
// configures copyApk task by adding transformation request as a property value | ||
copyApksProvider.configure { | ||
it.transformationRequest.set(transformationRequest) | ||
} | ||
} | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
recipes/workerEnabledTransformation/build-logic/settings.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2022 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2022 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
9 changes: 9 additions & 0 deletions
9
recipes/workerEnabledTransformation/gradle/libs.versions.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" } | ||
|
||
|
Oops, something went wrong.