Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Flavors and Variants

Status: 🟡 Partial — [flavors] parses and validates, and BuildConfig generation works. The build path does not yet apply flavors: --flavor, --variant, and --all-variants are accepted and ignored. See Implementation Status.

Flavors build several versions of the same application from one codebase: free and paid, staging and production, one white-label build per customer.

Android developers know this as productFlavors. Kargo generalizes it — flavors work on every target, not just Android.

Dimensions, flavors, variants

  • A dimension is an independent axis of variation: tier, environment, brand.
  • A flavor is one value on an axis: free and paid within tier.
  • A variant is one flavor from each dimension, plus a profile.

Two dimensions of two flavors each, with two profiles, gives eight variants:

free-staging-dev        paid-staging-dev
free-staging-release    paid-staging-release
free-production-dev     paid-production-dev
free-production-release paid-production-release

The count multiplies fast. Exclusions exist because of this.

Declaring flavors

[flavors]
dimensions = ["tier", "environment"]
default = { tier = "paid", environment = "staging" }

[flavors.tier.free]
build-config = { IS_PAID = "false", AD_SUPPORTED = "true" }
application-id-suffix = ".free"

[flavors.tier.paid]
build-config = { IS_PAID = "true", AD_SUPPORTED = "false" }

[flavors.environment.staging]
build-config = { API_URL = "https://staging.api.example.com", LOG_LEVEL = "DEBUG" }
application-id-suffix = ".staging"

[flavors.environment.production]
build-config = { API_URL = "https://api.example.com", LOG_LEVEL = "ERROR" }

dimensions is ordered, and that order determines variant names — tier before environment means free-staging, never staging-free.

KeyPurpose
build-configCompile-time constants for this flavor
application-id-suffixAppended to the application id, so variants install side by side
signingPer-flavor signing configuration

BuildConfig

Each variant compiles against a generated BuildConfig object:

// generated — do not edit
object BuildConfig {
    const val IS_PAID: String = "true"
    const val AD_SUPPORTED: String = "false"
    const val API_URL: String = "https://api.example.com"
    const val LOG_LEVEL: String = "ERROR"
    const val BUILD_TYPE: String = "release"   // from the profile
    const val DEBUG: Boolean = false           // from the profile
    const val VERSION_NAME: String = "1.0.0"   // from [package]
}
val client = HttpClient(BuildConfig.API_URL)
if (BuildConfig.IS_PAID.toBoolean()) enablePremium()

Values merge from three sources, later overriding earlier:

  1. the manifest's top-level [build-config]
  2. each matching flavor's build-config
  3. profile-derived values (BUILD_TYPE, DEBUG)

Everything in build-config is a String except the profile-derived DEBUG. That is deliberate — the manifest is TOML and inferring Kotlin types from TOML values produces surprises. Call .toBoolean() or .toInt() at the use site.

The same values are exported to hooks and plugins as KARGO_BUILD_CONFIG_* environment variables.

Flavor-specific sources

src/
├── main/kotlin/              # every variant
├── freeMain/kotlin/          # tier=free
├── paidMain/kotlin/          # tier=paid
├── stagingMain/kotlin/       # environment=staging
├── productionMain/kotlin/
└── freeStagingMain/kotlin/   # exactly free+staging (rare)

Sources merge common → dimension flavor → exact variant. A later layer can replace a file from an earlier one, which is how a flavor swaps an icon, a theme, or a single implementation class.

Prefer build-config for values and source sets for behavior. A flavor that overrides twelve files is usually two products wearing one manifest.

Flavor-specific dependencies

[flavor.free.dependencies]
ad-sdk = "com.example:ad-sdk:2.0.0"

[flavor.paid.dependencies]
premium = "com.example:premium:1.0.0"

[flavor.staging.dependencies]
leak-canary = "com.squareup.leakcanary:leakcanary-android:2.12"

Note the singular [flavor.<name>.dependencies] for dependencies versus plural [flavors.<dimension>.<name>] for definitions.

Each variant resolves its own dependency graph: [dependencies] plus every matching flavor's additions. If two flavors in different dimensions pull incompatible versions of the same artifact, that combination fails resolution with both paths named — rather than silently picking one.

Building a variant

kargo build --variant free-staging-release   # one exact variant
kargo build --flavor paid                    # every variant containing paid
kargo build --all-variants                   # everything
kargo run --variant paid-production-release
kargo test --flavor free
kargo variant list                           # what is defined
kargo variant info paid-staging-release      # sources, deps, build config

Output goes to build/<variant>/<target>/.

With no flavor flags, Kargo builds the default variant. Omit default and you must name one explicitly — a reasonable thing to want when no variant is a safe accident.

Excluding combinations

[flavors]
dimensions = ["tier", "environment"]
exclude = [
    { tier = "free", environment = "production" },
]

Excluded combinations do not exist: they are not built by --all-variants, not listed by kargo variant list, and naming one is an error.

Per-flavor signing

[flavors.tier.paid.signing]
keystore = "release.keystore"
key-alias = "paid-release"
store-password-cmd = "op read 'op://Dev/Keystore/password'"

store-password-cmd runs an external command and reads the password from its stdout, so the secret never lands in a file — neither the manifest nor .kargo.env. Any secret manager with a CLI works.

When not to use flavors

Flavors suit variation in configuration. They suit variation in product badly.

SituationBetter tool
Different API URL per environmentFlavors
Free tier lacks three screensFlavors
Two apps sharing a coreWorkspace with a shared module
Platform differencesTargets and expect/actual
A customer needs a different data modelSeparate projects

The eight-variant matrix above means eight builds and eight test runs. If every variant needs its own test suite, the dimensions are modelling products.

Next