How Matrix‑ApkChecker Cuts Android APK Size: A Deep Dive into Mobile Optimization
Matrix‑ApkChecker, an in‑house APM tool used by WeChat, analyzes Android APKs to identify oversized files, redundant resources, unused assets, and other inefficiencies, providing detailed reports and enabling targeted optimizations such as removing duplicate files, converting PNGs to WebP, and stripping unused libraries, ultimately reducing app size by over 130 KB.
Introduction
Matrix is an in‑house APM (Application Performance Management) system used by the WeChat client. Matrix‑ApkChecker, a component of Matrix, analyzes Android APK packages, applying a set of predefined rules to detect issues and generate detailed reports for problem analysis, version tracking, and size reduction.
Features
Read manifest information (package name, versionCode, etc.)
List files sorted by size, with optional suffix filtering
Count methods in dex files, grouped by class or package
Detect whether resources have been obfuscated (AndResGuard)
Find PNG files without an alpha channel for possible conversion to JPG
Check for multiple ABI versions of native libraries
Search for uncompressed file types
Count R class fields
Identify duplicate files
Detect multiple native libraries statically linking STL
Locate unused resources and assets
Find untrimmed native libraries
Example analysis
The following example demonstrates how Matrix‑ApkChecker evaluates a sample APK and guides size‑reduction actions.
Key findings include uncompressed PNG files, duplicate files, unused resources, and redundant assets.
Targeted optimizations
Delete duplicate files (e.g., remove icon.png and round.png from res/drawable), reducing PNG size by 23.89 KB.
Convert eligible PNGs to WebP, saving an additional 7.03 KB.
Remove unused .mp3 files from assets/music, saving 69.39 KB.
Delete unused resources, decreasing the arsc file by 36.99 KB.
Overall, the sample APK size was reduced by 137.3 KB.
Implementation principle
The workflow starts with UnzipTask to unpack the APK, perform global preparations (e.g., de‑obfuscation using mapping.txt and resMapping.txt), and collect file size statistics. Subsequent tasks run in parallel, each implementing a specific check:
ManifestAnalyzeTask : Parses AndroidManifest.xml using AXmlResourceParser to extract package name, version code, etc.
ShowFileSizeTask : Filters files by size and suffix, sorting results.
MethodCountTask : Uses Google’s com.android.dexdeps library to count methods in dex files.
ResProguardCheckTask : Determines if resources have been obfuscated by checking for the renamed r folder.
FindNonAlphaPngTask : Detects PNGs lacking an alpha channel via java.awt.BufferedImage.
MultiLibCheckTask : Checks for native libraries built for multiple ABIs.
CheckMultiSTLTask : Uses nm to see if native libraries statically link the STL.
CountRTask : Counts fields in generated R classes using com.android.dexdeps .
UncompressedFileTask : Compares pre‑ and post‑compression sizes to find uncompressed files.
DuplicatedFileTask : Identifies duplicate files by comparing MD5 hashes.
UnusedResourceTask : Reads R.txt, scans smali references, parses XML and resource.arsc to locate resources never used.
Each task outputs a JSON object, which can be formatted by OutputFormater into HTML or other custom formats.
Advantages
Distributed as an executable JAR, easy to integrate into CI pipelines (e.g., Jenkins).
Extensible via custom tasks inheriting from ApkTask.
Customizable output formats by extending TaskJsonResult or TaskHtmlResult and registering them.
Use in WeChat APM
WeChat’s terminal APM system employs Matrix‑ApkChecker to monitor APK size changes across versions, generate issue tickets, and provide optimization suggestions. Visual dashboards show size trends, per‑version issues, and detailed file‑type breakdowns.
Future plans
Matrix will be open‑sourced soon, and Matrix‑ApkChecker will be released as part of the project, inviting the community to adopt it for app quality monitoring and continuous improvement.
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.
