Mastering Android Resource Management: From Classification to Optimization
This article delves into Android resource fundamentals, classification, referencing, compilation, shrinking, and practical governance techniques, illustrating how to detect and resolve conflicts, unused assets, missing class references, and hard‑coded texts to improve build size and runtime stability.
Overview
This is the fourth article in the "Firing at Engineering Corruption" series, focusing on Android resources and how to combat resource‑related decay.
1. Basics of Android Resources
Android resources are divided into Resource (structured, ID‑based) and Asset (raw file) categories. Resources provide a controlled access mechanism with unique IDs and configuration qualifiers for multi‑language, multi‑device, and feature support, while assets are accessed directly via file paths.
1.1 Resource Classification
The official documentation defines 24 resource types. All of them can be referenced in Java code using the R.<type>.<name> syntax, and some can also be accessed via @<type>/<name> in manifests and XML files.
1.2 Resource Referencing
References can be direct (compile‑time) or indirect/dynamic (runtime). They appear in Java code, the AndroidManifest.xml, or other resource files. Dynamic references (e.g., Resources.getIdentifier) provide flexibility but incur a lookup cost.
1.3 Resource Compilation
During the build, resources are merged, then processed by AAPT2, producing several artifacts:
Compiled AndroidManifest.xml with resource IDs.
resources.arsc – a symbol table mapping IDs to values.
Compiled resource files (binary layouts, drawables, etc.).
R.java – static fields for each resource.
Keep‑rule files for ProGuard.
1.4 Resource Shrinking
AGP’s resource‑shrinking feature removes unused resources by tracing references from the manifest and Java code. Because of dynamic references, the algorithm is conservative: any string constant that matches a resource name is treated as a reference, which can lead to missed or false‑positive removals. A whitelist and a strict mode are provided to mitigate these issues.
1.5 Interesting Technical Points
Using system android:* attributes avoids generating duplicate IDs, saving space.
Identical attr names across modules are de‑duplicated, improving reuse. id resources are globally reusable; only the maximum number needed per layout is required.
2. Governance Practices
2.1 Conflict Resources
When different modules contain resources with the same name but different values, the build keeps only one (order‑dependent), causing nondeterministic behavior. A conflict‑detection tool lists such resources, allowing teams to resolve them before they cause runtime issues.
[conflict] drawable/al_down_arrow
|-- xhdpi-v4
|-- md5:cc2ef446bf586b03fd08332a5a75b304 (com.ali.user.sdk:au:4.10.6.18)
|-- md5:5f9c59ec3fba027c5783120effa12789 (com.ta.android:lo4android:4.10.6.18)
[conflict] string/str_retry
|-- en
|-- not calculated (com.ali.android.phone:bee-build:10.2.3.358)
|-- default
|-- 重试 (com.ali.android.phone:photo-build:10.2.3.57)
|-- 点击重试 (com.ali.android.phone:bee-build:10.2.3.358)The tool also supports ignore‑lists and can fail the build when conflicts are detected.
2.2 Unused Resources
Unused resources are those not reachable from any Java code or manifest entry. The detection respects dynamic references and produces a list of candidates for removal, which contributes to APK size reduction.
project:app:1.0
|-- array/planets_array
|-- color/white
|-- drawable/fake_drawable
|-- layout/layout_miss_view
|-- raw/app_resource_raw_chinese_text
|-- string/string_resource_chinese_name
|-- xml/app_resource_xml_chinese_text
project:library-aar-1:1.0
|-- layout/layout_contain_merge
|-- string/library_aar_1_name2.3 Missing Class References
Layouts may reference custom views whose classes are absent from the final DEX. This does not break the build but causes runtime crashes when the layout is inflated. Detection reports the offending layout and missing class.
* [ignored] layout-xxxhdpi/layout_include_layout (project:library-aar-1:1.0)
|-- com.example.libraryaar1.NonExistCustomView
* layout/layout_miss_view (project:app:1.0, project:library-aar-1:1.0)
|-- com.example.myapplication.NonExistView2
|-- com.example.myapplication.NonExistView2.4 Hard‑Coded Text
Strings directly embedded in resources can cause privacy compliance issues and hinder localization. A regex‑based scanner extracts Chinese characters (or any pattern) from strings, arrays, raw files, assets, etc., helping teams locate and refactor hard‑coded texts.
project:app:1.0
|-- array/planets_array [text] string‑array contains Chinese item
|-- raw/app_resource_raw_chinese_text [text] 我是raw类型xml资源文件中,包含的中文文本
|-- string/string_resource_chinese_name [text] 我是中文string资源
|-- xml/app_resource_xml_chinese_text [text] 我是xml资源中的中文文本
|-- layout/activity_main [text] android:text="你好,世界!"
project:library-aar-1:1.0
|-- asset/library_aar_1_asset_chinese_text [text] 我是包含中文文本的asset资源文件.2.5 Full Governance Panorama
Combining conflict detection, unused‑resource analysis, missing‑class checks, and hard‑coded‑text scanning provides a comprehensive anti‑corruption pipeline for Android resources, significantly reducing APK size and improving runtime reliability.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
