Build variants let you create different versions of your app easily. For example, one for testing (debug) and one for users (release).
Build variants (debug, release) in Android Kotlin
android {
buildTypes {
debug {
applicationIdSuffix ".debug"
versionNameSuffix "-DEBUG"
debuggable true
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
}The debug build type is for testing and usually includes extra info.
The release build type is optimized and signed for publishing.
buildTypes {
debug {
applicationIdSuffix ".debug"
debuggable true
}
release {
minifyEnabled false
}
}buildTypes {
debug {
versionNameSuffix "-dev"
}
release {
signingConfig signingConfigs.release
}
}This Gradle script defines two build variants: debug and release. Debug adds ".debug" to the app ID and "-DEBUG" to the version name, making it easy to identify. Release enables code shrinking and uses signing for publishing.
android {
compileSdk 33
defaultConfig {
applicationId "com.example.myapp"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
}
buildTypes {
debug {
applicationIdSuffix ".debug"
versionNameSuffix "-DEBUG"
debuggable true
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
}Always test your release build before publishing to catch issues.
Debug builds are slower but easier to fix problems in.
You can create more build variants for flavors like free or paid versions.
Build variants let you make different app versions from one project.
Debug is for testing; release is for publishing.
You control settings like app ID, version, and optimization per variant.
Practice
debug build variant in Android development?Solution
Step 1: Understand build variant roles
Thedebugvariant is designed for developers to test and debug the app during development.Step 2: Compare with release variant
Thereleasevariant is optimized and signed for publishing, unlikedebug.Final Answer:
To test the app with debugging tools enabled -> Option BQuick Check:
Debug variant = testing with debugging [OK]
- Confusing debug with release variant
- Thinking debug is for publishing
- Assuming debug disables logging
release build variant in the build.gradle file?Solution
Step 1: Identify correct block name
The correct block to define build variants isbuildTypes, notbuildVariants.Step 2: Check release variant syntax
InsidebuildTypes,releasecan haveminifyEnabledset to true or false. buildTypes { release { minifyEnabled false } } uses correct syntax withminifyEnabled false.Final Answer:
buildTypes { release { minifyEnabled false } } -> Option CQuick Check:
Use buildTypes block for release variant [OK]
- Using buildVariants instead of buildTypes
- Mixing debug and release settings
- Incorrect minifyEnabled value placement
build.gradle snippet, what will be the application ID for the debug build variant?
android {
defaultConfig {
applicationId "com.example.app"
}
buildTypes {
debug {
applicationIdSuffix ".debug"
}
release {
minifyEnabled true
}
}
}Solution
Step 1: Identify default application ID
The default application ID iscom.example.appfromdefaultConfig.Step 2: Check debug variant suffix
Thedebugbuild type adds.debugas a suffix to the application ID.Final Answer:
com.example.app.debug -> Option DQuick Check:
Debug variant adds suffix .debug [OK]
- Ignoring applicationIdSuffix
- Confusing suffix placement
- Assuming release suffix applies to debug
build.gradle snippet:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled true
}
}
What is the problem with this configuration?Solution
Step 1: Understand minifyEnabled purpose
Minify (obfuscation) is usually enabled in release builds to shrink and protect code.Step 2: Check debug build settings
Enabling minify in debug builds is uncommon and can make debugging harder.Final Answer:
minifyEnabled should be false for debug builds to avoid obfuscation -> Option AQuick Check:
Debug builds usually disable minify [OK]
- Enabling minify in debug builds
- Thinking proguardFiles are invalid in release
- Missing braces in buildTypes block
staging that behaves like release but with a different application ID and no code shrinking. Which build.gradle snippet correctly achieves this?Solution
Step 1: Use initWith to copy release settings
TheinitWith releasecopies all release build settings to staging.Step 2: Override applicationIdSuffix and minifyEnabled
SetapplicationIdSuffix ".staging"to differentiate the app ID andminifyEnabled falseto disable code shrinking.Final Answer:
buildTypes { staging { initWith release applicationIdSuffix ".staging" minifyEnabled false } } -> Option AQuick Check:
initWith release + override suffix and minify [OK]
- Using initWith debug instead of release
- Forgetting to disable minify for staging
- Using wrong applicationIdSuffix
Ready to prove it?
Pass the test and this pattern moves unknown
