ProductFlavor

Encapsulates all product flavors properties for this project.

Product flavors represent different versions of your project that you expect to co-exist on a single device, the Google Play store, or repository. For example, you can configure 'demo' and 'full' product flavors for your app, and each of those flavors can specify different features, device requirements, resources, and application ID's--while sharing common source code and resources. So, product flavors allow you to output different versions of your project by simply changing only the components and settings that are different between them.

Configuring product flavors is similar to configuring build types: add them to the productFlavors block of your module's build.gradle file and configure the settings you want. Product flavors support the same properties as the BaseExtension.getDefaultConfig() block—this is because defaultConfig defines a ProductFlavor object that the plugin uses as the base configuration for all other flavors. Each flavor you configure can then override any of the default values in defaultConfig, such as the applicationId.

When using Android plugin 3.0.0 and higher, each flavor must belong to a dimension.

When you configure product flavors, the Android plugin automatically combines them with your BuildType configurations to create build variants. If the plugin creates certain build variants that you don't want, you can filter variants using android.variantFilter.

Properties

PropertyDescription
applicationId

The application ID.

applicationIdSuffix

Application id suffix. It is appended to the "base" application id when calculating the final application id for a variant.

consumerProguardFiles

ProGuard rule files to be included in the published AAR.

dimension

Specifies the flavor dimension that this product flavor belongs to.

externalNativeBuild

Encapsulates per-variant CMake and ndk-build configurations for your external native build.

generatedDensities
Deprecated

Deprecated equivalent of vectorDrawablesOptions.generatedDensities.

jackOptions
Deprecated

The Jack toolchain is deprecated.

javaCompileOptions

Options for configuration Java compilation.

manifestPlaceholders

The manifest placeholders.

matchingFallbacks

Specifies a sorted list of product flavors that the plugin should try to use when a direct variant match with a local module dependency is not possible.

multiDexEnabled

Whether Multi-Dex is enabled for this variant.

multiDexKeepFile

Text file that specifies additional classes that will be compiled into the main dex file.

multiDexKeepProguard

Text file with additional ProGuard rules to be used to determine which classes are compiled into the main dex file.

ndk

Encapsulates per-variant configurations for the NDK, such as ABI filters.

proguardFiles

Specifies the ProGuard configuration files that the plugin should use.

signingConfig

Signing config used by this product flavor.

testApplicationId

Test application ID.

testFunctionalTest

See instrumentation.

testHandleProfiling

See instrumentation.

testInstrumentationRunner

Test instrumentation runner class name.

testInstrumentationRunnerArguments

Test instrumentation runner custom arguments.

useJack
Deprecated

The Jack toolchain is deprecated.

vectorDrawables

Options to configure the build-time support for vector drawables.

versionCode

Version code.

versionName

Version name.

versionNameSuffix

Version name suffix. It is appended to the "base" version name when calculating the final version name for a variant.

wearAppUnbundled

Returns whether to enable unbundling mode for embedded wear app. If true, this enables the app to transition from an embedded wear app to one distributed by the play store directly.

Methods

MethodDescription
buildConfigField(type, name, value)

Adds a new field to the generated BuildConfig class.

consumerProguardFile(proguardFile)

Adds a proguard rule file to be included in the published AAR.

consumerProguardFiles(proguardFiles)

Adds proguard rule files to be included in the published AAR.

maxSdkVersion(maxSdkVersion)

Sets the maximum SDK version to the given value.

minSdkVersion(minSdkVersion)

Sets minimum SDK version.

minSdkVersion(minSdkVersion)

Sets minimum SDK version.

missingDimensionStrategy(dimension, requestedValue)

Specifies a flavor that the plugin should try to use from a given dimension in a dependency.

missingDimensionStrategy(dimension, requestedValues)

Specifies a sorted list of flavors that the plugin should try to use from a given dimension in a dependency.

missingDimensionStrategy(dimension, requestedValues)

Specifies a sorted list of flavors that the plugin should try to use from a given dimension in a dependency.

proguardFile(proguardFile)

Specifies a ProGuard configuration file that the plugin should use.

proguardFiles(files)

Specifies ProGuard configuration files that the plugin should use.

resConfig(config)

Adds a resource configuration filter.

resConfigs(config)

Adds several resource configuration filters.

resConfigs(config)

Adds several resource configuration filters.

resValue(type, name, value)

Adds a new generated resource.

setConsumerProguardFiles(proguardFileIterable)

Specifies a proguard rule file to be included in the published AAR.

setProguardFiles(proguardFileIterable)

Sets the ProGuard configuration files.

setTestProguardFiles(files)

Specifies proguard rule files to be used when processing test code.

targetSdkVersion(targetSdkVersion)

Sets the target SDK version to the given value.

targetSdkVersion(targetSdkVersion)

Sets the target SDK version to the given value.

testInstrumentationRunnerArgument(key, value)

Adds a custom argument to the test instrumentation runner, e.g:

testInstrumentationRunnerArguments(args)

Adds custom arguments to the test instrumentation runner, e.g:

testProguardFile(proguardFile)

Adds a proguard rule file to be used when processing test code.

testProguardFiles(proguardFiles)

Adds proguard rule files to be used when processing test code.

Script blocks

No script blocks

Property details

String applicationId

The application ID.

See Set the Application ID

String applicationIdSuffix

Application id suffix. It is appended to the "base" application id when calculating the final application id for a variant.

In case there are product flavor dimensions specified, the final application id suffix will contain the suffix from the default product flavor, followed by the suffix from product flavor of the first dimension, second dimension and so on. All of these will have a dot in between e.g. "defaultSuffix.dimension1Suffix.dimensions2Suffix".

List<File> consumerProguardFiles

ProGuard rule files to be included in the published AAR.

These proguard rule files will then be used by any application project that consumes the AAR (if ProGuard is enabled).

This allows AAR to specify shrinking or obfuscation exclude rules.

This is only valid for Library project. This is ignored in Application project.

String dimension

Specifies the flavor dimension that this product flavor belongs to.

When configuring product flavors with Android plugin 3.0.0 and higher, you must specify at least one flavor dimension, using the flavorDimensions property, and then assign each flavor to a dimension. Otherwise, you will get the following build error:

Error:All flavors must now belong to a named flavor dimension.
The flavor 'flavor_name' is not assigned to a flavor dimension.

By default, when you specify only one dimension, all flavors you configure automatically belong to that dimension. If you specify more than one dimension, you need to manually assign each flavor to a dimension, as shown in the sample below:

android {
    ...
    // Specifies the flavor dimensions you want to use. The order in which you
    // list each dimension determines its priority, from highest to lowest,
    // when Gradle merges variant sources and configurations. You must assign
    // each product flavor you configure to one of the flavor dimensions.
    flavorDimensions 'api', 'version'

    productFlavors {
      demo {
        // Assigns this product flavor to the 'version' flavor dimension.
        dimension 'version'
        ...
    }

      full {
        dimension 'version'
        ...
      }

      minApi24 {
        // Assigns this flavor to the 'api' dimension.
        dimension 'api'
        minSdkVersion '24'
        versionNameSuffix "-minApi24"
        ...
      }

      minApi21 {
        dimension "api"
        minSdkVersion '21'
        versionNameSuffix "-minApi21"
        ...
      }
   }
}

To learn more about configuring flavor dimensions, read Combine multiple flavors.

ExternalNativeBuildOptions externalNativeBuild

Encapsulates per-variant CMake and ndk-build configurations for your external native build.

To learn more, see Add C and C++ Code to Your Project.

Set<String> generatedDensities

Note: This property is deprecated and will be removed in a future version of the plugin.

Deprecated equivalent of vectorDrawablesOptions.generatedDensities.

JackOptions jackOptions

Note: This property is deprecated and will be removed in a future version of the plugin.

The Jack toolchain is deprecated.

If you want to use Java 8 language features, use the improved support included in the default toolchain. To learn more, read Use Java 8 language features.

JavaCompileOptions javaCompileOptions

Options for configuration Java compilation.

Map<String, Object> manifestPlaceholders

The manifest placeholders.

See Inject Build Variables into the Manifest.

List<String> matchingFallbacks

Specifies a sorted list of product flavors that the plugin should try to use when a direct variant match with a local module dependency is not possible.

Android plugin 3.0.0 and higher try to match each variant of your module with the same one from its dependencies. For example, when you build a "freeDebug" version of your app, the plugin tries to match it with "freeDebug" versions of the local library modules the app depends on.

However, there may be situations in which, for a given flavor dimension that exists in both the app and its library dependencies, your app includes flavors that a dependency does not. For example, consider if both your app and its library dependencies include a "tier" flavor dimension. However, the "tier" dimension in the app includes "free" and "paid" flavors, but one of its dependencies includes only "demo" and "paid" flavors for the same dimension. When the plugin tries to build the "free" version of your app, it won't know which version of the dependency to use, and you'll see an error message similar to the following:

Error:Failed to resolve: Could not resolve project :mylibrary.
Required by:
    project :app

In this situation, you should use matchingFallbacks to specify alternative matches for the app's "free" product flavor, as shown below:

// In the app's build.gradle file.
android {
    flavorDimensions 'tier'
    productFlavors {
        paid {
            dimension 'tier'
            // Because the dependency already includes a "paid" flavor in its
            // "tier" dimension, you don't need to provide a list of fallbacks
            // for the "paid" flavor.
        }
        free {
            dimension 'tier'
            // Specifies a sorted list of fallback flavors that the plugin
            // should try to use when a dependency's matching dimension does
            // not include a "free" flavor. You may specify as many
            // fallbacks as you like, and the plugin selects the first flavor
            // that's available in the dependency's "tier" dimension.
            matchingFallbacks = ['demo', 'trial']
        }
    }
}

Note that, for a given flavor dimension that exists in both the app and its library dependencies, there is no issue when a library includes a product flavor that your app does not. That's because the plugin simply never requests that flavor from the dependency.

If instead you are trying to resolve an issue in which a library dependency includes a flavor dimension that your app does not, use missingDimensionStrategy.

Boolean multiDexEnabled

Whether Multi-Dex is enabled for this variant.

File multiDexKeepFile

Text file that specifies additional classes that will be compiled into the main dex file.

Classes specified in the file are appended to the main dex classes computed using aapt.

If set, the file should contain one class per line, in the following format: com/example/MyClass.class

File multiDexKeepProguard

Text file with additional ProGuard rules to be used to determine which classes are compiled into the main dex file.

If set, rules from this file are used in combination with the default rules used by the build system.

Encapsulates per-variant configurations for the NDK, such as ABI filters.

List<File> proguardFiles

Specifies the ProGuard configuration files that the plugin should use.

There are two ProGuard rules files that ship with the Android plugin and are used by default:

  • proguard-android.txt
  • proguard-android-optimize.txt

proguard-android-optimize.txt is identical to proguard-android.txt , exccept with optimizations enabled. You can use getDefaultProguardFile(String filename) to return the full path of the files.

SigningConfig signingConfig

Signing config used by this product flavor.

String testApplicationId

Test application ID.

See Set the Application ID

Boolean testFunctionalTest

See instrumentation.

Boolean testHandleProfiling

See instrumentation.

String testInstrumentationRunner

Test instrumentation runner class name.

This is a fully qualified class name of the runner, e.g. android.test.InstrumentationTestRunner

See instrumentation.

Map<String, String> testInstrumentationRunnerArguments

Test instrumentation runner custom arguments.

e.g. [key: "value"] will give adb shell am instrument -w -e key value com.example...".

See instrumentation.

Test runner arguments can also be specified from the command line:

./gradlew connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.size=medium
./gradlew connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.foo=bar

Boolean useJack

Note: This property is deprecated and will be removed in a future version of the plugin.

The Jack toolchain is deprecated.

If you want to use Java 8 language features, use the improved support included in the default toolchain. To learn more, read Use Java 8 language features.

VectorDrawablesOptions vectorDrawables

Options to configure the build-time support for vector drawables.

Integer versionCode

Version code.

See Versioning Your Application

String versionName

Version name.

See Versioning Your Application

String versionNameSuffix

Version name suffix. It is appended to the "base" version name when calculating the final version name for a variant.

In case there are product flavor dimensions specified, the final version name suffix will contain the suffix from the default product flavor, followed by the suffix from product flavor of the first dimension, second dimension and so on.

Boolean wearAppUnbundled

Returns whether to enable unbundling mode for embedded wear app. If true, this enables the app to transition from an embedded wear app to one distributed by the play store directly.

Method details

void buildConfigField(String type, String name, String value)

Adds a new field to the generated BuildConfig class.

The field is generated as: <type> <name> = <value>;

This means each of these must have valid Java content. If the type is a String, then the value should include quotes.

void consumerProguardFile(Object proguardFile)

Adds a proguard rule file to be included in the published AAR.

This proguard rule file will then be used by any application project that consume the AAR (if proguard is enabled).

This allows AAR to specify shrinking or obfuscation exclude rules.

This is only valid for Library project. This is ignored in Application project.

void consumerProguardFiles(Object... proguardFiles)

Adds proguard rule files to be included in the published AAR.

This proguard rule file will then be used by any application project that consume the AAR (if proguard is enabled).

This allows AAR to specify shrinking or obfuscation exclude rules.

This is only valid for Library project. This is ignored in Application project.

void maxSdkVersion(int maxSdkVersion)

Sets the maximum SDK version to the given value.

See uses-sdk element documentation.

void minSdkVersion(int minSdkVersion)

Sets minimum SDK version.

See uses-sdk element documentation.

void minSdkVersion(String minSdkVersion)

Sets minimum SDK version.

See uses-sdk element documentation.

void missingDimensionStrategy(String dimension, String requestedValue)

Specifies a flavor that the plugin should try to use from a given dimension in a dependency.

Android plugin 3.0.0 and higher try to match each variant of your module with the same one from its dependencies. For example, consider if both your app and its dependencies include a "tier" flavor dimension, with flavors "free" and "paid". When you build a "freeDebug" version of your app, the plugin tries to match it with "freeDebug" versions of the local library modules the app depends on.

However, there may be situations in which a library dependency includes a flavor dimension that your app does not. For example, consider if a library dependency includes flavors for a "minApi" dimension, but your app includes flavors for only the "tier" dimension. So, when you want to build the "freeDebug" version of your app, the plugin doesn't know whether to use the "minApi23Debug" or "minApi18Debug" version of the dependency, and you'll see an error message similar to the following:

Error:Failed to resolve: Could not resolve project :mylibrary.
Required by:
    project :app

In this type of situation, use missingDimensionStrategy in the defaultConfig block to specify the default flavor the plugin should select from each missing dimension, as shown in the sample below. You can also override your selection in the productFlavors block, so each flavor can specify a different matching strategy for a missing dimension. (Tip: you can also use this property if you simply want to change the matching strategy for a dimension that exists in both the app and its dependencies.)

// In the app's build.gradle file.
android {
    defaultConfig{
    // Specifies a flavor that the plugin should try to use from
    // a given dimension. The following tells the plugin that, when encountering
    // a dependency that includes a "minApi" dimension, it should select the
    // "minApi18" flavor.
    missingDimensionStrategy 'minApi', 'minApi18'
    // You should specify a missingDimensionStrategy property for each
    // dimension that exists in a local dependency but not in your app.
    missingDimensionStrategy 'abi', 'x86'
    }
    flavorDimensions 'tier'
    productFlavors {
        free {
            dimension 'tier'
            // You can override the default selection at the product flavor
            // level by configuring another missingDimensionStrategy property
            // for the "minApi" dimension.
            missingDimensionStrategy 'minApi', 'minApi23'
        }
        paid {}
    }
}

void missingDimensionStrategy(String dimension, String... requestedValues)

Specifies a sorted list of flavors that the plugin should try to use from a given dimension in a dependency.

Android plugin 3.0.0 and higher try to match each variant of your module with the same one from its dependencies. For example, consider if both your app and its dependencies include a "tier" flavor dimension, with flavors "free" and "paid". When you build a "freeDebug" version of your app, the plugin tries to match it with "freeDebug" versions of the local library modules the app depends on.

However, there may be situations in which a library dependency includes a flavor dimension that your app does not. For example, consider if a library dependency includes flavors for a "minApi" dimension, but your app includes flavors for only the "tier" dimension. So, when you want to build the "freeDebug" version of your app, the plugin doesn't know whether to use the "minApi23Debug" or "minApi18Debug" version of the dependency, and you'll see an error message similar to the following:

Error:Failed to resolve: Could not resolve project :mylibrary.
Required by:
    project :app

In this type of situation, use missingDimensionStrategy in the defaultConfig block to specify the default flavor the plugin should select from each missing dimension, as shown in the sample below. You can also override your selection in the productFlavors block, so each flavor can specify a different matching strategy for a missing dimension. (Tip: you can also use this property if you simply want to change the matching strategy for a dimension that exists in both the app and its dependencies.)

// In the app's build.gradle file.
android {
    defaultConfig{
    // Specifies a sorted list of flavors that the plugin should try to use from
    // a given dimension. The following tells the plugin that, when encountering
    // a dependency that includes a "minApi" dimension, it should select the
    // "minApi18" flavor. You can include additional flavor names to provide a
    // sorted list of fallbacks for the dimension.
    missingDimensionStrategy 'minApi', 'minApi18', 'minApi23'
    // You should specify a missingDimensionStrategy property for each
    // dimension that exists in a local dependency but not in your app.
    missingDimensionStrategy 'abi', 'x86', 'arm64'
    }
    flavorDimensions 'tier'
    productFlavors {
        free {
            dimension 'tier'
            // You can override the default selection at the product flavor
            // level by configuring another missingDimensionStrategy property
            // for the "minApi" dimension.
            missingDimensionStrategy 'minApi', 'minApi23', 'minApi18'
        }
        paid {}
    }
}

void missingDimensionStrategy(String dimension, List<String> requestedValues)

Specifies a sorted list of flavors that the plugin should try to use from a given dimension in a dependency.

Android plugin 3.0.0 and higher try to match each variant of your module with the same one from its dependencies. For example, consider if both your app and its dependencies include a "tier" flavor dimension, with flavors "free" and "paid". When you build a "freeDebug" version of your app, the plugin tries to match it with "freeDebug" versions of the local library modules the app depends on.

However, there may be situations in which a library dependency includes a flavor dimension that your app does not. For example, consider if a library dependency includes flavors for a "minApi" dimension, but your app includes flavors for only the "tier" dimension. So, when you want to build the "freeDebug" version of your app, the plugin doesn't know whether to use the "minApi23Debug" or "minApi18Debug" version of the dependency, and you'll see an error message similar to the following:

Error:Failed to resolve: Could not resolve project :mylibrary.
Required by:
    project :app

In this type of situation, use missingDimensionStrategy in the defaultConfig block to specify the default flavor the plugin should select from each missing dimension, as shown in the sample below. You can also override your selection in the productFlavors block, so each flavor can specify a different matching strategy for a missing dimension. (Tip: you can also use this property if you simply want to change the matching strategy for a dimension that exists in both the app and its dependencies.)

// In the app's build.gradle file.
android {
    defaultConfig{
    // Specifies a sorted list of flavors that the plugin should try to use from
    // a given dimension. The following tells the plugin that, when encountering
    // a dependency that includes a "minApi" dimension, it should select the
    // "minApi18" flavor. You can include additional flavor names to provide a
    // sorted list of fallbacks for the dimension.
    missingDimensionStrategy 'minApi', 'minApi18', 'minApi23'
    // You should specify a missingDimensionStrategy property for each
    // dimension that exists in a local dependency but not in your app.
    missingDimensionStrategy 'abi', 'x86', 'arm64'
    }
    flavorDimensions 'tier'
    productFlavors {
        free {
            dimension 'tier'
            // You can override the default selection at the product flavor
            // level by configuring another missingDimensionStrategy property
            // for the "minApi" dimension.
            missingDimensionStrategy 'minApi', 'minApi23', 'minApi18'
        }
        paid {}
    }
}

void proguardFile(Object proguardFile)

Specifies a ProGuard configuration file that the plugin should use.

There are two ProGuard rules files that ship with the Android plugin and are used by default:

  • proguard-android.txt
  • proguard-android-optimize.txt

proguard-android-optimize.txt is identical to proguard-android.txt , exccept with optimizations enabled. You can use getDefaultProguardFile(String filename) to return the full path of each file.

void proguardFiles(Object... files)

Specifies ProGuard configuration files that the plugin should use.

There are two ProGuard rules files that ship with the Android plugin and are used by default:

  • proguard-android.txt
  • proguard-android-optimize.txt

proguard-android-optimize.txt is identical to proguard-android.txt , exccept with optimizations enabled. You can use getDefaultProguardFile(String filename) to return the full path of each file.

void resConfig(String config)

Adds a resource configuration filter.

If a qualifier value is passed, then all other resources using a qualifier of the same type but of different value will be ignored from the final packaging of the APK.

For instance, specifying 'hdpi', will ignore all resources using mdpi, xhdpi, etc...

To package only the localization languages your app includes as string resources, specify 'auto'. For example, if your app includes string resources for 'values-en' and 'values-fr', and its dependencies provide 'values-en' and 'values-ja', Gradle packages only the 'values-en' and 'values-fr' resources from the app and its dependencies. Gradle does not package 'values-ja' resources in the final APK.

void resConfigs(String... config)

Adds several resource configuration filters.

If a qualifier value is passed, then all other resources using a qualifier of the same type but of different value will be ignored from the final packaging of the APK.

For instance, specifying 'hdpi', will ignore all resources using mdpi, xhdpi, etc...

To package only the localization languages your app includes as string resources, specify 'auto'. For example, if your app includes string resources for 'values-en' and 'values-fr', and its dependencies provide 'values-en' and 'values-ja', Gradle packages only the 'values-en' and 'values-fr' resources from the app and its dependencies. Gradle does not package 'values-ja' resources in the final APK.

void resConfigs(Collection<String> config)

Adds several resource configuration filters.

If a qualifier value is passed, then all other resources using a qualifier of the same type but of different value will be ignored from the final packaging of the APK.

For instance, specifying 'hdpi', will ignore all resources using mdpi, xhdpi, etc...

To package only the localization languages your app includes as string resources, specify 'auto'. For example, if your app includes string resources for 'values-en' and 'values-fr', and its dependencies provide 'values-en' and 'values-ja', Gradle packages only the 'values-en' and 'values-fr' resources from the app and its dependencies. Gradle does not package 'values-ja' resources in the final APK.

void resValue(String type, String name, String value)

Adds a new generated resource.

This is equivalent to specifying a resource in res/values.

See Resource Types.

void setConsumerProguardFiles(Iterable<?> proguardFileIterable)

Specifies a proguard rule file to be included in the published AAR.

This proguard rule file will then be used by any application project that consume the AAR (if proguard is enabled).

This allows AAR to specify shrinking or obfuscation exclude rules.

This is only valid for Library project. This is ignored in Application project.

void setProguardFiles(Iterable<?> proguardFileIterable)

Sets the ProGuard configuration files.

There are two ProGuard rules files that ship with the Android plugin and are used by default:

  • proguard-android.txt
  • proguard-android-optimize.txt

proguard-android-optimize.txt is identical to proguard-android.txt , exccept with optimizations enabled. You can use getDefaultProguardFile(String filename) to return the full path of the files.

void setTestProguardFiles(Iterable<?> files)

Specifies proguard rule files to be used when processing test code.

Test code needs to be processed to apply the same obfuscation as was done to main code.

void targetSdkVersion(int targetSdkVersion)

Sets the target SDK version to the given value.

See uses-sdk element documentation.

void targetSdkVersion(String targetSdkVersion)

Sets the target SDK version to the given value.

See uses-sdk element documentation.

void testInstrumentationRunnerArgument(String key, String value)

Adds a custom argument to the test instrumentation runner, e.g:

testInstrumentationRunnerArgument "size", "medium"

Test runner arguments can also be specified from the command line:

./gradlew connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.size=medium
./gradlew connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.foo=bar

void testInstrumentationRunnerArguments(Map<String, String> args)

Adds custom arguments to the test instrumentation runner, e.g:

testInstrumentationRunnerArguments(size: "medium", foo: "bar")

Test runner arguments can also be specified from the command line:

./gradlew connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.size=medium
./gradlew connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.foo=bar

void testProguardFile(Object proguardFile)

Adds a proguard rule file to be used when processing test code.

Test code needs to be processed to apply the same obfuscation as was done to main code.

void testProguardFiles(Object... proguardFiles)

Adds proguard rule files to be used when processing test code.

Test code needs to be processed to apply the same obfuscation as was done to main code.