Android 12 Developer Preview is here! Try it out, and give us your feedback!

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

open Unit
androidTest(selector: VariantSelector = selector().all(), callback: Action<AndroidTest>)

open Unit
androidTest(selector: VariantSelector = selector().all(), callback: (AndroidTest) -> Unit)

abstract Unit
androidTests(selector: VariantSelector = selector().all(), callback: Action<AndroidTest>)

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

abstract Unit
androidTests(selector: VariantSelector = selector().all(), callback: (AndroidTest) -> Unit)

Action based version of the AndroidTest above.

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

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

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

Action based version of beforeVariants above.

abstract Unit
onVariants(selector: VariantSelector = selector().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 = selector().all(), callback: Action<VariantT>)

Action based version of onVariants above.

abstract Unit
registerExtension(dslExtension: DslExtension, configurator: (variantExtensionConfig: VariantExtensionConfig<VariantT>) -> VariantExtension)

Register an Android Gradle Plugin DSL extension.

abstract VariantSelector

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

open Unit
unitTest(selector: VariantSelector = selector().all(), callback: Action<UnitTest>)

open Unit
unitTest(selector: VariantSelector = selector().all(), callback: (UnitTest) -> Unit)

abstract Unit
unitTests(selector: VariantSelector = selector().all(), callback: Action<UnitTest>)

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

abstract Unit
unitTests(selector: VariantSelector = selector().all(), callback: (UnitTest) -> Unit)

Action based version of the unitTests above.

Properties

abstract SdkComponents

Provides access to underlying Android SDK and build-tools components like adb.

Public methods

androidTest

open fun androidTest(
    selector: VariantSelector = selector().all(),
    callback: Action<AndroidTest>
): Unit

Deprecated.

androidTest

open fun androidTest(
    selector: VariantSelector = selector().all(),
    callback: (AndroidTest) -> Unit
): Unit

Deprecated.

androidTests

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

Deprecated.

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

At this stage, access to the DSL objects is disallowed.

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 AndroidTest information which is used to configure org.gradle.api.Task inputs. Such information represented as org.gradle.api.provider.Property can still be modified ensuring that all org.gradle.api.Tasks created by the Android Gradle Plugin use the updated value.

androidTests

abstract fun androidTests(
    selector: VariantSelector = selector().all(),
    callback: (AndroidTest) -> Unit
): Unit

Deprecated.

Action based version of the AndroidTest above.

beforeVariants

abstract fun beforeVariants(
    selector: VariantSelector = selector().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 = selector().all() VariantSelector 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 = selector().all(),
    callback: Action<VariantBuilderT>
): Unit

Action based version of beforeVariants above.

onVariants

abstract fun onVariants(
    selector: VariantSelector = selector().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 represented as org.gradle.api.provider.Property can still be modified ensuring that all org.gradle.api.Tasks created by the Android Gradle Plugin use the updated value.

onVariants

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

Action based version of onVariants above.

registerExtension

abstract fun registerExtension(
    dslExtension: DslExtension,
    configurator: (variantExtensionConfig: VariantExtensionConfig<VariantT>) -> VariantExtension
): Unit

Register an Android Gradle Plugin DSL extension.

Please see Gradle documentation first at : https://docs.gradle.org/current/userguide/custom_plugins.html#sec:getting_input_from_the_build

A lambda must be provided to create and configure the variant scoped object that will be stored with the Android Gradle Plugin com.android.build.api.variant.Variant instance.

Variant Scoped objects should use org.gradle.api.provider.Property for its mutable state to allow for late binding. (see com.android.build.api.variant.Variant for examples).

Parameters
dslExtension: DslExtension the DSL extension configuration.
configurator: (variantExtensionConfig: VariantExtensionConfig<VariantT>) -> VariantExtension a lambda to create a variant scoped object. The lambda is provided with the VariantExtensionConfig that can be used to retrieve the VariantT instance as well as DSL extensions registered with DslExtension
Return
an sub type of VariantExtension instance that will be stored with the VariantT instance and can be retrieved by Variant.getExtension API.

selector

abstract fun selector(): VariantSelector

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

Return
VariantSelector to select the variants of interest.

unitTest

open fun unitTest(
    selector: VariantSelector = selector().all(),
    callback: Action<UnitTest>
): Unit

Deprecated.

unitTest

open fun unitTest(
    selector: VariantSelector = selector().all(),
    callback: (UnitTest) -> Unit
): Unit

Deprecated.

unitTests

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

Deprecated.

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

At this stage, access to the DSL objects is disallowed.

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 UnitTest information which is used to configure org.gradle.api.Task inputs. Such information represented as org.gradle.api.provider.Property can still be modified ensuring that all org.gradle.api.Tasks created by the Android Gradle Plugin use the updated value.

unitTests

abstract fun unitTests(
    selector: VariantSelector = selector().all(),
    callback: (UnitTest) -> Unit
): Unit

Deprecated.

Action based version of the unitTests above.

Properties

sdkComponents

abstract val sdkComponents: SdkComponents

Provides access to underlying Android SDK and build-tools components like adb.

Return
SdkComponents to access Android SDK used by Gradle.