VariantBuilder

Added in 4.2.0

interface VariantBuilder : ComponentBuilder

Known direct subclasses
ApplicationVariantBuilder

Model for application components that only contains configuration-time properties that impacts the build flow.

DynamicFeatureVariantBuilder

Model for dynamic feature components that only contains configuration-time properties that impacts the build flow.

LibraryVariantBuilder

Model for library components that only contains configuration-time properties that impacts the build flow.

TestVariantBuilder

Model for test components that only contains configuration-time properties that impacts the build flow.


Model for variant components that only contains configuration-time properties that impacts the build flow.

Variant components are the main output of the plugin (e.g. APKs, AARs). They contain references to optional secondary components (tests, fixtures). Their presence can be controlled in select subtypes of VariantBuilder.

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

androidComponents {
beforeVariants(selector().all()) { variant: VariantBuilder ->
}
}

Note that depending on the actual implementation of AndroidComponentsExtension, the object received may be a subtype. For instance ApplicationAndroidComponentsExtension.beforeVariants will pass ApplicationVariantBuilder to the lambda.

See here for more information

Summary

Public functions

Unit
<T : Any> registerExtension(type: Class<T>, instance: T)

Registers an extension object to the variant object.

Public properties

Boolean

This property is deprecated. Will be removed in AGP 9.0 - Use (variantBuilder as HasUnitTestBuilder).enableUnitTest

Int?

Sets the maximum supported SDK Version for this variant.

Int?

Sets the minimum supported SDK Version for this variant.

String?

Sets the minimum supported SDK Version for this variant as a Preview codename.

Int

Specifies the bytecode version to be generated.

Int?

This property is deprecated. Will be removed in v9.0 - Use (variantBuilder as GeneratesApkBuilder).targetSdk

String?

This property is deprecated. Will be removed in v9.0 - Use (variantBuilder as GeneratesApkBuilder).targetSdkPreview

Boolean

This property is deprecated. Will be removed in AGP 9.0 - Use (variantBuilder as HasUnitTestBuilder).enableUnitTest

Inherited properties

From com.android.build.api.variant.ComponentBuilder
Boolean

Set to true if the variant is active and should be configured, false otherwise.

Boolean

This property is deprecated. Will be removed in 9.0

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.

Public functions

registerExtension

Added in 7.0.0
fun <T : Any> registerExtension(type: Class<T>, instance: T): Unit

Registers an extension object to the variant object. Extension objects can be looked up during the AndroidComponentsExtension.onVariants callbacks by using the Variant.getExtension API.

This is very useful for third party plugins that want to attach some variant specific configuration object to the Android Gradle Plugin variant object and make it available to other plugins.

Parameters
type: Class<T>

the registered object type (can be a supertype of instance), this is the type that must be passed to the Variant.getExtension API.

instance: T

the object to associate to the AGP Variant object.

Public properties

enableUnitTest

Added in 7.0.0
Deprecated in 8.1.0
var enableUnitTestBoolean

Set to true if the variant's has any unit tests, false otherwise. Value is Boolean#True by default.

maxSdk

Added in 7.0.0
var maxSdkInt?

Sets the maximum supported SDK Version for this variant.

It is not safe to read this value. Use Variant.maxSdk instead.

minSdk

Added in 7.0.0
var minSdkInt?

Sets the minimum supported SDK Version for this variant. Setting this it will override previous calls of minSdk and minSdkPreview setters. Only one of minSdk and minSdkPreview should be set.

It is not safe to read this value. Use Variant.minSdk instead.

Returns
Int?

the minimum supported SDK Version or null if minSdkPreview was used to set it.

minSdkPreview

Added in 7.0.0
var minSdkPreviewString?

Sets the minimum supported SDK Version for this variant as a Preview codename. Setting this it will override previous calls of minSdk and minSdkPreview setters. Only one of minSdk and minSdkPreview should be set.

It is not safe to read this value. Use Variant.minSdk instead.

Returns
String?

the minimum supported SDK Version or null if minSdk was used to set it.

renderscriptTargetApi

Added in 4.2.0
var renderscriptTargetApiInt

Specifies the bytecode version to be generated. We recommend you set this value to the lowest API level able to provide all the functionality you are using. -1 means unspecified.

It is not safe to read this value. Use GeneratesApk.renderscript instead

Returns
Int

the renderscript target api or -1 if not specified.

targetSdk

Added in 7.0.0
Deprecated in 7.4.0
var targetSdkInt?

Sets the target SDK Version for this variant as a Preview codename. Setting this it will override previous calls of targetSdk and targetSdkPreview setters. Only one of targetSdk and targetSdkPreview should be set.

targetSdk is now managed by GeneratesApkBuilder instead of VariantBuilder.

Returns
Int?

the target SDK Version or null if targetSdkPreview was used to set it.

targetSdkPreview

Added in 7.0.0
Deprecated in 7.4.0
var targetSdkPreviewString?

Sets the target SDK Version for this variant as a Preview codename. Setting this it will override previous calls of targetSdk and targetSdkPreview setters. Only one of targetSdk and targetSdkPreview should be set.

targetSdkPreview is now managed by GeneratesApkBuilder instead of VariantBuilder.

Returns
String?

the target supported SDK Version or null if targetSdkPreview was used to set it.

unitTestEnabled

Added in 7.0.0
Deprecated in 7.0.0
var unitTestEnabledBoolean

Set to true if the variant's has any unit tests, false otherwise. Value is Boolean#True by default.