TExcelTAD

Product Flavors & Build Variants

Build types say how to build. Product flavors say what to build: a free and a paid edition, a white-label app per customer, a build for Google Play and one for another store. Every flavor belongs to a flavor dimension, and AGP multiplies everything out:

variant = one flavor from each dimension × one build type

Two flavors × two build types = four variants, each with its own tasks, outputs and source set.

kotlin
android {
    flavorDimensions += "tier"
    productFlavors {
        create("free") {
            dimension = "tier"
            applicationIdSuffix = ".free"     // free and paid install side by side
            versionNameSuffix = "-free"
        }
        create("paid") { dimension = "tier" }
    }
}

A flavor can override most of defaultConfig: applicationId, minSdk, versionCode, buildConfigField, resValue, manifestPlaceholders, signing.

The variant tasks

AGP names tasks <action><Flavor><BuildType>. tasks --all lists them; here are the assemble and bundle ones:

bash · try it yourself
mkdir -p hello/app/src/main/java/com/example/hello && cd hello
cat > settings.gradle.kts <<'KTS'
pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
dependencyResolutionManagement { repositories { google(); mavenCentral() } }
rootProject.name = "hello"
include(":app")
KTS
echo 'plugins { id("com.android.application") version "9.4.1" apply false }' > build.gradle.kts
cat > app/src/main/AndroidManifest.xml <<'XML'
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:label="Hello">
        <activity android:name=".MainActivity" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
XML
cat > app/src/main/java/com/example/hello/MainActivity.java <<'JAVA'
package com.example.hello;

public class MainActivity extends android.app.Activity {
    @Override protected void onCreate(android.os.Bundle state) {
        super.onCreate(state);
        android.widget.TextView view = new android.widget.TextView(this);
        view.setText(Features.title());
        setContentView(view);
    }
}
JAVA
mkdir -p app/src/free/java/com/example/hello app/src/paid/java/com/example/hello
cat > app/src/free/java/com/example/hello/Features.java <<'JAVA'
package com.example.hello;

final class Features {
    static String title() { return "Hello (free, with ads)"; }
}
JAVA
cat > app/src/paid/java/com/example/hello/Features.java <<'JAVA'
package com.example.hello;

final class Features {
    static String title() { return "Hello (paid)"; }
}
JAVA
cat > app/build.gradle.kts <<'KTS'
plugins { id("com.android.application") }

android {
    namespace = "com.example.hello"
    compileSdk = 36
    defaultConfig {
        applicationId = "com.example.hello"
        minSdk = 24
        targetSdk = 36
        versionCode = 1
        versionName = "1.0"
    }
    flavorDimensions += "tier"
    productFlavors {
        create("free") { dimension = "tier"; applicationIdSuffix = ".free"; versionNameSuffix = "-free" }
        create("paid") { dimension = "tier" }
    }
}
KTS
gradle wrapper > /dev/null
./gradlew tasks --all | grep -E '^app:(assemble|bundle)[A-Z]' | grep -vE 'Classes|Resources|Test'
app:assembleDebug - Assembles main outputs for all Debug variants.
app:assembleFree - Assembles main outputs for all Free variants.
app:assemblePaid - Assembles main outputs for all Paid variants.
app:assembleRelease - Assembles main outputs for all Release variants.
app:bundleDebug - Assembles bundles for all Debug variants.
app:bundleFree - Assembles bundles for all Free variants.
app:bundlePaid - Assembles bundles for all Paid variants.
app:bundleRelease - Assembles bundles for all Release variants.
app:assembleFreeDebug - Assembles main output for variant freeDebug
app:assembleFreeRelease - Assembles main output for variant freeRelease
app:assemblePaidDebug - Assembles main output for variant paidDebug
app:assemblePaidRelease - Assembles main output for variant paidRelease
app:bundleFreeDebug - Assembles bundle for variant freeDebug
app:bundleFreeRelease - Assembles bundle for variant freeRelease
app:bundlePaidDebug - Assembles bundle for variant paidDebug
app:bundlePaidRelease - Assembles bundle for variant paidRelease

There are aggregate tasks per dimension value (assembleFree builds freeDebug and freeRelease) and per build type (assembleRelease builds every flavor's release). assembleFreeDebug builds exactly one variant, and it's what you want in a fast inner loop and in focused CI jobs.

Source sets per variant

Each flavor, build type and variant gets its own source set folder, merged on top of src/main. The session above put a different Features.java in src/free/java and src/paid/java; src/main calls Features.title() without knowing which one it gets. Classes can't be overridden between main and a flavor (that's a duplicate class error), but they can be provided per flavor, as here. Resources and manifests do override, by priority:

text
src/freeDebug/   (variant)       highest priority
src/debug/       (build type)
src/free/        (flavor)
src/main/                        lowest priority

Let's build the debug variant of each flavor and look inside both dex files with dexdump, from build-tools:

bash · try it yourself
mkdir -p hello/app/src/main/java/com/example/hello && cd hello
cat > settings.gradle.kts <<'KTS'
pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
dependencyResolutionManagement { repositories { google(); mavenCentral() } }
rootProject.name = "hello"
include(":app")
KTS
echo 'plugins { id("com.android.application") version "9.4.1" apply false }' > build.gradle.kts
echo '<manifest xmlns:android="http://schemas.android.com/apk/res/android"><application android:label="Hello" /></manifest>' > app/src/main/AndroidManifest.xml
mkdir -p app/src/free/java/com/example/hello app/src/paid/java/com/example/hello
printf 'package com.example.hello;\nfinal class Features { static String title() { return "free, with ads"; } }\n' > app/src/free/java/com/example/hello/Features.java
printf 'package com.example.hello;\nfinal class Features { static String title() { return "paid"; } }\n' > app/src/paid/java/com/example/hello/Features.java
cat > app/build.gradle.kts <<'KTS'
plugins { id("com.android.application") }

android {
    namespace = "com.example.hello"
    compileSdk = 36
    defaultConfig { applicationId = "com.example.hello"; minSdk = 24; targetSdk = 36; versionCode = 1; versionName = "1.0" }
    flavorDimensions += "tier"
    productFlavors {
        create("free") { dimension = "tier"; applicationIdSuffix = ".free" }
        create("paid") { dimension = "tier" }
    }
}
KTS
gradle wrapper > /dev/null
./gradlew assembleFreeDebug assemblePaidDebug | grep -E '^> Task :app:(compile|assemble).*Java|^> Task :app:assemble|^BUILD'
find app/build/outputs/apk -name '*.apk' | sort
for f in free paid; do
  unzip -o -q app/build/outputs/apk/$f/debug/app-$f-debug.apk classes.dex -d $f
  echo "$f: $($ANDROID_HOME/build-tools/36.0.0/dexdump -d $f/classes.dex | sed -n '/Lcom\/example\/hello\/Features;/,/source_file_idx/p' | grep -o 'const-string [^,]*, "[^"]*"' | cut -d'"' -f2)"
done
> Task :app:compileFreeDebugJavaWithJavac
> Task :app:assembleFreeDebug
> Task :app:compilePaidDebugJavaWithJavac
> Task :app:assemblePaidDebug
BUILD SUCCESSFUL in 14s
app/build/outputs/apk/free/debug/app-free-debug.apk
app/build/outputs/apk/paid/debug/app-paid-debug.apk
free:
paid:

Note where the outputs land: apk/<flavor>/<buildType>/app-<flavor>-<buildType>.apk. CI scripts that upload artifacts must use these paths.

Several dimensions

With more than one dimension, the order of flavorDimensions decides both priority (first wins when two flavors set the same property) and the variant name. Here's the arithmetic as a runnable model. It's the same cartesian product AGP computes, including a filter that disables variants nobody needs:

python · try it yourself
from itertools import product

dimensions = {"tier": ["free", "paid"], "store": ["play", "galaxy"]}
build_types = ["debug", "release"]

def variants(disabled=lambda v: False):
    out = []
    for combo in product(*dimensions.values()):
        for bt in build_types:
            parts = list(combo) + [bt]
            name = parts[0] + "".join(p.capitalize() for p in parts[1:])
            if not disabled(parts):
                out.append(name)
    return out

all_variants = variants()
print(len(all_variants), "variants:", ", ".join(all_variants))
print("tasks: assemble" + all_variants[0][0].upper() + all_variants[0][1:], "...")

# Nobody needs debug builds of the Galaxy store flavor: drop them.
kept = variants(disabled=lambda p: p[1] == "galaxy" and p[2] == "debug")
print(len(kept), "after filtering:", ", ".join(kept))
8 variants: freePlayDebug, freePlayRelease, freeGalaxyDebug, freeGalaxyRelease, paidPlayDebug, paidPlayRelease, paidGalaxyDebug, paidGalaxyRelease
tasks: assembleFreePlayDebug ...
6 after filtering: freePlayDebug, freePlayRelease, freeGalaxyRelease, paidPlayDebug, paidPlayRelease, paidGalaxyRelease

The filter removed the two galaxy debug variants (one per tier). In AGP the same filter lives in the androidComponents block and runs before any task is created:

kotlin
androidComponents {
    beforeVariants(selector().withBuildType("debug").withFlavor("store" to "galaxy")) { variant ->
        variant.enable = false          // no tasks, no configuration time for this variant
    }
}

Each variant adds tasks, configuration time and CI minutes. See Field Notes: Flavors, SDK Levels & CI for how flavor counts get out of hand.

Test yourself

An app has dimensions tier (free, paid) and store (play, galaxy, amazon) and the two default build types. How many variants does AGP create before filtering?

  1. 7
  2. 10
  3. 12
  4. 24

Which task builds exactly one APK for the free flavor's release build type?

  1. assembleFree
  2. assembleRelease
  3. assembleFreeRelease
  4. bundleFree

Where would a resource that only the paid debug variant should use go?

  1. src/main/res
  2. src/paid/res
  3. src/paidDebug/res
  4. src/debug/res