How Android Resource Obfuscation Shrinks APK Size: Techniques & Implementation
This article explains how WeChat's resource obfuscation tool, AndResGuard, renames and compresses Android resources, modifies the resources.arsc binary, and combines 7‑zip compression to reduce APK size by about 1 MB while increasing anti‑tampering difficulty.
Introduction
In a previous article we discussed several tips for reducing Android APK size; this piece focuses on resource obfuscation, a technique used by WeChat to shorten resource IDs (e.g., converting
res/drawable/welcome.pngto
r/s/a.png) and apply deep 7‑zip compression, thereby shrinking the package and raising anti‑reverse‑engineering barriers.
Background
The resource‑obfuscation tool was first implemented in April 2014 and shipped in WeChat 5.4, saving roughly 1 MB of space. It was open‑sourced internally in August and later adopted by QQ Mail, QQ Space, Mobile Manager, and other products. The source code and usage instructions are available on GitHub: https://github.com/shwenzhang/AndResGuard .
Evolution of the Solution
Resource obfuscation aims to rename resources such as
res/drawable/icon.pngto
res/drawable/a.png, or even rename the entire path to
r/s/a.png.
<code>Proguard -> Resource Proguard R.string.name -> R.string.a res/drawable/icon -> res/drawable/a r/s/a</code>
Three possible implementation schemes are considered:
Follow Proguard’s approach: modify source code and XML, rename files (e.g., icon.png to a.png ), then let the Android build system handle the rest.
Since resource IDs are compiled into 32‑bit integers, we can directly edit the binary resources.arsc after compilation, leaving the packaging process untouched.
Process the APK directly without source or compilation steps: input an APK and output an obfuscated APK.
WeChat adopts the third scheme. The key challenge is how to modify
resources.arscto achieve the desired obfuscation.
Technical Implementation
The
resources.arscfile consists of five chunk types: TYPETABLE , TYPEPACKAGE , TYPE_STRING , TYPETYPE , and TYPECONFIG . Understanding each chunk is essential for safe modification.
Table chunk : Marks the start of the entire resources table; its
chunksizeequals the file size.
Package chunk : Begins a package; multiple packages can exist. The packageID occupies the high eight bits of a resource ID (system Android uses 0x01, typical apps like
com.tencent.mmuse 0x7f).
String block : Three kinds exist—table stringblock, typename stringblock, and specsname stringblock.
Type chunk : Contains type definitions such as
attr,
drawable,
layout,
id,
color,
anim. The Type ID follows the Package ID.
Config chunk : Describes resource configurations (orientation, density, language, etc.). Each type may have multiple config chunks.
Entry : Although not a separate chunk, each config contains many entries (e.g.,
icon1.png,
icon2.pngfor
drawable‑mdpi).
Modification Plan
1. Table stringblock : Change file paths, e.g.,
res/layout/test.xml→
res/layout/a.xml.
2. Resource file names : Rename files accordingly, e.g.,
test.xml→
a.xml.
3. Specsname stringblock : Replace all non‑whitelisted specsname entries with short alphanumeric strings (
[a‑z0‑9_]) to reduce size.
4. Entry IDs : Update the IDs in the
specsnameentries to point to the new obfuscated names.
5. Table chunk size : Adjust the final size field of the table chunk to reflect the modifications.
Combining with 7‑Zip Extreme Compression
Using 7‑zip yields a higher compression ratio, especially when we force compression of files that Android normally leaves uncompressed (e.g.,
resources.arsc,
.png,
.jpg). After modifying
resources.arsc, we re‑package the APK; the whole process takes about 35 seconds, far less than the previously quoted 799 ms or 699 ms.
Overall Processing Flow
Important Considerations
compress parameter : Specifying .png , .gif , and .jpg for compression greatly reduces APK size; for Android 2.2 compatibility, resources.arsc should stay under 1 MB before compression.
OS impact on 7‑zip : Linux and macOS produce better compression results than Windows.
keepmapping : Using the keepmapping option has little impact on incremental APK size but helps maintain consistent obfuscation across versions.
Channel packages : Re‑compressing a channel package can break 7‑zip’s effect; use the repackage command to apply 7‑zip again.
getIdentifier whitelist : Resources accessed via getIdentifier must be placed on a whitelist; similarly, desktop shortcut icons should be whitelisted.
TODO: Beyond resource obfuscation, we can also explore resource encryption and other protection mechanisms.
WeChat Client Technology Team
Official account of the WeChat mobile client development team, sharing development experience, cutting‑edge tech, and little‑known stories across Android, iOS, macOS, Windows Phone, and Windows.
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.