35
大型App面臨的挑戰 - Multidex探究與Apk size最佳化 2016/04/21 @ Android Taipei

大型App面臨的挑戰

Embed Size (px)

Citation preview

Page 1: 大型App面臨的挑戰

大型App面臨的挑戰

- Multidex探究與Apk size最佳化

2016/04/21 @ Android Taipei

Page 2: 大型App面臨的挑戰

Hello!I am Johnny Lee

Software Engineer at XPEC, HTC and Leopard MobileYou can find me at facebook.com/johnnnielee

Page 3: 大型App面臨的挑戰

Outline● Multidex● 衍生應用

○ Compressing sub-dex○ Install dex in worker thread

● Tips for reducing APK file size

Page 4: 大型App面臨的挑戰

Multidex

Page 5: 大型App面臨的挑戰

Single Dex methods over 65536

will cause

“Conversion to Dalvik format failed”

at build time

Page 6: 大型App面臨的挑戰

Single Dex file size >5MB

will cause

INSTALL_FAILED_DEXOPT

before Android 2.3 at apk install time

Page 7: 大型App面臨的挑戰

“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

Page 8: 大型App面臨的挑戰
Page 9: 大型App面臨的挑戰

What is dex file?

Page 10: 大型App面臨的挑戰

Java code → Java bytecode → Dalvik executable (DEX)

Page 11: 大型App面臨的挑戰

“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

Page 12: 大型App面臨的挑戰

How to use it?

Page 13: 大型App面臨的挑戰

STEP1: Change your Gradle build

configuration to enable multidex

Below Android 5.0

Page 14: 大型App面臨的挑戰

STEP2: Making application extends MultiDexApplication

Prior to Android 5.0

Page 15: 大型App面臨的挑戰

“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

Page 16: 大型App面臨的挑戰
Page 17: 大型App面臨的挑戰

身為一個專業的Android工程師...

Page 18: 大型App面臨的挑戰

What’s the secret behind

Multidex.install()?

Page 19: 大型App面臨的挑戰

● What’s the rule to divide dex into primary dex and sub dex?

● How to install dex at runtime?

Page 20: 大型App面臨的挑戰

Primary dex should contain all classes have been referenced before Mutidex.install() was called

Multidex.install()

Page 21: 大型App面臨的挑戰

● What’s the rule to divide dex into primary dex and sub dex?

● How to install dex at runtime?

Page 22: 大型App面臨的挑戰
Page 23: 大型App面臨的挑戰

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()

Page 24: 大型App面臨的挑戰
Page 25: 大型App面臨的挑戰

衍生應用

Page 26: 大型App面臨的挑戰

● Reducing APK size by compressing sub-dex

● Show splash activity before install sub-dex

Page 27: 大型App面臨的挑戰

● 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

Page 28: 大型App面臨的挑戰

8.64MB→ 7.79MB

Page 29: 大型App面臨的挑戰

● Modify Multidex.install()○ Decompress zipped sub-dex○ Install sub-dex

Page 30: 大型App面臨的挑戰

● Reducing APK size by compressing sub-dex

● Show splash activity before install sub-dex

Page 31: 大型App面臨的挑戰

● Defer install sub-dex process into worker thread

● Move splash activity related classes into maindex

● Show splash activity

Page 33: 大型App面臨的挑戰

Other tips for reducing APK file size

Page 34: 大型App面臨的挑戰

● 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

Page 35: 大型App面臨的挑戰

Thanks!Any questions?You can find me at facebook.com/johnnnielee