Mobile Development 20 min read

Baidu App APK Size Optimization: Principles, Tools, and Implementation

The Baidu App team applied a systematic, sustainable approach—combining Dex, resource, and native‑library optimizations with tools such as R8, ProGuard, AndResGuard, and custom compression pipelines—to shrink the APK by several megabytes, improve download conversion, and establish reusable mechanisms for future builds.

Baidu App Technology
Baidu App Technology
Baidu App Technology
Baidu App APK Size Optimization: Principles, Tools, and Implementation

Previously, Baidu App already had basic package size optimization mechanisms, but as a large‑scale app the rapid iteration of business features inevitably leads to explosive growth of the APK size.

Package size directly influences key metrics such as download conversion rate, installation time, runtime memory and disk space. According to internal Google Play data, reducing the APK size by 10 MB can increase the download conversion rate by about 1.5% (see Fig 1).

Android package size can be reduced by many means—business slicing, modularization, hybrid development, resource on‑demand delivery, etc. This series focuses on the "basic mechanism" optimizations that are unrelated to business logic but are embedded in the APK, such as Dex optimization, resource optimization, and native library (so) optimization.

The series will be presented in several parts: mindset, full Dex optimization plan, resource optimization practice and exploration, Dex optimization practice and exploration, so optimization exploration, and other optimization experiences and summaries.

Basic ideas

Divide and conquer : The optimization target is not only the final APK file but also the contents inside the APK, which require different optimization approaches.

Sustainable optimization : Good optimization mechanisms should work not only for the current build but also for future builds. For example, removing dead code from the source repository is a one‑time reduction, while a compiler‑level Dead Code Elimination (DCE) continues to eliminate dead code generated later.

Standing on the shoulders of giants : Package size optimization is not new; Android official tools and the community have long been working on it. However, for large‑scale apps a customized solution is necessary.

Clear trade‑offs : According to the first law of thermodynamics, any gain comes with a cost (e.g., human effort, longer compile time, higher integration difficulty). Understanding the cost helps decide which optimizations to apply.

Constraints and awareness : In addition to automated mechanisms, a complementary constraint system and developer awareness are required to achieve the best results.

APK structure analysis

classes.dex – one or more Dex files containing compiled Java/Kotlin bytecode.

resources.arsc – resource table linking resource IDs to actual resources.

res/ – compiled resources (except values) referenced by the resource table.

lib/ – native libraries (so files) organized by ABI.

assets/ – raw assets not indexed in resources.arsc.

META-INF/ – signing information (MANIFEST.MF, CERT.SF, CERT.RSA).

AndroidManifest.xml – application manifest describing package name, components, permissions, etc.

ZIP structure analysis

Local file header – describes each source file.

File data – the raw file content.

Data descriptor – checksum and size information.

Central directory – records the ZIP directory entries.

End of central directory record – marks the end of the ZIP archive.

Existing optimization tools

ProGuard : Before AGP 3.3, ProGuard performed class file shrinking, obfuscation, and instruction‑level optimization before Dex conversion.

R8 : Since AGP 3.3, R8 replaces ProGuard, integrating D8 and desugaring, offering faster builds and similar shrinking/obfuscation capabilities.

AndResGuard : A resource‑obfuscation tool from WeChat that also provides compression and encryption options.

ByteX : Bytecode instrumentation framework from ByteDance, used for R‑class inlining, debug‑info removal, access‑method inlining, etc.

Booster : Didi’s quality‑optimization framework covering resource compression, .ap_ compression, redundant resource removal, R‑class inlining, DataBinding BR inlining, etc.

Android Gradle Plugin (AGP) tasks :

OptimizeResources – shortens resource file paths (enabled by default).

StripSymbols – removes unneeded symbols from native libraries via llvm‑strip.

MinifyWithR8/ProGuard – performs code shrinking/obfuscation.

ShrinkResources – replaces unused resources with tiny placeholders (requires minifyEnabled).

PackageOptions – controls filtering, merging, and stripping of files.

Splits – configures ABI and resource splits.

Baidu App specific optimization items

Dex optimization – divided into compile‑time (using bytecode tools such as ASM) and packaging‑time (using Dex tools like Titan‑Dex). Highlights include:

R‑class optimization: replace R.type.name usages with constant IDs so the R class can be removed by ProGuard/R8.

Line‑number optimization: remove debug info by dropping -keepattributes SourceFile,LineNumberTable .

Annotation optimization: remove System annotations via -keepattributes *Annotation* and -keepattributes InnerClasses,Signature,EnclosingMethod .

Resource optimization – targets both the resources.arsc table and raw resource files:

Resource name unification: collapse all resource names to a single string in the string pool.

Resource path shortening: hash‑based path obfuscation and removal of most file extensions.

Resource configuration optimization: reduce unnecessary configuration entries to shrink alignment padding.

Image compression: compress PNGs using TinyPNG and ImageOptim (WebP is limited to minSdkVersion 18).

Unused resource cleaning: combine ShrinkResources output with custom tools to truly delete unused files.

New ShrinkResource (AGP 7.1.0): precise shrinking removes unused files and values, but arsc placeholders remain.

Arsc compression: not applied because compressed arsc prevents mmap loading and increases memory usage; Android 11 enforces uncompressed, 4‑byte aligned arsc.

ZIP optimization – uses 7z and zopfli compression algorithms (zopfli offers higher ratio but longer time). Important notes: do not compress resources.arsc and keep correct order of compression, alignment, and signing.

Other optimizations :

Night‑mode resource package: switched from reflection‑based ID lookup to direct ID matching, enabling resource‑APK size reduction.

ProGuard/R8 rule management: plan to define a strict rule usage guideline and validate each component’s keep rules.

Volume pipeline: automated pipelines for package size constraints, binary component size checks, and APK composition analysis, with real‑time monitoring of size regressions.

Results

The table below shows before/after sizes for each optimization item (all sizes in MB):

Optimization Item

Before

After

Diff

Dex line‑number optimization & rule convergence (released)

123.58

119.37

4.21

Resource name/path optimization, 7z compression, night‑mode (released)

122.54

116.36

6.18

Image compression & hot‑fix instrumentation (released)

117.07

116.00

1.07

Dex annotation optimization (gray‑release)

117.16

115.95

1.21

Enable R8 (gray‑release)

119.06

116.62

2.44

zopfli compression, resource shrinking, configuration optimization (gray‑release)

119.57

116.65

2.92

Conclusion

This article introduced the overall thinking behind Baidu App’s APK size optimization, dissected the optimization targets, presented the existing tools, and summarized the concrete optimization items and their gains. Future work will detail the principles and implementations of each optimization type and gradually open‑source the generic tools.

AndroidR8APKDexresourcesProGuardsize optimization
Baidu App Technology
Written by

Baidu App Technology

Official Baidu App Tech Account

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.