Upload
chih-chung-lee
View
1.995
Download
0
Embed Size (px)
Citation preview
大型App面臨的挑戰
- Multidex探究與Apk size最佳化
2016/04/21 @ Android Taipei
Hello!I am Johnny Lee
Software Engineer at XPEC, HTC and Leopard MobileYou can find me at facebook.com/johnnnielee
Outline● Multidex● 衍生應用
○ Compressing sub-dex○ Install dex in worker thread
● Tips for reducing APK file size
Multidex
Single Dex methods over 65536
will cause
“Conversion to Dalvik format failed”
at build time
Single Dex file size >5MB
will cause
INSTALL_FAILED_DEXOPT
before Android 2.3 at apk install time
“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
What is dex file?
Java code → Java bytecode → Dalvik executable (DEX)
“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
How to use it?
STEP1: Change your Gradle build
configuration to enable multidex
Below Android 5.0
STEP2: Making application extends MultiDexApplication
Prior to Android 5.0
“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
身為一個專業的Android工程師...
What’s the secret behind
Multidex.install()?
● What’s the rule to divide dex into primary dex and sub dex?
● How to install dex at runtime?
Primary dex should contain all classes have been referenced before Mutidex.install() was called
Multidex.install()
● What’s the rule to divide dex into primary dex and sub dex?
● How to install dex at runtime?
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()
衍生應用
● Reducing APK size by compressing sub-dex
● Show splash activity before install sub-dex
● 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
8.64MB→ 7.79MB
● Modify Multidex.install()○ Decompress zipped sub-dex○ Install sub-dex
● Reducing APK size by compressing sub-dex
● Show splash activity before install sub-dex
● Defer install sub-dex process into worker thread
● Move splash activity related classes into maindex
● Show splash activity
Other tips for reducing APK file size
● 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
Thanks!Any questions?You can find me at facebook.com/johnnnielee