Variant

Added in 4.2.0

interface Variant : Component, HasAndroidResources

Known direct subclasses
ApplicationVariant

Model for application components that only contains build-time properties.

DynamicFeatureVariant

Model for dynamic feature components that only contains build-time properties.

LibraryVariant

Model for library components that only contains build-time properties.

TestVariant

Model for test components that only contains build-time properties.


Model for variant components that only contains build-time properties

Variant components are the main output of the plugin (e.g. APKs, AARs). They contain references to optional secondary components (tests, fixtures)

It is the object passed to the AndroidComponentsExtension.onVariants method, like this:

androidComponents {
onVariants(selector().all()) { variant: Variant ->
}
}

Note that depending on the actual implementation of AndroidComponentsExtension, the object received may be of a child type. For instance ApplicationAndroidComponentsExtension.onVariants will pass ApplicationVariant to the lambda.

See here for more information

Summary

Public functions

T?
<T : Any?> getExtension(type: Class<T>)

Returns an extension object registered via the VariantBuilder.registerExtension API or null if none were registered under the passed type.

Unit
@Incubating
missingDimensionStrategy(
    dimension: String,
    vararg requestedValues: String
)

Set up a new matching request for a given flavor dimension and value.

Public properties

MapProperty<StringBuildConfigField<Serializable>>

Variant's BuildConfigField which will be generated in the BuildConfig class.

List<Component>

List containing this variant and all of its nestedComponents

MapProperty<StringAny>

Additional per variant experimental properties.

ExternalNativeBuild?

Variant's cmake ExternalNativeBuild, initialized by merging the product flavor values or null if no cmake external build is configured for this variant.

MapProperty<StringString>

MapProperty of the variant's manifest placeholders.

Int?

Gets the maximum supported SDK Version for this variant.

Int?
AndroidVersion

Gets the minimum supported SDK Version for this variant.

AndroidVersion
List<Component>

List of the components nested in this variant, the returned list will contain:

Packaging

Variant's packagingOptions, initialized by the corresponding global DSL element.

ListProperty<RegularFile>

List of proguard configuration files for this variant.

AndroidVersion

Gets the target SDK Version for this variant.

UnitTest?

Variant's UnitTest, or null if the unit tests for this variant are disabled.

Inherited functions

From com.android.build.api.variant.Component
String
@Incubating
computeTaskName(action: String, subject: String)

Utility method to create unique task name for component.

Unit

This function is deprecated. Will be removed in v9.0, use the instrumentation block.

Unit
<ParamT : InstrumentationParameters> transformClassesWith(
    classVisitorFactoryImplClass: Class<AsmClassVisitorFactory<ParamT>>,
    scope: InstrumentationScope,
    instrumentationParamsConfig: (ParamT) -> Unit
)

This function is deprecated. Will be removed in v9.0, use the instrumentation block.

From com.android.build.api.variant.HasAndroidResources
ResValue.Key
makeResValueKey(type: String, name: String)

Make a ResValue.Key to interact with resValues's MapProperty

Inherited properties

From com.android.build.api.variant.Component
Configuration

Access to the variant's annotation processor Configuration; for example, the debugAnnotationProcessor Configuration for the debug variant.

Artifacts

Access to the variant's buildable artifacts for build customization.

FileCollection

Access to the variant's compile classpath.

Configuration

Access to the variant's compile Configuration; for example, the debugCompileClasspath Configuration for the debug variant.

Boolean

Whether the produced artifacts will be debuggable

Instrumentation

Access to the variant's instrumentation options.

JavaCompilation

Access to the variant's java compilation options.

LifecycleTasks

Provides access to the LifecycleTasks created for this component.

Provider<String>

The namespace of the generated R and BuildConfig classes.

Configuration

Access to the variant's runtime Configuration; for example, the debugRuntimeClasspath Configuration for the debug variant.

Sources

Access to variant's source files.

From com.android.build.api.variant.ComponentIdentity
String?

Build type name.

String?

The multi-flavor name of the variant.

String

Component's name.

List<Pair<StringString>>

List of flavor names.

From com.android.build.api.variant.HasAndroidResources
Property<Boolean>

Variant's is pseudo locales enabled, initialized by the corresponding DSL elements.

MapProperty<ResValue.KeyResValue>

Variant's ResValue which will be generated.

Public functions

getExtension

Added in 7.0.0
fun <T : Any?> getExtension(type: Class<T>): T?

Returns an extension object registered via the VariantBuilder.registerExtension API or null if none were registered under the passed type.

Returns
T?

the registered object or null.

missingDimensionStrategy

Added in 8.2.0
@Incubating
fun missingDimensionStrategy(
    dimension: String,
    vararg requestedValues: String
): Unit

Set up a new matching request for a given flavor dimension and value.

Parameters
dimension: String

the flavor dimension

vararg requestedValues: String

the flavor name(s)

See also
missingDimensionStrategy

To learn more, read d.android.com//build/build-variants.

Public properties

buildConfigFields

Added in 4.2.0
val buildConfigFieldsMapProperty<StringBuildConfigField<Serializable>>

Variant's BuildConfigField which will be generated in the BuildConfig class.

components

Added in 7.3.0
val componentsList<Component>

List containing this variant and all of its nestedComponents

Example:

androidComponents.onVariants(selector().withName("debug")) {
// components contains the debug variant along with its unitTests, androidTests, and
// testFixtures (if enabled).
components.forEach { component ->
component.runtimeConfiguration
.resolutionStrategy
.dependencySubstitution {
substitute(project(":foo")).using(project(":bar"))
}
}
}

experimentalProperties

Added in 7.0.0
val experimentalPropertiesMapProperty<StringAny>

Additional per variant experimental properties.

Initialized from com.android.build.api.dsl.CommonExtension.experimentalProperties

Values provided should not be based on a Task execution result, as most of these properties are read during configuration.

externalNativeBuild

Added in 7.0.0
val externalNativeBuildExternalNativeBuild?

Variant's cmake ExternalNativeBuild, initialized by merging the product flavor values or null if no cmake external build is configured for this variant.

manifestPlaceholders

Added in 4.2.0
val manifestPlaceholdersMapProperty<StringString>

MapProperty of the variant's manifest placeholders.

Placeholders are organized with a key and a value. The value is a String that will be used as is in the merged manifest.

Returns
MapProperty<StringString>

the MapProperty with keys as String

maxSdk

Added in 8.1.0
val maxSdkInt?

Gets the maximum supported SDK Version for this variant.

maxSdkVersion

Added in 7.0.0
Deprecated in 8.1.0
val maxSdkVersionInt?

minSdk

Added in 8.1.0
val minSdkAndroidVersion

Gets the minimum supported SDK Version for this variant.

minSdkVersion

Added in 7.0.0
Deprecated in 8.1.0
val minSdkVersionAndroidVersion

nestedComponents

Added in 7.1.0
val nestedComponentsList<Component>

List of the components nested in this variant, the returned list will contain:

Use this list to do operations on all nested components of this variant without having to manually check whether the variant has each component.

Example:

androidComponents.onVariants(selector().withName("debug")) {
// will return unitTests, androidTests, testFixtures for the debug variant (if enabled).
nestedComponents.forEach { component ->
component.instrumentation.transformClassesWith(NestedComponentsClassVisitorFactory::class.java,
InstrumentationScope.Project) {}
}
}

packaging

Added in 7.0.0
val packagingPackaging

Variant's packagingOptions, initialized by the corresponding global DSL element.

proguardFiles

Added in 7.0.0
val proguardFilesListProperty<RegularFile>

List of proguard configuration files for this variant. The list is initialized from the corresponding DSL element, and cannot be queried at configuration time. At configuration time, you can only add new elements to the list.

This list will be initialized from com.android.build.api.dsl.VariantDimension#proguardFile for non test related variants and from com.android.build.api.dsl.VariantDimension.testProguardFiles for test related variants.

targetSdkVersion

Added in 7.0.0
Deprecated in 7.4.1
val targetSdkVersionAndroidVersion

Gets the target SDK Version for this variant.

unitTest

Added in 7.0.0
Deprecated in 8.1.0
val unitTestUnitTest?

Variant's UnitTest, or null if the unit tests for this variant are disabled.