Google is committed to advancing racial equity for Black communities. See how.

AndroidComponentsExtension

@Incubating interface AndroidComponentsExtension<VariantBuilderT : VariantBuilder, VariantT : Variant>
com.android.build.api.extension.AndroidComponentsExtension

Generic extension for Android Gradle Plugin related components.

Each component has a type, like application or library and will have a dedicated extension with methods that are related to the particular component type.

Summary

Public methods

abstract Unit
beforeVariants(selector: VariantSelector<VariantBuilderT> = selector<VariantBuilderT>().all(), callback: (VariantBuilderT) -> Unit)

Method to register a callback to be called with VariantBuilderT instances that satisfies the selector.

abstract Unit
beforeVariants(selector: VariantSelector<VariantBuilderT> = selector<VariantBuilderT>().all(), callback: Action<VariantBuilderT>)

Action based version of beforeVariants above.

abstract Unit
onVariants(selector: VariantSelector<VariantT> = selector<VariantT>().all(), callback: (VariantT) -> Unit)

Allow for registration of a callback to be called with variant instances of type VariantT once the list of com.android.build.api.artifact.Artifact has been determined.

abstract Unit
onVariants(selector: VariantSelector<VariantT> = selector<VariantT>().all(), callback: Action<VariantT>)

Action based version of onVariants above.

abstract GenericVariantSelector<T>

Creates a GenericVariantSelector instance that can be configured to reduce the set of ComponentBuilder instances participating in the beforeVariants and onVariants callback invocation.

Public methods

beforeVariants

abstract fun beforeVariants(
    selector: VariantSelector<VariantBuilderT> = selector<VariantBuilderT>().all(),
    callback: (VariantBuilderT) -> Unit
): Unit

Method to register a callback to be called with VariantBuilderT instances that satisfies the selector. The callback will be called as soon as the VariantBuilderT instance has been created but before any com.android.build.api.artifact.Artifact has been determined, therefore the build flow can still be changed when the callback is invoked.

At this stage, access to the DSL objects is disallowed, use afterDsl method to programmatically access the DSL objects before the VariantBuilderT object is built.

Example without selection:

androidComponents {
    beforeVariants {
        println("Called with variant : ${'$'}name")
    }
}

Example with selection:

androidComponents {
    val debug = selector().withBuildType("debug")
    beforeVariants(debug) {
        println("Called with variant : ${'$'}name")
    }
}
Parameters
selector: VariantSelector<VariantBuilderT> = selector<VariantBuilderT>().all() GenericVariantSelector instance to select which instance of VariantBuilderT are of interest. By default, all instances are of interest.
callback: (VariantBuilderT) -> Unit lambda to be called with each instance of VariantBuilderT of interest.

beforeVariants

abstract fun beforeVariants(
    selector: VariantSelector<VariantBuilderT> = selector<VariantBuilderT>().all(),
    callback: Action<VariantBuilderT>
): Unit

Action based version of beforeVariants above.

onVariants

abstract fun onVariants(
    selector: VariantSelector<VariantT> = selector<VariantT>().all(),
    callback: (VariantT) -> Unit
): Unit

Allow for registration of a callback to be called with variant instances of type VariantT once the list of com.android.build.api.artifact.Artifact has been determined.

At this stage, access to the DSL objects is disallowed and access to the VariantBuilderT instance is limited to read-only access.

Because the list of artifacts (including private ones) is final, one cannot change the build flow anymore as org.gradle.api.Tasks are now expecting those artifacts as inputs. However users can modify such artifacts by replacing or transforming them, see com.android.build.api.artifact.Artifacts for details.

Code executing in the callback also has access to the VariantT information which is used to configure org.gradle.api.Task inputs (for example, the buildConfigFields). Such information is represented as org.gradle.api.provider.Property and can still be modified and have internal org.gradle.api.Task to the Android Gradle Plugin see and use the updated value.

onVariants

abstract fun onVariants(
    selector: VariantSelector<VariantT> = selector<VariantT>().all(),
    callback: Action<VariantT>
): Unit

Action based version of onVariants above.

selector

abstract fun <T : ComponentIdentity> selector(): GenericVariantSelector<T>

Creates a GenericVariantSelector instance that can be configured to reduce the set of ComponentBuilder instances participating in the beforeVariants and onVariants callback invocation.

Return
GenericVariantSelector to select the variants of interest.