App size optimization helps make your app smaller. Smaller apps download faster and use less space on phones.
App size optimization in Android Kotlin
android {
defaultConfig {
}
buildTypes {
release {
// Enable shrinking and obfuscation
minifyEnabled true
// Remove unused resources
shrinkResources true
// Use ProGuard or R8 rules
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}minifyEnabled removes unused code and obfuscates names.
shrinkResources removes unused resources like images and strings.
minifyEnabled false shrinkResources false
minifyEnabled true shrinkResources false
minifyEnabled true shrinkResources true
This is a minimal Android app setup with code and resource shrinking enabled for release builds. It uses a simple built-in layout to keep size small.
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdk 33
defaultConfig {
applicationId "com.example.appsize"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
// Simple MainActivity.kt
package com.example.appsize
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(android.R.layout.simple_list_item_1)
}
}Always test your app after enabling shrinking to avoid removing needed code or resources.
Use ProGuard or R8 rules to keep important code from being removed.
Large images can be compressed or replaced with vector graphics to save space.
Enable minifyEnabled and shrinkResources in your build config to reduce app size.
Use ProGuard or R8 to remove unused code and rename classes.
Test your app carefully after optimization to ensure it still works well.
Practice
minifyEnabled in an Android app's build configuration?Solution
Step 1: Understand
This setting activates code shrinking tools like ProGuard or R8 to remove unused code.minifyEnabledroleStep 2: Effect on app size
Removing unused code reduces the final APK or AAB size, making the app smaller.Final Answer:
To remove unused code and reduce the app size -> Option CQuick Check:
minifyEnabled = Remove unused code [OK]
- Thinking it improves runtime speed directly
- Confusing it with enabling debugging
- Assuming it adds features like language support
build.gradle file for an Android app?Solution
Step 1: Check Gradle syntax for booleans
In Gradle Groovy DSL, boolean properties are set withproperty truesyntax.Step 2: Apply to
The correct syntax isshrinkResourcesshrinkResources trueinside the buildTypes block.Final Answer:
shrinkResources true -> Option DQuick Check:
Gradle boolean syntax = property true [OK]
- Using equals sign (Kotlin DSL)
- Using colon instead of space
- Calling it like a function
build.gradle snippet, what will be the effect on the app size?
buildTypes {
release {
minifyEnabled true
shrinkResources true
}
}Solution
Step 1: Analyze
This enables code shrinking tools to remove unused code.minifyEnabled trueStep 2: Analyze
This removes unused resources like images and strings from the APK.shrinkResources trueStep 3: Combined effect on app size
Both together reduce app size by removing unused code and resources.Final Answer:
App size will decrease by removing unused code and resources -> Option BQuick Check:
minifyEnabled + shrinkResources = smaller app [OK]
- Thinking it adds debugging info
- Assuming no effect on size
- Confusing syntax causing build failure
minifyEnabled true and shrinkResources true but your app crashes on startup. What is the most likely cause?Solution
Step 1: Understand minify effect on code
Minify removes unused code but can mistakenly remove needed classes if rules are missing.Step 2: Importance of ProGuard rules
Proper rules keep essential classes from removal, preventing crashes.Final Answer:
You forgot to add ProGuard rules to keep necessary classes -> Option AQuick Check:
Missing ProGuard rules cause crashes after minify [OK]
- Blaming debugging mode
- Assuming syntax error without checking logs
- Ignoring ProGuard configuration
Solution
Step 1: Understand resource shrinking behavior
Resource shrinking removes unused resources detected by static analysis.Step 2: Handling resources used via reflection
Resources accessed dynamically may appear unused and get removed unless explicitly kept.Step 3: Use ProGuard keep rules
Adding keep rules inproguard-rules.protells the shrinker to keep these resources.Final Answer:
Addkeeprules inproguard-rules.proto keep resource references -> Option AQuick Check:
Keep rules protect resources used dynamically [OK]
- Disabling shrinkResources loses size benefits
- Renaming files does not protect them
- Using resConfigs only filters languages, not resources
Ready to prove it?
Pass the test and this pattern moves unknown
