Build variants, flavors, and build types for dev, staging, and production environments.
Build Types
// build.gradle.kts (app)
android {
buildTypes {
debug {
isMinifyEnabled = false
isDebuggable = true
applicationIdSuffix = ".debug"
versionNameSuffix = "-debug"
signingConfig = signingConfigs.getByName("debug")
// Manifest placeholders
manifestPlaceholders["hostName"] = "api-dev.example.com"
// Build config fields
buildConfigField("boolean", "IS_DEBUG", "true")
buildConfigField("String", "API_BASE_URL", "\"https://api-dev.example.com\"")
buildConfigField("int", "LOG_LEVEL", "3") // Log.DEBUG
// Resource values
resValue("string", "app_name", "MyApp Debug")
resValue("color", "app_primary", "#FF0000")
}
release {
isMinifyEnabled = true
isShrinkResources = true
isDebuggable = false
signingConfig = signingConfigs.getByName("release")
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
manifestPlaceholders["hostName"] = "api.example.com"
buildConfigField("boolean", "IS_DEBUG", "false")
buildConfigField("String", "API_BASE_URL", "\"https://api.example.com\"")
buildConfigField("int", "LOG_LEVEL", "6") // Log.ERROR
resValue("string", "app_name", "MyApp")
resValue("color", "app_primary", "#0066FF")
}
// Custom build type
staging {
initWith(release)
isDebuggable = true
applicationIdSuffix = ".staging"
versionNameSuffix = "-staging"
signingConfig = signingConfigs.getByName("release")
manifestPlaceholders["hostName"] = "api-staging.example.com"
buildConfigField("boolean", "IS_DEBUG", "true")
buildConfigField("String", "API_BASE_URL", "\"https://api-staging.example.com\"")
buildConfigField("int", "LOG_LEVEL", "3")
resValue("string", "app_name", "MyApp Staging")
resValue("color", "app_primary", "#FFAA00")
}
}
}Product Flavors
android {
flavorDimensions = "environment"
productFlavors {
free {
dimension = "environment"
applicationIdSuffix = ".free"
versionNameSuffix = "-free"
// Feature flags
buildConfigField("boolean", "HAS_PREMIUM", "false")
buildConfigField("boolean", "HAS_ANALYTICS", "true")
buildConfigField("String", "ANALYTICS_KEY", "\"free_key\"")
// Resources
resValue("string", "app_name", "MyApp Free")
resValue("string", "premium_upsell", "Upgrade to Premium!")
}
premium {
dimension = "environment"
applicationIdSuffix = ".premium"
versionNameSuffix = "-premium"
buildConfigField("boolean", "HAS_PREMIUM", "true")
buildConfigField("boolean", "HAS_ANALYTICS", "true")
buildConfigField("String", "ANALYTICS_KEY", "\"premium_key\"")
resValue("string", "app_name", "MyApp Premium")
resValue("string", "premium_upsell", "")
// Premium-only source set
// src/premium/java/... for premium features
}
enterprise {
dimension = "environment"
applicationIdSuffix = ".enterprise"
versionNameSuffix = "-enterprise"
buildConfigField("boolean", "HAS_PREMIUM", "true")
buildConfigField("boolean", "HAS_ANALYTICS", "true")
buildConfigField("boolean", "HAS_SSO", "true")
buildConfigField("String", "ANALYTICS_KEY", "\"enterprise_key\"")
resValue("string", "app_name", "MyApp Enterprise")
// Enterprise source set
}
}
}Variant Combinations
# With the above configuration, you get these variants:
# Debug variants:
- freeDebug
- premiumDebug
- enterpriseDebug
# Release variants:
- freeRelease
- premiumRelease
- enterpriseRelease
# Staging variants:
- freeStaging
- premiumStaging
- enterpriseStaging
# Each variant has unique:
- applicationId (e.g., com.example.myapp.free.debug)
- versionName (e.g., 1.0.0-free-debug)
- BuildConfig fields
- Resources
- Source setsSource Sets
app/
├── src/
│ ├── main/ # Common code
│ │ ├── java/
│ │ ├── res/
│ │ └── AndroidManifest.xml
│ ├── debug/ # Debug-only code
│ │ ├── java/com/example/debug/DebugLogger.kt
│ │ └── res/values/strings.xml
│ ├── release/ # Release-only code
│ │ └── java/com/example/release/ReleaseLogger.kt
│ ├── staging/ # Staging-only code
│ ├── free/ # Free flavor code
│ │ ├── java/com/example/free/FreeFeatures.kt
│ │ └── res/layout/premium_upsell.xml
│ ├── premium/ # Premium flavor code
│ │ ├── java/com/example/premium/PremiumFeatures.kt
│ │ └── res/values/strings.xml
│ ├── enterprise/ # Enterprise flavor code
│ ├── freeDebug/ # Free + Debug
│ ├── premiumRelease/ # Premium + Release
│ └── enterpriseStaging/ # Enterprise + Staging
Flavor-Specific Code
// src/free/java/com/example/free/FreeFeatures.kt
object Features {
val hasPremium = false
val hasAnalytics = true
val hasSSO = false
val maxProjects = 3
}
// src/premium/java/com/example/premium/PremiumFeatures.kt
object Features {
val hasPremium = true
val hasAnalytics = true
val hasSSO = false
val maxProjects = 100
}
// src/enterprise/java/com/example/enterprise/EnterpriseFeatures.kt
object Features {
val hasPremium = true
val hasAnalytics = true
val hasSSO = true
val maxProjects = Int.MAX_VALUE
}
// Usage in common code
class FeatureManager {
fun canCreateProject(): Boolean {
return currentProjectCount < Features.maxProjects
}
}Flavor-Specific Dependencies
dependencies {
// Common
implementation("androidx.core:core-ktx:1.12.0")
// Free flavor
freeImplementation("com.google.firebase:firebase-analytics-ktx:21.5.0")
// Premium flavor
premiumImplementation("com.google.firebase:firebase-analytics-ktx:21.5.0")
premiumImplementation("com.revenuecat.purchases:purchases-ktx:7.0.0")
// Enterprise flavor
enterpriseImplementation("com.google.firebase:firebase-analytics-ktx:21.5.0")
enterpriseImplementation("com.okta.android:okta-oidc:3.0.0")
// Debug only
debugImplementation("com.squareup.leakcanary:leakcanary-android:2.14")
// Release only
releaseImplementation("com.google.firebase:firebase-crashlytics-ktx:18.6.0")
}Manifest Placeholders
android {
defaultConfig {
manifestPlaceholders = [
"hostName": "api.example.com",
"scheme": "https",
"appAuthRedirectScheme": "com.example.myapp"
]
}
productFlavors {
free {
manifestPlaceholders["hostName"] = "api-free.example.com"
}
premium {
manifestPlaceholders["hostName"] = "api-premium.example.com"
}
}
buildTypes {
debug {
manifestPlaceholders["hostName"] = "api-dev.example.com"
}
}
}<!-- AndroidManifest.xml -->
<activity android:name=".AuthActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="${scheme}"
android:host="${hostName}"
android:pathPrefix="/oauth2redirect" />
</intent-filter>
</activity>
<!-- Deep links with manifest placeholders -->
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" android:host="${hostName}" />
</intent-filter>Version Management
// gradle.properties
versionMajor = 1
versionMinor = 0
versionPatch = 0
versionBuild = 1
// build.gradle.kts
android {
defaultConfig {
versionCode = versionMajor * 10000 + versionMinor * 100 + versionPatch
versionName = "${versionMajor}.${versionMinor}.${versionPatch}"
}
// Auto-increment version code for each variant
applicationVariants.all { variant ->
def flavorName = variant.flavorName
def buildTypeName = variant.buildType.name
// Custom version code per variant
def variantCode = versionCode
if (flavorName.contains("premium")) variantCode += 100000
if (flavorName.contains("enterprise")) variantCode += 200000
if (buildTypeName == "debug") variantCode += 1000000
if (buildTypeName == "staging") variantCode += 2000000
variant.outputs.all {
versionCodeOverride = variantCode
outputFileName = "${variant.name}-${versionName}.apk"
}
}
}Build Config Usage
// BuildConfig generated per variant
object AppConfig {
// API
val BASE_URL = BuildConfig.API_BASE_URL
val IS_DEBUG = BuildConfig.IS_DEBUG
val LOG_LEVEL = BuildConfig.LOG_LEVEL
// Features
val HAS_PREMIUM = BuildConfig.HAS_PREMIUM
val HAS_ANALYTICS = BuildConfig.HAS_ANALYTICS
val HAS_SSO = BuildConfig.HAS_SSO
val ANALYTICS_KEY = BuildConfig.ANALYTICS_KEY
// App
val APP_NAME = BuildConfig.FLAVOR // free, premium, enterprise
val BUILD_TYPE = BuildConfig.BUILD_TYPE // debug, release, staging
val VERSION_NAME = BuildConfig.VERSION_NAME
val VERSION_CODE = BuildConfig.VERSION_CODE
val APPLICATION_ID = BuildConfig.APPLICATION_ID
}
// Runtime usage
class ApiClient {
private val baseUrl = AppConfig.BASE_URL
private val isDebug = AppConfig.IS_DEBUG
fun createRetrofit(): Retrofit {
val builder = Retrofit.Builder().baseUrl(baseUrl)
if (isDebug) {
builder.addInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
})
}
return builder.build()
}
}
// Feature flags
@Composable
fun PremiumUpsell() {
if (!AppConfig.HAS_PREMIUM) {
Text(text = stringResource(R.string.premium_upsell))
}
}Gradle Commands
# Build specific variant
./gradlew assembleFreeDebug
./gradlew assemblePremiumRelease
./gradlew assembleEnterpriseStaging
# Build all variants
./gradlew assembleDebug
./gradlew assembleRelease
./gradlew assemble
# Install specific variant
./gradlew installFreeDebug
./gradlew installPremiumRelease
# List all variants
./gradlew tasks --all | grep assemble
# Clean specific variant
./gradlew cleanFreeDebugTesting Variants
// test/FreeDebugTest.kt
class FreeDebugTest {
@Test
fun freeDebug_hasCorrectConfig() {
assertFalse(BuildConfig.HAS_PREMIUM)
assertTrue(BuildConfig.IS_DEBUG)
assertEquals("https://api-dev.example.com", BuildConfig.API_BASE_URL)
}
}
// test/PremiumReleaseTest.kt
class PremiumReleaseTest {
@Test
fun premiumRelease_hasCorrectConfig() {
assertTrue(BuildConfig.HAS_PREMIUM)
assertFalse(BuildConfig.IS_DEBUG)
assertEquals("https://api.example.com", BuildConfig.API_BASE_URL)
}
}Try It
Add buildTypes (debug, release, staging). Add productFlavors (free, premium). Use flavorDimensions. Create source sets. Test BuildConfig fields. Build specific variants.
# Build commands:
./gradlew assembleFreeDebug
./gradlew assemblePremiumRelease
./gradlew installFreeDebugBuild Variants mastery
- Create debug, release, staging build types
- Define free, premium, enterprise flavors
- Set flavorDimensions
- Configure applicationIdSuffix and versionNameSuffix per flavor
- Add BuildConfig fields for feature flags
- Create flavor-specific source sets
- Add flavor-specific dependencies
- Use manifest placeholders for deep links
- Auto-increment versionCode per variant
- Test BuildConfig in unit tests
Build types: debug/release/staging (minify, signing, debuggable). Flavors: free/premium/enterprise (features, resources). Variants = BuildType × Flavor. Source sets: src/<flavor><BuildType>/. BuildConfig for feature flags. manifestPlaceholders for dynamic manifest.
Resources
- Build Variants: https://developer.android.com/build/build-variants
- Build Types: https://developer.android.com/build/build-types
- Product Flavors: https://developer.android.com/build/product-flavors
- Manifest Placeholders: https://developer.android.com/build/manifest-placeholder