Understanding and Applying Facebook’s Redex Bytecode Optimizer for Android APK Size and Startup Time Reduction
This article explains the principles and usage of Facebook’s open‑source Android bytecode optimizer Redex, presents its design, lists the optimization techniques it provides, and shares practical results of applying Redex (including Interdex) to the Anjuke app, highlighting the achieved APK size reduction and startup‑time improvements as well as the integration costs.
Introduction The article introduces Facebook’s open‑source bytecode optimizer Redex, describing its goal of reducing Android APK size and improving app startup speed, and sets the context of APK slimming and startup optimization in mobile development.
Background APK size reduction and startup time are common performance concerns. Conventional methods include optimizing native libraries, resources, and code. The author’s team, after standard optimizations, investigated Redex as a way to further shrink the DEX bytecode.
What is Redex Redex is an Android Bytecode Optimizer that operates on DEX files. Facebook reports roughly 25% size reduction and 30% cold‑start speedup, though actual gains depend on project specifics and integration effort.
Design Overview Redex works at the DEX level, enabling global optimizations such as class‑level linking and code consolidation. Its pipeline consists of independent, plug‑in‑style optimization passes that can be freely combined and configured.
Optimization Techniques Key optimizations include: 1. Obfuscation and compression of class/method names. 2. Function inlining to reduce call overhead. 3. Dead‑code removal via reachability analysis. 4. Class reordering (Interdex) based on startup class usage. 5. Interface removal when only one implementation exists. 6. Metadata stripping for unused annotations.
How to Use Redex 1. Build and install Redex (the author compiled it on macOS, noting toolchain challenges). 2. Configure the optimizer; default passes cover the techniques above. 3. Optionally enable Interdex, which requires a class‑order file generated from a startup trace (the provided script is outdated, so the author replaced the ClassLoader to capture the order). 4. After optimization, re‑sign and zip‑align the APK.
Optimization Results on Anjuke App Applying default + Interdex optimizations yielded: - APK size: ~10% reduction for debug builds, only ~100 KB for release builds. - Cold‑start time: ~20% improvement on Android 4.x devices, negligible change on Android 5.0+ (due to ART changes). The limited benefit is attributed to the small impact of Interdex on newer runtimes and the already‑optimized release APK.
Conclusion Redex’s integration cost (build‑process changes, stability concerns, debugging difficulty) outweighs its modest gains for the Anjuke project, leading to a decision not to adopt it directly. Nevertheless, individual passes—especially class reordering—remain valuable and can be incorporated into custom build pipelines.
References 1. Redex official documentation: https://fbredex.com/ 2. Open‑sourcing Redex: Making Android apps smaller and faster 3. Optimizing Android bytecode with Redex
Author Lu Jing, Android development engineer at the Real Estate Technology Department.
58 Tech
Official tech channel of 58, a platform for tech innovation, sharing, and communication.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.