# Smaller APKs with resource optimization

DevFeed: [Smaller APKs with resource optimization](<https://devfeed.tech/articles/smaller-apks-with-resource-optimization-20973.md>)

Original publisher: [Read original article](<https://jakewharton.com/smaller-apks-with-resource-optimization/>)

Published: 2020-09-01T00:00:00Z

Content type: tutorial

Language: en

Sources: [Jake Wharton](<https://devfeed.tech/sources/jake-wharton.md>)

Topics: [Optimization](<https://devfeed.tech/topics/optimization.md>), [Android APK](<https://devfeed.tech/topics/android-apk.md>), [Android Gradle Plugin](<https://devfeed.tech/topics/android-gradle-plugin.md>), [Gradle](<https://devfeed.tech/topics/gradle.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-apk](<https://devfeed.tech/tags/android-apk.md>), [android-gradle-plugin](<https://devfeed.tech/tags/android-gradle-plugin.md>), [apk](<https://devfeed.tech/tags/apk.md>), [build](<https://devfeed.tech/tags/build.md>), [gradle](<https://devfeed.tech/tags/gradle.md>), [optimization](<https://devfeed.tech/tags/optimization.md>), [release](<https://devfeed.tech/tags/release.md>)

## AI overview

This article explains how resource names and paths are stored multiple times in an Android APK and shows how Android Gradle Plugin 4.2 resource optimization can reduce APK size. It uses a minimal Gradle-built app and APK inspection to compare the effects.

## Source excerpt

How many times does the name of a layout file appear in an Android APK? We can build a minimal APK with a single layout file to count the occurrences empirically. Building an Android app with Gradle requires only one thing: an AndroidManifest.xml file with a package. From there we can add a dummy layout whose contents are just <merge/> since we only care about its name. . ├── build.gradle └── src └── main ├── AndroidManifest.xml └── res └── layout └── home_view.xml Running gradle assembleRelease will produce a release APK measuring a paltry 2,118 bytes. We can dump its contents using xxd and look for home_view byte sequences. $ xxd build/outputs/apk/release/app-release-unsigned.apk ⋮ 000004c0: 0000 0074 0000 0018 0000 0072 6573 2f6c ...t.......res/l 000004d0: 6179 6f75 742f 686f 6d65 5f76 6965 772e ayout/home_view. 000004e0: 786d 6c63 66e0 6028 6160 6060 6490 61d0 xmlcf.`(a```d.a. ⋮ 00000570: 0000 0000 0000 0000 1818 7265 732f 6c61 ..........res/la 00000580: 796f 7574 2f68 6f6d 655f 7669 6577 2e78 yout/home_view.x 00000590: 6d6c 0000 0002 2001 f801 0000 7f00 0000 ml.... ......... ⋮ 00000700: 0000 0000 0909 686f 6d65 5f76 6965 7700 ......home_view. 00000710: 0202 1000 1400 0000 0100 0000 0100 0000 ................ ⋮ 00000870: 0000 ad04 0000 7265 732f 6c61 796f 7574 ......res/layout 00000880: 2f68 6f6d 655f 7669 6577 2e78 6d6c 504b /home_view.xmlPK ⋮ There are three uncompressed occurrences of the path and one uncompressed occurrence of only the name in the APK based on this output. If you have not read my post on calculating zip entry size or are not familiar with the structure of a zip file, a zip file is a list of file entries followed by a directory of all available entries. Each entry contains the file path and so does the directory. This accounts for the first occurrence (the entry header) and the last occurrence (the directory record) in the output. The middle two occurrences in the output are from inside the resources.arsc file which is a database of sorts for