Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reducing APK size

Reducing APK size

A few tips and techniques to make your Android applications lighter.

Christophe Beyls

June 23, 2016
Tweet

More Decks by Christophe Beyls

Other Decks in Programming

Transcript

  1. Why reduce the APK size? - Faster download times and

    less consumed data. (slow connections, costly/limited data fees) - Smaller install size on the device. (most entry-level phones only have 8 Gb of storage)
  2. Also... Less bytecode means - Faster application startup (classloader). Especially

    when avoiding multidex. - Much smaller install size on the device. Lollipop & Marshmallow install size = 3-4x apk size - Faster installation on Lollipop & Marshmallow.
  3. Inside an APK file - classes.dex: application bytecode. Sometimes more

    than one file (multidex). - lib/: folder with native libraries. Optional. - res/: folder with resources files. Images + binary XML files. (layouts, selector drawables, menus, anims, …) - resources.arsc: compiled resources tables - strings, colors, dimens, styles, ids. Uncompressed for faster access.
  4. 1. Optimize your dependencies Never include the full version of

    Google Play Services. dependencies { compile 'com.google.android.gms:play-services:9.0.2' } Only declare the modules you need. dependencies { compile 'com.google.android.gms:play-services-maps:9.0.2' compile 'com.google.android.gms:play-services-location:9.0.2' compile 'com.google.android.gms:play-services-gcm:9.0.2' }
  5. 1. Optimize your dependencies Choose lighter libraries. www.methodscount.com Example Dex

    Size: - Picasso 2.5.2 106 KB - Glide 3.7.0 403 KB - Fresco 0.9.0 1483 KB If possible, avoid libraries requiring large native binaries (.so files). Examples: Fresco, Realm.
  6. 2. Use Proguard for release builds android { buildTypes {

    release { minifyEnabled true proguardFiles getDefaultProguardFile(‘proguard-android. txt'), 'proguard-rules.pro' } } } - Big apps: multidex for debug, Proguard for release. - Add Proguard rules (if any) for each library you’re using. Many libraries already include Proguard rules inside their .aar file. - Avoid using reflection in your code. Else, add Proguard rules for it.
  7. 2. Use Proguard for release builds “I can’t read my

    stack traces anymore” 1. Lazy: add Proguard rule -dontobfuscate 2. Better: upload [app]/build/outputs/mapping/release/mapping.txt to Google Play Developer Console
  8. If your app includes native binaries (.so files) 3. Split

    APK by CPU architecture Create one APK file for each CPU architecture. android { ... splits { abi { enable true reset() include 'x86', 'armeabi-v7a', 'mips' universalApk false } } }
  9. 4. Remove unused resource files Very useful to remove unused

    parts of libraries. Requires Proguard. android { buildTypes { release { minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android. txt'), 'proguard-rules.pro' } } }
  10. 5. Optimize images - Manually optimize PNG (and JPEG) files

    using tools like Mac OS X: ImageOptim Linux: Trimage Windows: FileOptimizer. - Then disable automatic PNG optimization. Because it makes already optimized files bigger (issue 65335). android { aaptOptions { cruncherEnabled false } }
  11. 5. Optimize images - Replace JPEG files with lossy WebP

    files (Android 4+). Same quality, 33% smaller file size. - Replace small PNG icons with Vector Drawables and enable full VectorDrawable support in the support library 23.2+: android { defaultConfig { vectorDrawables.useSupportLibrary = true } } - More image optimizations: medium.com/@duhroach
  12. 6. Remove unsupported locales - Libraries like AppCompat or Google

    Play Services include many strings in 75+ languages. - Specify the locales you support, the build tool will remove the rest. - Expect gaining a few hundred kilobytes. android { defaultConfig { ... resConfigs "en", "fr", "nl" } }