Bird
Raised Fist0
Android Kotlinmobile~20 mins

Build variants (debug, release) in Android Kotlin - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Build Variants Demo
This screen shows a simple message that changes depending on the build variant (debug or release). It helps you understand how to use build variants in Android Kotlin projects.
Target UI
-------------------------
| Build Variants Demo    |
|-----------------------|
| Message:               |
| [Build variant message]|
|                       |
-------------------------
Create two build variants: debug and release
Show a TextView with message 'This is DEBUG build' in debug variant
Show a TextView with message 'This is RELEASE build' in release variant
Use buildConfigField or resource values to differentiate messages
Use a simple Activity with a TextView centered on screen
Starter Code
Android Kotlin
package com.example.buildvariantsdemo

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val messageTextView = findViewById<TextView>(R.id.messageTextView)
        // TODO: Set messageTextView text based on build variant
    }
}
Task 1
Task 2
Solution
Android Kotlin
package com.example.buildvariantsdemo

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val messageTextView = findViewById<TextView>(R.id.messageTextView)
        messageTextView.text = BuildConfig.BUILD_MESSAGE
    }
}

// In build.gradle (Module: app) add inside android block:
// buildTypes {
//     debug {
//         buildConfigField "String", "BUILD_MESSAGE", '"This is DEBUG build"'
//     }
//     release {
//         buildConfigField "String", "BUILD_MESSAGE", '"This is RELEASE build"'
//         minifyEnabled false
//         proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
//     }
// }

// In res/layout/activity_main.xml:
// <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
//     android:layout_width="match_parent"
//     android:layout_height="match_parent"
//     android:gravity="center"
//     android:orientation="vertical">
//
//     <TextView
//         android:id="@+id/messageTextView"
//         android:layout_width="wrap_content"
//         android:layout_height="wrap_content"
//         android:textSize="24sp" />
// </LinearLayout>

We use buildConfigField in the build.gradle file to define a string constant BUILD_MESSAGE that differs between debug and release builds. In the MainActivity, we set the TextView text to this constant. This way, when you run the app in debug mode, it shows "This is DEBUG build", and in release mode, it shows "This is RELEASE build". This demonstrates how build variants can change app behavior or content easily.

Final Result
Completed Screen
-------------------------
| Build Variants Demo    |
|-----------------------|
| Message:               |
| This is DEBUG build    |
|                       |
-------------------------
When running debug build, the message shows 'This is DEBUG build'.
When running release build, the message shows 'This is RELEASE build'.
Stretch Goal
Add a button that shows a Toast message indicating the current build variant.
💡 Hint
Use BuildConfig.DEBUG boolean to check if the build is debug or release and show appropriate Toast.

Practice

(1/5)
1. What is the main purpose of the debug build variant in Android development?
easy
A. To publish the app on the Play Store
B. To test the app with debugging tools enabled
C. To reduce the app size for faster downloads
D. To disable all logging and debugging features

Solution

  1. Step 1: Understand build variant roles

    The debug variant is designed for developers to test and debug the app during development.
  2. Step 2: Compare with release variant

    The release variant is optimized and signed for publishing, unlike debug.
  3. Final Answer:

    To test the app with debugging tools enabled -> Option B
  4. Quick Check:

    Debug variant = testing with debugging [OK]
Hint: Debug variant is for testing, release is for publishing [OK]
Common Mistakes:
  • Confusing debug with release variant
  • Thinking debug is for publishing
  • Assuming debug disables logging
2. Which of the following is the correct way to specify a release build variant in the build.gradle file?
easy
A. buildVariants { debug { minifyEnabled false } }
B. buildVariants { release { minifyEnabled true } }
C. buildTypes { release { minifyEnabled false } }
D. buildTypes { debug { minifyEnabled true } }

Solution

  1. Step 1: Identify correct block name

    The correct block to define build variants is buildTypes, not buildVariants.
  2. Step 2: Check release variant syntax

    Inside buildTypes, release can have minifyEnabled set to true or false. buildTypes { release { minifyEnabled false } } uses correct syntax with minifyEnabled false.
  3. Final Answer:

    buildTypes { release { minifyEnabled false } } -> Option C
  4. Quick Check:

    Use buildTypes block for release variant [OK]
Hint: Use buildTypes, not buildVariants, to define release [OK]
Common Mistakes:
  • Using buildVariants instead of buildTypes
  • Mixing debug and release settings
  • Incorrect minifyEnabled value placement
3. Given this 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
    }
  }
}
medium
A. com.example.app
B. com.example.debug.app
C. com.example.apprelease
D. com.example.app.debug

Solution

  1. Step 1: Identify default application ID

    The default application ID is com.example.app from defaultConfig.
  2. Step 2: Check debug variant suffix

    The debug build type adds .debug as a suffix to the application ID.
  3. Final Answer:

    com.example.app.debug -> Option D
  4. Quick Check:

    Debug variant adds suffix .debug [OK]
Hint: Debug adds suffix; release usually does not [OK]
Common Mistakes:
  • Ignoring applicationIdSuffix
  • Confusing suffix placement
  • Assuming release suffix applies to debug
4. You have this 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?
medium
A. minifyEnabled should be false for debug builds to avoid obfuscation
B. proguardFiles cannot be used in release builds
C. minifyEnabled must be true for debug builds
D. The buildTypes block is missing a closing brace

Solution

  1. Step 1: Understand minifyEnabled purpose

    Minify (obfuscation) is usually enabled in release builds to shrink and protect code.
  2. Step 2: Check debug build settings

    Enabling minify in debug builds is uncommon and can make debugging harder.
  3. Final Answer:

    minifyEnabled should be false for debug builds to avoid obfuscation -> Option A
  4. Quick Check:

    Debug builds usually disable minify [OK]
Hint: Disable minify in debug to ease debugging [OK]
Common Mistakes:
  • Enabling minify in debug builds
  • Thinking proguardFiles are invalid in release
  • Missing braces in buildTypes block
5. You want to create a new build variant called staging that behaves like release but with a different application ID and no code shrinking. Which build.gradle snippet correctly achieves this?
hard
A. buildTypes { staging { initWith release applicationIdSuffix ".staging" minifyEnabled false } }
B. buildTypes { staging { initWith debug applicationIdSuffix ".staging" minifyEnabled true } }
C. buildTypes { staging { applicationIdSuffix ".staging" minifyEnabled true } }
D. buildTypes { staging { initWith release applicationIdSuffix ".release" minifyEnabled true } }

Solution

  1. Step 1: Use initWith to copy release settings

    The initWith release copies all release build settings to staging.
  2. Step 2: Override applicationIdSuffix and minifyEnabled

    Set applicationIdSuffix ".staging" to differentiate the app ID and minifyEnabled false to disable code shrinking.
  3. Final Answer:

    buildTypes { staging { initWith release applicationIdSuffix ".staging" minifyEnabled false } } -> Option A
  4. Quick Check:

    initWith release + override suffix and minify [OK]
Hint: Use initWith release, then override needed settings [OK]
Common Mistakes:
  • Using initWith debug instead of release
  • Forgetting to disable minify for staging
  • Using wrong applicationIdSuffix
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard