Bird
Raised Fist0
Android Kotlinmobile~20 mins

App size optimization in Android Kotlin - Mini App: Build & Ship

Choose your learning style10 modes available
Build: App Size Optimization Demo
This screen demonstrates simple techniques to reduce app size by removing unused resources and enabling code shrinking.
Target UI
-------------------------
| App Size Optimization |
|-----------------------|
| [Optimize Button]     |
|                       |
| Status: Ready         |
-------------------------
Add a button labeled 'Optimize' that simulates app size optimization.
Display a status text below the button showing 'Ready' initially.
When the button is clicked, update the status text to 'Optimization Complete'.
Use ProGuard/R8 configuration to enable code shrinking and resource shrinking in build.gradle.
Remove unused drawable resource from the project (simulate by not referencing it).
Starter Code
Android Kotlin
package com.example.appsizeoptimization

import android.os.Bundle
import android.widget.Button
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 optimizeButton: Button = findViewById(R.id.optimizeButton)
        val statusText: TextView = findViewById(R.id.statusText)

        // TODO: Add click listener to update status text
    }
}
Task 1
Task 2
Task 3
Solution
Android Kotlin
package com.example.appsizeoptimization

import android.os.Bundle
import android.widget.Button
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 optimizeButton: Button = findViewById(R.id.optimizeButton)
        val statusText: TextView = findViewById(R.id.statusText)

        optimizeButton.setOnClickListener {
            statusText.text = "Optimization Complete"
        }
    }
}

/* build.gradle (Module: app) snippet:
android {
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}
*/

We added a click listener to the 'Optimize' button that changes the status text to 'Optimization Complete' when clicked. This simulates the user triggering an optimization action.

In the build.gradle file, enabling minifyEnabled and shrinkResources activates code shrinking and resource shrinking using R8 and ProGuard. This reduces the app size by removing unused code and resources.

Unused drawable resources should be removed from the project folder to avoid increasing app size unnecessarily.

Final Result
Completed Screen
-------------------------
| App Size Optimization |
|-----------------------|
| [Optimize Button]     |
|                       |
| Status: Optimization   |
|         Complete      |
-------------------------
User taps the 'Optimize' button.
Status text changes from 'Ready' to 'Optimization Complete'.
Stretch Goal
Add a toggle switch to enable or disable code shrinking at runtime simulation.
💡 Hint
Use a Switch widget and update a TextView to show if shrinking is enabled or disabled.

Practice

(1/5)
1. What is the main purpose of enabling minifyEnabled in an Android app's build configuration?
easy
A. To add more debugging information
B. To increase the app's runtime speed
C. To remove unused code and reduce the app size
D. To enable multi-language support

Solution

  1. Step 1: Understand minifyEnabled role

    This setting activates code shrinking tools like ProGuard or R8 to remove unused code.
  2. Step 2: Effect on app size

    Removing unused code reduces the final APK or AAB size, making the app smaller.
  3. Final Answer:

    To remove unused code and reduce the app size -> Option C
  4. Quick Check:

    minifyEnabled = Remove unused code [OK]
Hint: minifyEnabled removes unused code to shrink app size [OK]
Common Mistakes:
  • Thinking it improves runtime speed directly
  • Confusing it with enabling debugging
  • Assuming it adds features like language support
2. Which of the following is the correct way to enable resource shrinking in your build.gradle file for an Android app?
easy
A. shrinkResources = true
B. shrinkResources()
C. shrinkResources: true
D. shrinkResources true

Solution

  1. Step 1: Check Gradle syntax for booleans

    In Gradle Groovy DSL, boolean properties are set with property true syntax.
  2. Step 2: Apply to shrinkResources

    The correct syntax is shrinkResources true inside the buildTypes block.
  3. Final Answer:

    shrinkResources true -> Option D
  4. Quick Check:

    Gradle boolean syntax = property true [OK]
Hint: Use `property true` syntax in build.gradle (Groovy) [OK]
Common Mistakes:
  • Using equals sign (Kotlin DSL)
  • Using colon instead of space
  • Calling it like a function
3. Given this build.gradle snippet, what will be the effect on the app size?
buildTypes {
  release {
    minifyEnabled true
    shrinkResources true
  }
}
medium
A. App size will increase due to added debugging info
B. App size will decrease by removing unused code and resources
C. App size will remain the same
D. App will fail to build due to syntax error

Solution

  1. Step 1: Analyze minifyEnabled true

    This enables code shrinking tools to remove unused code.
  2. Step 2: Analyze shrinkResources true

    This removes unused resources like images and strings from the APK.
  3. Step 3: Combined effect on app size

    Both together reduce app size by removing unused code and resources.
  4. Final Answer:

    App size will decrease by removing unused code and resources -> Option B
  5. Quick Check:

    minifyEnabled + shrinkResources = smaller app [OK]
Hint: Both flags together shrink code and resources [OK]
Common Mistakes:
  • Thinking it adds debugging info
  • Assuming no effect on size
  • Confusing syntax causing build failure
4. You enabled minifyEnabled true and shrinkResources true but your app crashes on startup. What is the most likely cause?
medium
A. You forgot to add ProGuard rules to keep necessary classes
B. You did not enable debugging mode
C. You used wrong syntax in build.gradle
D. You did not update your Android Studio version

Solution

  1. Step 1: Understand minify effect on code

    Minify removes unused code but can mistakenly remove needed classes if rules are missing.
  2. Step 2: Importance of ProGuard rules

    Proper rules keep essential classes from removal, preventing crashes.
  3. Final Answer:

    You forgot to add ProGuard rules to keep necessary classes -> Option A
  4. Quick Check:

    Missing ProGuard rules cause crashes after minify [OK]
Hint: Always add ProGuard rules when minifying [OK]
Common Mistakes:
  • Blaming debugging mode
  • Assuming syntax error without checking logs
  • Ignoring ProGuard configuration
5. You want to reduce your Android app size by removing unused resources but keep some images used only in code via reflection. How should you configure your build to avoid removing these images?
hard
A. Add keep rules in proguard-rules.pro to keep resource references
B. Disable shrinkResources and rely only on minifyEnabled
C. Rename the images to start with underscore to protect them
D. Use shrinkResources with resConfigs to specify languages only

Solution

  1. Step 1: Understand resource shrinking behavior

    Resource shrinking removes unused resources detected by static analysis.
  2. Step 2: Handling resources used via reflection

    Resources accessed dynamically may appear unused and get removed unless explicitly kept.
  3. Step 3: Use ProGuard keep rules

    Adding keep rules in proguard-rules.pro tells the shrinker to keep these resources.
  4. Final Answer:

    Add keep rules in proguard-rules.pro to keep resource references -> Option A
  5. Quick Check:

    Keep rules protect resources used dynamically [OK]
Hint: Use keep rules to protect resources used by reflection [OK]
Common Mistakes:
  • Disabling shrinkResources loses size benefits
  • Renaming files does not protect them
  • Using resConfigs only filters languages, not resources
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