In the Android Gradle plugin, how do you give a staging build its own backend URL and application ID so it installs beside production?
answer
- a third build type
- copy settings from debug
- suffix on the application ID
- generated class with per-variant fields
- folders that override main
basics
~10 sAdd a staging build type with initWith(debug) and applicationIdSuffix ".staging", declare the URL with buildConfigField per build type, enable buildFeatures.buildConfig, and override resources such as the app name in a src/staging source set.
solid answer
~30 sCreate a `staging` build type with `initWith(getByName("debug"))` so it copies debug's settings, then set `applicationIdSuffix = ".staging"`; the build type's suffix is appended after any flavor's, so `freeStaging` becomes `com.example.notes.free.staging` and installs beside production. Declare the backend with `buildConfigField("String", "API_BASE_URL", ...)` in `defaultConfig` and override it in `staging`, and turn on `buildFeatures { buildConfig = true }`, because current AGP releases do not generate `BuildConfig` otherwise. Put a different `app_name` in `src/staging/res/values/strings.xml`; the variant, build type and flavor source sets override `main`. Library modules without a `staging` build type need `matchingFallbacks`, and `debugImplementation` dependencies do not reach staging.
code
kotlin · 34 linesandroid {
namespace = "com.example.notes"
defaultConfig {
applicationId = "com.example.notes"
buildConfigField("String", "API_BASE_URL", "\"https://api.example.com/\"")
}
buildFeatures {
buildConfig = true
}
buildTypes {
create("staging") {
initWith(getByName("debug"))
applicationIdSuffix = ".staging"
buildConfigField("String", "API_BASE_URL", "\"https://staging.example.com/\"")
matchingFallbacks += listOf("debug")
}
}
flavorDimensions += "tier"
productFlavors {
create("free") {
dimension = "tier"
applicationIdSuffix = ".free"
buildConfigField("Boolean", "IS_PAID", "false")
}
create("paid") {
dimension = "tier"
buildConfigField("Boolean", "IS_PAID", "true")
}
}
}go deeper
Recall that a build type changes how the app is built and that applicationIdSuffix lets two builds of one app install side by side.
Explain buildConfigField with buildFeatures.buildConfig, source set priority for resources, and why classes cannot be overridden from main.
Show the traps you have hit: matchingFallbacks for libraries, debug-only tooling leaking or missing, and signing secrets kept out of the repository.
Decide how many environments become build types versus runtime configuration, since every build type multiplies variants, CI time and test effort.
## The setup Take a notes app with two product flavors in a `tier` dimension, `free` and `paid`, plus the default `debug` and `release` build types. QA wants a **staging** build that talks to the staging backend, shows a different name on the launcher, and installs next to the production app on the same phone. Three Android Gradle plugin features do the work: a build type, the generated `BuildConfig` class, and per-variant source sets. ## A staging build type - `create("staging") { initWith(getByName("debug")) }` copies every setting of the `debug` build type, including its debuggable flag and its debug signing config, so you only override what differs. - `applicationIdSuffix = ".staging"` changes the **application ID**, the identity the device and Google Play use. No two installed apps can share one, so a different ID is what lets staging sit beside production. - Gradle applies the build type after the product flavor, so suffixes stack in that order: with `free` adding `.free`, the `freeStaging` variant is `com.example.notes.free.staging`. - The `namespace` does not change. It stays the package of the generated `R` and `BuildConfig` classes, so code needs no per-variant imports. - Library modules usually have only `debug` and `release`. Without `matchingFallbacks += listOf("debug")` on `staging`, the plugin cannot pick a library variant and dependency resolution fails. ## Values per variant: BuildConfig `buildConfigField(type, name, value)` adds a constant to the generated `BuildConfig` class. The value is pasted into generated Java source, so a string needs escaped quotes: `"\"https://staging.example.com/\""`. | Where you declare it | Applies to | |---|---| | `defaultConfig` | Every variant, unless overridden | | A build type | Every variant of that build type | | A product flavor | Every variant of that flavor, such as an `IS_PAID` flag | Current AGP releases generate `BuildConfig` only when the module sets `buildFeatures { buildConfig = true }`; without it, references to `BuildConfig.API_BASE_URL` fail to resolve. Anything compiled into `BuildConfig` can be read by anyone holding the APK, so it is a place for URLs and flags, never for secrets. For values the manifest needs, `manifestPlaceholders` and the built-in `${applicationId}` placeholder do the same job. ## Files per variant: source sets Each build type, flavor and variant can have its own folder under `src/`. For the `freeStaging` variant, Gradle merges, from highest priority to lowest: 1. `src/freeStaging/` (the variant) 2. `src/staging/` (the build type) 3. `src/free/` (the flavor) 4. `src/main/` Resources and manifests merge in that order, so a `strings.xml` in `src/staging/res/values/` that redefines `app_name` wins over `main`. Code does not override: a class defined in both `src/main/` and `src/staging/` is a duplicate-class build error, so a class that differs per build type must live in every build type's folder and not in `main`. The same rule decides debug-only code. A network inspector declared with `debugImplementation` or placed in `src/debug/` reaches only the `debug` build type. `initWith` copies settings, not source sets or dependencies, so staging gets that tooling only through its own `"stagingImplementation"(...)` or `src/staging/` code. ## Signing per build type - The `debug` build type is signed automatically with a debug certificate kept in `$HOME/.android/debug.keystore`; staging inherits that through `initWith`. Google Play does not accept debug-signed apps. - The `release` build type is not signed unless you attach a `signingConfig` from `signingConfigs`. - Keep keystore passwords out of the build file: read them from environment variables on CI, or from a `keystore.properties` file that is never committed. ## When an environment should not be a build type Every build type multiplies with every flavor: two flavors and three build types already make six variants, each with its own tasks and CI time. A build type earns its place when the build itself must differ (application ID, signing, debuggable flag, bundled tooling). When only a value differs, a runtime setting can do the job instead: - A debug-only settings screen that switches the backend URL at run time, kept out of release through `src/debug/`. - A value read from a config file packaged per flavor in `src/<flavor>/assets/`. Instrumented tests follow the same identity rules: the test APK for a variant gets that variant's application ID with `.test` appended, so `com.example.notes.free.staging.test` tests the free staging build. ## A short checklist - One build type per backend environment, one flavor per product tier. - A distinct application ID for every variant that must coexist on a device. - `buildFeatures.buildConfig` on wherever `buildConfigField` is used. - `matchingFallbacks` for every custom build type the libraries lack.
- Why does the staging build fail to resolve a library module that works in debug?Variant-aware dependency matching asks each library for the same build type the app is building. A library with only `debug` and `release` has no `staging`, so resolution fails. `matchingFallbacks += listOf("debug")` on the app's `staging` build type tells the plugin which library build type to use instead.
- How do you keep the release keystore password out of the repository?Leave the passwords out of `signingConfigs` and read them at configuration time from environment variables set by CI, or from a local `keystore.properties` file that is never committed. Only the `release` build type gets that signing config; debug and staging keep the automatic debug certificate.
- Changing namespace is how you give staging its own application ID
- buildConfigField works on its own without enabling the BuildConfig feature
- initWith(debug) also brings debugImplementation dependencies to staging
- Put the staging API key in BuildConfig, since the app is compiled
- Define the same class in src/main and src/staging to override it