AndroidComponentsExtension

public interface AndroidComponentsExtension<DslExtensionT extends CommonExtension<@NonNull ?, @NonNull ?, @NonNull ?, @NonNull ?, @NonNull ?>, VariantBuilderT extends VariantBuilder, VariantT extends Variant> extends DslLifecycle, AndroidComponents

Known direct subclasses
ApplicationAndroidComponentsExtension

Extension for the Android Application Gradle Plugin components.

DynamicFeatureAndroidComponentsExtension

Extension for the Android Dynamic Feature Gradle Plugin components.

LibraryAndroidComponentsExtension

Extension for the Android Library Gradle Plugin components.

TestAndroidComponentsExtension

Extension for the Android Test Gradle Plugin components.


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.

Parameters
<DslExtensionT extends CommonExtension<@NonNull ?, @NonNull ?, @NonNull ?, @NonNull ?, @NonNull ?>>

the type of the DSL to be used in DslLifecycle.finalizeDsl

<VariantBuilderT extends VariantBuilder>

the ComponentBuilder type produced by this variant.

<VariantT extends Variant>

the Variant type produced by this variant.

Summary

Public methods

abstract void
beforeVariants(
    @NonNull VariantSelector selector,
    @NonNull Action<@NonNull VariantBuilderT> callback
)

Action based version of beforeVariants above.

abstract void
beforeVariants(
    @NonNull VariantSelector selector,
    @NonNull Function1<@NonNull VariantBuilderT, Unit> callback
)

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

abstract void
finalizeDSl(@NonNull Action<@NonNull DslExtensionT> callback)

This method is deprecated. Replaced by finalizeDsl

abstract void
onVariants(
    @NonNull VariantSelector selector,
    @NonNull Action<@NonNull VariantT> callback
)

Action based version of onVariants above.

abstract void
onVariants(
    @NonNull VariantSelector selector,
    @NonNull Function1<@NonNull VariantT, Unit> callback
)

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 void
@Incubating
registerExtension(
    @NonNull DslExtension dslExtension,
    @NonNull Function1<@NonNull VariantExtensionConfig<@NonNull VariantT>, @NonNull VariantExtension> configurator
)

Register an Android Gradle Plugin DSL extension.

abstract void

Register a new source type to all source sets.

abstract @NonNull VariantSelector

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

Inherited methods

From com.android.build.api.variant.AndroidComponents
abstract @NonNull AndroidPluginVersion

The version of the Android Gradle Plugin currently in use.

abstract @NonNull SdkComponents

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

From com.android.build.api.variant.DslLifecycle
abstract void
finalizeDsl(@NonNull Action<@NonNull DslExtensionT> callback)

Action based version of finalizeDsl above.

abstract void
finalizeDsl(@NonNull Function1<@NonNull DslExtensionT, Unit> callback)

API to customize the DSL Objects programmatically after they have been evaluated from the build files and before used in the build process next steps like variant or tasks creation.

Public methods

beforeVariants

abstract void beforeVariants(
    @NonNull VariantSelector selector,
    @NonNull Action<@NonNull VariantBuilderT> callback
)

Action based version of beforeVariants above.

beforeVariants

abstract void beforeVariants(
    @NonNull VariantSelector selector,
    @NonNull Function1<@NonNull VariantBuilderT, Unit> callback
)

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 finalizeDsl 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
@NonNull VariantSelector selector

VariantSelector instance to select which instance of VariantBuilderT are of interest. By default, all instances are of interest.

@NonNull Function1<@NonNull VariantBuilderT, Unit> callback

lambda to be called with each instance of VariantBuilderT of interest.

finalizeDSl

abstract void finalizeDSl(@NonNull Action<@NonNull DslExtensionT> callback)

Action based version of finalizeDsl above.

onVariants

abstract void onVariants(
    @NonNull VariantSelector selector,
    @NonNull Action<@NonNull VariantT> callback
)

Action based version of onVariants above.

onVariants

abstract void onVariants(
    @NonNull VariantSelector selector,
    @NonNull Function1<@NonNull VariantT, Unit> callback
)

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.

registerExtension

@Incubating
abstract void registerExtension(
    @NonNull DslExtension dslExtension,
    @NonNull Function1<@NonNull VariantExtensionConfig<@NonNull VariantT>, @NonNull VariantExtension> configurator
)

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 alongside the Android Gradle Plugin's 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).

The DslExtension.Builder allow you to choose if you want to extend Project, BuiltType or ProductFlavor. You can extend just one or up to all of them.

A BuildType extension of type BuildTypeDslExtension will allow users to have the following declarations in their build files:

android {
buildTypes {
debug {
extensions.configure<BuildTypeDslExtension> {
buildTypeSettingOne = "build_type_debug"
}
}
}
}

A Product flavor extension of type ProductFlavorDslExtension will allow users to specify the following declarations in their build files:

android {
flavorDimensions += "version"
productFlavors {
create("demo") {
dimension = "version"
extensions.configure<ProductFlavorDslExtension> {
productFlavorSettingOne = "product_flavor_demo"
productFlavorSettingTwo = 99
}
}
}
}
Parameters
@NonNull DslExtension dslExtension

the DSL extension configuration.

@NonNull Function1<@NonNull VariantExtensionConfig<@NonNull VariantT>, @NonNull VariantExtension> configurator

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

Returns
void

an instance of a sub type of VariantExtension that will be stored with the VariantT instance and can be retrieved by Variant.getExtension API.

registerSourceType

@Incubating
abstract void registerSourceType(@NonNull String name)

Register a new source type to all source sets.

The name of the source type will be used to create expected directories names in the various source sets. For instance, src/main/ or src/debug/.

There is no notion of priorities between the build-type, flavor specific directories, all the expected directories will be interpreted as a flat namespace.

Therefore, any org.gradle.api.Task that needs access to the entire list of source folders can just use the Sources.extras's Flat.all method for that source type.

However, If you need to have overriding priorities between the expected directories and therefore require a merging activity, you can still use this API but you will need to create a merging task that will have all sources in input and produce a single output folder for the merged sources.

Parameters
@NonNull String name

the name of the source type.

selector

abstract @NonNull VariantSelector selector()

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

Returns
@NonNull VariantSelector

VariantSelector to select the variants of interest.