ProGuard and R8 help make your Android app smaller and faster by removing unused code and optimizing what remains.
ProGuard and R8 optimization in Android Kotlin
android {
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}minifyEnabled true turns on code shrinking and optimization.
proguardFiles lists the configuration files for rules to keep or remove code.
minifyEnabled false
minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'my-proguard-rules.pro'
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}This example shows a simple Android app with ProGuard and R8 enabled for release builds. The ProGuard rules keep the main activity so it is not removed. When you build the release version, unused code is removed and the app size is smaller.
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.proguarddemo'
compileSdk 33
defaultConfig {
applicationId 'com.example.proguarddemo'
minSdk 21
targetSdk 33
versionCode 1
versionName '1.0'
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
// proguard-rules.pro
# Keep MainActivity class
-keep class com.example.proguarddemo.MainActivity { *; }
// MainActivity.kt
package com.example.proguarddemo
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val textView = TextView(this)
textView.text = "Hello ProGuard and R8!"
setContentView(textView)
}
}R8 is the default code shrinker and optimizer in Android Studio now, replacing ProGuard but still using ProGuard rules.
Always test your app after enabling minify to make sure important code is not removed.
You can add custom rules in proguard-rules.pro to keep code needed by reflection or libraries.
ProGuard and R8 help shrink and optimize your Android app code.
Enable minifyEnabled true in release build to activate them.
Use ProGuard rules files to control what code to keep or remove.
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
