Using ProGuard and R8 helps your Android app run faster and use less memory. They remove unused code and shrink your app, which means the app loads quicker and uses less battery. This helps keep your app smooth at 60 frames per second, especially on older devices.
ProGuard and R8 optimization in Android Kotlin - Build, Publish & Deploy
Enable R8 in your build to automatically shrink and optimize your code. Use minifyEnabled true in your build.gradle. Keep your ProGuard rules simple and only keep what you need. Avoid keeping large libraries if unused. Test your app after shrinking to catch any missing code that might cause crashes.
ProGuard and R8 reduce your app size by removing unused classes and methods. Smaller apps download faster and start quicker. This improves user experience and reduces data usage. A smaller app bundle also means faster installation and less storage used on the device.
ProGuard and R8 are Android-specific tools. iOS apps use different tools like Bitcode and Swift compiler optimizations. Android requires code shrinking because apps often include many libraries. iOS apps are usually smaller by default but use different app thinning methods.
Google Play requires apps to be signed and optimized for performance. Using R8 and ProGuard helps meet performance guidelines by reducing app size and improving speed. Make sure your app does not remove code needed for Google Play services or analytics, or it may fail review.
Your app takes 5 seconds to load this screen. What's likely wrong?
- You might not have enabled R8 or ProGuard, so your app is too large and slow to start.
- Your ProGuard rules might be too strict, removing needed code causing crashes or delays.
- You could have unused libraries bloating your app size and slowing startup.
Practice
minifyEnabled true in your Android app's release build?Solution
Step 1: Understand the role of
SettingminifyEnabledminifyEnabled trueactivates code shrinking and optimization tools like ProGuard and R8.Step 2: Identify the effect on app code
These tools remove unused code and optimize the remaining code to reduce app size and improve performance.Final Answer:
To shrink and optimize the app code by removing unused parts -> Option DQuick Check:
minifyEnabled true = shrink and optimize [OK]
- Thinking it increases debug info instead of reducing code
- Confusing minifyEnabled with disabling optimizations
- Assuming it generates UI layouts automatically
build.gradle for release builds?Solution
Step 1: Recall the standard syntax for ProGuard files
The correct syntax usesgetDefaultProguardFilefor the default file and then adds custom rules.Step 2: Match the correct order and file names
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' correctly callsgetDefaultProguardFile('proguard-android-optimize.txt')and then adds'proguard-rules.pro'.Final Answer:
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' -> Option CQuick Check:
Correct ProGuard file syntax = proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' [OK]
- Swapping file names or forgetting getDefaultProguardFile()
- Setting minifyEnabled false disables ProGuard
- Using wrong file extensions like .txt instead of .pro
-keep class com.example.MyClass { *; }What will happen to
MyClass during code shrinking?Solution
Step 1: Understand the
The-keeprule-keepdirective tells ProGuard to keep the specified class and its members without removing or renaming them.Step 2: Analyze the rule syntax
The rule-keep class com.example.MyClass { *; }means keep the entire class and all its members intact.Final Answer:
It will be kept intact with all its members -> Option AQuick Check:
-keep class = keep all members [OK]
- Thinking -keep removes unused classes
- Assuming only public methods are kept
- Confusing renaming with removal
minifyEnabled true but your app crashes at runtime due to missing classes. What is the most likely cause?Solution
Step 1: Identify the cause of missing classes
When code shrinking removes classes used only by reflection, runtime crashes occur.Step 2: Understand the role of
Adding-keeprules-keeprules for such classes prevents their removal during shrinking.Final Answer:
You forgot to add -keep rules for classes used via reflection -> Option BQuick Check:
Missing classes after shrinking = missing -keep rules [OK]
- Assuming minifyEnabled false causes missing classes
- Confusing file name errors with missing classes
- Thinking disabling R8 causes missing classes
com.example.models but allow R8 to rename their members to reduce size. Which ProGuard rule should you use?Solution
Step 1: Understand the difference between -keep and -keepnames
-keepprevents removal and renaming;-keepnamesprevents renaming but allows removal.Step 2: Match the requirement
You want to keep classes but allow member renaming, so-keepnameson classes is correct.Final Answer:
-keepnames class com.example.models.** -> Option AQuick Check:
Keep classes but rename members = -keepnames [OK]
- Using -keep which prevents renaming
- Confusing -keepclassmembers with -keepnames
- Assuming -keepclassmembernames keeps classes
Ready to prove it?
Pass the test and this pattern moves unknown
