Skip to content

Commit

Permalink
Add a recipe for showing the usage of onVariants API
Browse files Browse the repository at this point in the history
Bug: n/a
Test: this is a test
Change-Id: Ifc56e46200e599bf38a284fa0b37c610b4d65415
  • Loading branch information
micahjo7 committed Nov 22, 2023
1 parent e17438f commit 60674d7
Show file tree
Hide file tree
Showing 14 changed files with 388 additions and 0 deletions.
4 changes: 4 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ recipe_test(
recipe_test(
name = "transformAllClasses",
)

recipe_test(
name = "onVariants",
)
58 changes: 58 additions & 0 deletions recipes/onVariants/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Registering callbacks using onVariants in plugin

This recipe shows how to access and modify the variant objects in your project.
Plugin demonstrates a possible usages of the onVariants() API in
[CustomPlugin.kt](build-logic/plugins/src/main/kotlin/CustomPlugin.kt).


| Module | Content |
|----------------------------|--------------------------------------------------------------------------|
| [build-logic](build-logic) | Contains the Project plugin that is the core of the recipe. |
| [app](app) | An Android application that will be configured with additional variants. |


The onVariants API differs from beforeVariants in that it gives access to the variants only after all the artifacts
produced by AGP have been determined. This means the only values that can be modified are `Property` values, which
are resolved during task execution. One of these properties is the application ID, which is used as an example in this
recipe. These properties can then be wired up to providers from custom tasks, as this recipe demonstrates.

## Details
### Configuring variants
Variants are set with two predefined build types `debug` and `release`.

### onVariants
The onVariants API also supports `VariantSelectors`.

Recipe has multiple examples of usage onVariants API in Kotlin:

Following snippet selects all variants with `release` build type and executes callback that
updates the applicationId for those variants.

```
val releaseSelector = androidComponents.selector().withBuildType("release")
androidComponents.onVariants(releaseSelector) { variant ->
variant.applicationId.set("newApplicationId")
}
```

More examples are in [CustomPlugin.kt](build-logic/plugins/src/main/kotlin/CustomPlugin.kt).

## To Run
[`CustomPlugin`](build-logic/plugins/src/main/kotlin/CustomPlugin.kt) registers an instance of a task
`CheckApplicationIdTask` per variant, and this task verifies and outputs a modified application ID corresponding to the
variant name.

To run the examples, you can just do

```
./gradlew checkDebugApplicationId
```

and you will see the output:

```
Application ID for debug variant: debug.applicationId
```

Running with a different variant will produce the corresponding output.
30 changes: 30 additions & 0 deletions recipes/onVariants/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.on_variants")
}

android {
namespace = "com.example.android.recipes.onVariants"
compileSdk = $COMPILE_SDK
defaultConfig {
minSdk = $MINIMUM_SDK
targetSdk = $COMPILE_SDK
}
}
17 changes: 17 additions & 0 deletions recipes/onVariants/app/src/main/AndroidManifest.xml
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 recipes/onVariants/build-logic/gradle.properties
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 recipes/onVariants/build-logic/gradle/libs.versions.toml
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 recipes/onVariants/build-logic/plugins/build.gradle.kts
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("customPlugin") {
id = "android.recipes.on_variants"
implementationClass = "CustomPlugin"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.provider.Property
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.register
import java.lang.RuntimeException

/**
* This custom plugin shows examples for the onVariants API which includes updating the application ID.
*/
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 ->
variant.applicationId.set("${variant.name}.applicationId")
val taskName = "check${variant.name}ApplicationId"
project.tasks.register<CheckApplicationIdTask>(taskName) {
output.set(
project.layout.buildDirectory.dir("$taskName")
)
variantName.set(variant.name)
applicationId.set(variant.applicationId)
}
}
}
}
}

/**
* This task checks and outputs the name of the application ID.
*/
abstract class CheckApplicationIdTask : DefaultTask() {

// In order of the task to be up-to-date when the inputs have not changed,
// the task must declare an output, even if it's not used. Tasks with no
// output are always run regardless of whether the inputs changed
@get:OutputDirectory
abstract val output: DirectoryProperty

@get:Input
abstract val variantName: Property<String>

@get:Input
abstract val applicationId: Property<String>

@TaskAction
fun taskAction() {
val expectedApplicationId = "${variantName.get()}.applicationId"
if (applicationId.get() != expectedApplicationId) {
throw RuntimeException("Expected application ID to be '$expectedApplicationId'")
}
println("Application ID for ${variantName.get()} variant: ${applicationId.get()}")
}
}
33 changes: 33 additions & 0 deletions recipes/onVariants/build-logic/settings.gradle.kts
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")
20 changes: 20 additions & 0 deletions recipes/onVariants/build.gradle.kts
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
}
23 changes: 23 additions & 0 deletions recipes/onVariants/gradle.properties
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 recipes/onVariants/gradle/libs.versions.toml
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" }


22 changes: 22 additions & 0 deletions recipes/onVariants/recipe_metadata.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
title = "onVariants"

description ="""
This sample shows how to use onVariants in Variant API.
"""

[agpVersion]
min = "8.0.0"

# Relevant Gradle tasks to run per recipe
[gradleTasks]
tasks = [
":app:checkDebugApplicationId",
":app:checkReleaseApplicationId"
]

# All the relevant metadata fields to create an index based on language/API/etc'
[indexMetadata]
index = [
"AndroidComponentsExtension.onVariants",
"variant.applicationId"
]
Loading

0 comments on commit 60674d7

Please sign in to comment.