ProGuard is an open-source tool that shrinks, optimizes and obfuscates Java bytecode, removing unused classes and members, simplifying code paths and renaming identifiers to short meaningless names. In mobile performance work on Android, it was for many years the standard release build step for reducing app size and method count, and its rule file format still configures R8, the compiler that replaced it as the default in the Android Gradle Plugin.
ProGuard operates on compiled class files before they are converted to Dalvik bytecode, so it sees the whole program, including third-party libraries, and can delete code that the app never reaches.
How ProGuard processes an app
ProGuard runs a sequence of passes over the input class files and libraries, starting from a set of entry points that the developer declares as reachable. Everything not reachable from those entry points is considered dead code.
| Pass | What it does | Main benefit |
|---|---|---|
| Shrinking | Removes unreachable classes, fields and methods | Smaller APK and fewer methods |
| Optimization | Inlines methods, removes dead branches, merges classes | Less bytecode and faster execution paths |
| Obfuscation | Renames classes and members to short names such as a and b | Smaller string tables and harder reverse engineering |
| Preverification | Adds stack map information for Java ME and Java 6+ | Faster class loading on Java platforms |
Shrinking relies on static reachability analysis, often called tree shaking. ProGuard builds a call graph from the entry points, marks every class and member that can be reached, and discards the rest. On Android, preverification is unnecessary because the dex compiler performs its own verification, so Android builds normally disable that pass.
Keep rules and the configuration file
ProGuard is configured through a plain-text rule file, traditionally named proguard-rules.pro in Android projects. The most important rules tell the tool which code must survive shrinking and renaming because it is reached in ways static analysis cannot see.
| Directive | Effect | Typical use |
|---|---|---|
-keep | Preserves the class and listed members from removal and renaming | Classes created by reflection |
-keepclassmembers | Preserves members only if the class itself is kept | Serialized fields, enum methods |
-keepnames | Allows removal but prevents renaming | Classes looked up by name if used |
-dontwarn | Suppresses warnings about missing referenced classes | Optional library dependencies |
-printmapping | Writes the old-to-new name mapping file | Decoding obfuscated stack traces |
Libraries usually ship consumer rules inside their packages, which the build merges automatically. The developer adds rules only for app-specific reflection, JNI callbacks, custom views referenced from XML and data classes processed by serialization frameworks.
ProGuard and R8 in Android builds
Since Android Gradle Plugin 3.4, the default code shrinker has been R8, which performs shrinking, optimization, obfuscation and dexing in a single step. R8 reads the same ProGuard rule syntax, so most projects migrated without rewriting their configuration, and developers still speak of “ProGuard rules” when they mean R8 input.
| Tool | Output | Key characteristic |
|---|---|---|
| ProGuard | Optimized Java class files | Open source, general Java tool, separate step before dexing |
| R8 | Dex bytecode directly | Default Android shrinker, faster builds, more aggressive optimizations |
| DexGuard | Hardened dex bytecode | Commercial, adds encryption and runtime tamper protection |
In practice, R8 often produces smaller output than ProGuard because it optimizes with knowledge of the final dex format and the Android runtime. ProGuard remains relevant for non-Android Java applications, for desktop and server tools, and as the reference for rule semantics. R8 has a full mode that applies stricter assumptions and may require additional keep rules that classic ProGuard configurations did not need.
Effects on app size and runtime performance
The main performance gain from ProGuard-style processing is a smaller app size, which shortens downloads, reduces storage pressure and lowers install abandonment. Reductions depend heavily on how many libraries an app bundles; apps built on large frameworks often shrink their code by a third or more, while lean apps gain less.
Runtime effects are more modest but real. Fewer classes and methods mean less work for the class loader, lower memory used by method metadata and less pressure toward the 64K method reference limit of a single dex file. Inlining and dead branch removal can trim CPU time on hot paths, although on modern Android runtimes the ahead-of-time and just-in-time compilers already perform similar optimizations, so the startup improvement is usually incremental rather than dramatic. Baseline Profiles address startup more directly and work alongside shrinking.
Risks, debugging and common mistakes
The central risk of ProGuard is that code reached only dynamically gets removed or renamed, producing crashes that never appear in debug builds. The table lists typical symptoms and their remedies.
| Symptom | Likely cause | Fix |
|---|---|---|
| ClassNotFoundException in release only | Class loaded by reflection was removed | Add a targeted keep rule |
| JSON fields arrive empty or null | Serialized field names were obfuscated | Keep field names or use annotations |
| Native crash in JNI callback | Java method called from C code was renamed | Keep the native interface methods |
| Unreadable crash reports | Mapping file not uploaded | Archive and upload mapping.txt for each release |
- Overly broad keep rules, such as keeping entire packages, silently cancel most of the size benefit.
- Losing the mapping file makes obfuscated stack traces permanently unreadable; store it with every build.
- Treating obfuscation as security is a mistake, because renaming only slows reverse engineering and does not protect secrets in the binary.
- Testing only debug builds hides shrinker problems; release variants need their own automated tests.
In plain words
ProGuard is like packing a suitcase with a strict checklist. Before a trip, it looks at everything the app might carry, keeps only what is actually used and throws out the rest. It also relabels the items with short codes so the suitcase is lighter and harder for strangers to sort through.
The result is a smaller, tidier app that downloads faster and takes less space on the phone. The one danger is that it may throw away something needed only in unusual moments, so developers write a list of items that must always stay packed.
A real-world example
A small team maintains a fitness tracking app for Android that bundles a charting library, a networking stack and an analytics SDK. Their release APK has grown to about 38 MB, and store data shows that many users in regions with slow networks abandon the download. The team enables code shrinking and obfuscation with the standard optimized default rules plus a few keep rules for their JSON model classes.
The first release build crashes on the workout history screen because a class instantiated by reflection was removed. After adding one targeted keep rule and a release-variant UI test, the build passes. The code portion of the APK shrinks by roughly 40 percent, total size falls to about 29 MB, and the team uploads the mapping file to its crash reporting service so that obfuscated stack traces remain readable.