This document summarizes challenges that large apps face with multidex and reducing APK size. It discusses how multidex allows apps to have multiple dex files if the methods exceed 65,536 or the dex file size is over 5MB. It also provides tips for using multidex in Gradle builds and compressing sub-dex files to reduce the APK size. Additionally, it suggests deferring sub-dex installation to a worker thread, moving splash activities to the main dex, and using techniques like resource proguarding, icon fonts, and image compression to further reduce the APK size.
5. Single Dex methods over 65536
will cause
“Conversion to Dalvik format failed”
at build time
6. Single Dex file size >5MB
will cause
INSTALL_FAILED_DEXOPT
before Android 2.3 at apk install time
7. “
Multidex build tools construct a
primary dex (classes.dex) and
supporting (classes2.dex, classes3.
dex) as needed. The build system
will then package them into an APK
file for distribution.
-Android Developer
11. “Programs are commonly written in Java
and compiled to bytecode for the Java
virtual machine, which is then translated
to Dalvik bytecode and stored in .dex
(Dalvik EXecutable).
-Wikipedia
15. “Android 5.0 and higher uses a runtime
called ART which natively supports
loading multiple dex files from
application APK files. ART scans for
classes(..N).dex files and compiles
them into a single .oat file at install
time.
-Android Developer
For Android 5.0 and higher
19. ● What’s the rule to divide dex
into primary dex and sub dex?
● How to install dex at runtime?
20. Primary dex should contain all classes have been
referenced before Mutidex.install() was called
Multidex.install()
21. ● What’s the rule to divide dex
into primary dex and sub dex?
● How to install dex at runtime?
22.
23. 1. Call DexPathList$makeDexElement() by reflection to force
Dalvik loads dex files
2. Get dexElements by reflection and write back extra elements by
System.arraycopy()
26. ● Reducing APK size by
compressing sub-dex
● Show splash activity before
install sub-dex
27. ● Inject compress dex task into build.gradle
○ Unpack Proguarded jar to get class files
○ Split class files into main-dex and sub-dexs.
○ Generate dex file from class file by “dx” build tool
○ Compress sub-dex files by lzma
○ Add compressed files into resource file
34. ● Resource Proguard(link)
○ Obfuscate resources id in APK
■ res/drawable/logo.png → r/s/a.png
■ Reduce resource table size in Resources.arsc
○ Repack Resources.arsc by 7-zip
● IconFont(link)
○ Merge multiple SVGs into one TTF
● TinyPNG(link)
○ Reduce PNG size by decreasing the number of colors in
the image