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

Optimize APK Size

Optimize APK Size

How to optimize APK size and why it so important for your product

Kirill Rozov

August 07, 2018
Tweet

More Decks by Kirill Rozov

Other Decks in Education

Transcript

  1. Kirill Rozov
    Lead Android Developer
    Optimize APK size

    View full-size slide

  2. medium.com/googleplaydev/shrinking-apks-growing-installs-5d3fcba23ce2
    Average downloaded APK size

    View full-size slide

  3. What for?
    Download APK size
    0
    5
    10
    15
    20
    25
    Size from Google Play for Xiaomi Mi A1 Android 8.0

    View full-size slide

  4. Why it’s problem?

    View full-size slide

  5. Why it’s problem?
    • Support low-end devices

    View full-size slide

  6. Why it’s problem?
    • Support low-end devices
    • Low space on device memory

    View full-size slide

  7. Why it’s problem?
    • Support low-end devices
    • Low space on device memory
    • How long time it will take to download app

    I need it right now!!!

    View full-size slide

  8. Why it’s problem?
    • Support low-end devices
    • Low space on device memory
    • How long time it will take to download app

    I need it right now!!!
    • No Wi-Fi

    View full-size slide

  9. Why it’s problem?
    • Support low-end devices
    • Low space on device memory
    • How long time it will take to download app

    I need it right now!!!
    • No Wi-Fi
    • Cellular data costs

    View full-size slide

  10. Negatives Effects

    View full-size slide

  11. Negatives Effects
    • Lower install convertion rate

    View full-size slide

  12. Negatives Effects
    • Lower install convertion rate
    • Google Play Featuring problems

    View full-size slide

  13. Negatives Effects
    • Lower install convertion rate
    • Google Play Featuring problems
    • Google Play Security Checks problems

    View full-size slide

  14. Negatives Effects
    • Lower install convertion rate
    • Google Play Featuring problems
    • Google Play Security Checks problems
    • Problems with Instant App (4Mb limits)

    View full-size slide

  15. APK Structure

    View full-size slide

  16. APK Structure

    View full-size slide

  17. APK Structure

    View full-size slide

  18. Size related to app

    View full-size slide

  19. Size related to app
    • APK size

    View full-size slide

  20. Size related to app
    • APK size
    • Initial download size

    View full-size slide

  21. Size related to app
    • APK size
    • Initial download size
    • On-device install size

    View full-size slide

  22. Size related to app
    • APK size
    • Initial download size
    • On-device install size
    • Update download size

    View full-size slide

  23. Size related to app
    • APK size
    • Initial download size
    • On-device install size
    • Update download size

    View full-size slide

  24. Graphic Optimizations

    View full-size slide

  25. Image Optimizers

    View full-size slide

  26. PNGQuant ImageMagick PNGGauntlet PNGOut
    PNGCrush OptiPNG CryoPNG PNG Compressor
    Yahoo Smush.it PNGOptimizer PunyPNG TinyPNG
    PNGWolf Advpng DeflOpt Defluff
    Huffmix TruePNG PNGng-s9 zopfliPNG

    View full-size slide

  27. Optimization results
    9.0 MB

    View full-size slide

  28. Optimization results
    6.5 MB

    View full-size slide

  29. Optimization results
    228 Images

    Save: 28%

    Average: 36%

    Max saving: 98.6%
    6.5 MB

    View full-size slide

  30. Android PNG Cruncher
    android {
    buildTypes {
    release {
    // Disable PNG optimisation
    crunchPngs false
    }
    }
    }

    View full-size slide

  31. SVGO
    github.com/svg/svgo

    View full-size slide

  32. PNG vs WebP
    PNG, 958 KB WebP 100%, 286 KB
    -70%

    View full-size slide

  33. PNG vs WebP
    PNG, 958 KB WebP 75%, 29 KB
    -97%

    View full-size slide

  34. Vector graphics

    View full-size slide

  35. Vector graphics
    xxxhdpi
    3.6 KB
    xxhdpi
    xhdpi hdpi
    mdpi
    2.5 KB 1.6 KB 1.1 KB 0.7 KB
    PNG, 100x100dp, 8 bit

    View full-size slide

  36. Vector graphics
    Size, bytes
    Optimized PNG,
    5 files
    SVG VectorDrawable Android Shape
    RAW 10,023 359 416 232
    In APK 10,023 - 371 241

    View full-size slide

  37. Glide + SVG
    github.com/kirich1409/SvgGlidePlugins

    View full-size slide

  38. POTrace
    PNG SVG
    potrace.sourceforge.net

    View full-size slide

  39. Reuse graphics

    View full-size slide

  40. Image tint
    android:src="@drawable/ic_favorite_black_48dp"
    android:tint=“@color/blue"
    />
    Drawable.setTint(int)
    Drawable.setTint(ColorStateList)

    View full-size slide

  41. Optimize images
    • Use image optimizer
    • Use WebP
    • Use vector graphics
    • Reuse similar images

    View full-size slide

  42. Resources

    Optimizations

    View full-size slide

  43. Downloadable fonts

    View full-size slide

  44. Unnecessary
    resources

    View full-size slide

  45. Resource shrinker
    android {
    buildTypes {
    release {
    minifyEnabled true
    shrinkResources true
    }
    }
    }

    View full-size slide

  46. Resource configurations
    android {
    defaultConfig {
    resConfigs ’ru’, ‘en’, ‘es’, ‘fr’, ‘ge’
    }
    }

    View full-size slide

  47. Resource configurations
    android {
    defaultConfig {
    resConfigs ’ru’, ‘en’, ‘es’, ‘fr’, ‘ge’
    resConfigs ’mdpi’, ‘hdpi’, ‘xhdpi’, ‘xxhdpi’
    resConfigs ’normal’, ‘large’, ‘xlarge’
    }
    }

    View full-size slide

  48. Resource Optimizations
    • Downloadable fonts
    • Remove unused resources
    • Reuse resources

    View full-size slide

  49. Resource Optimizations
    • Downloadable fonts
    • Remove unused resources
    • Reuse resources
    • Use recommended media formats

    AAC for audio, H264 AVC for Video and etc.

    View full-size slide

  50. Resource Optimizations
    • Downloadable fonts
    • Remove unused resources
    • Reuse resources
    • Use recommended media formats

    AAC for audio, H264 AVC for Video and etc.
    • Update strings over-the-air

    View full-size slide

  51. Code Optimizations

    View full-size slide

  52. D8 dex compiler

    View full-size slide

  53. DX compilation time
    Time (s)
    0
    5
    10
    15
    20
    DX D8
    * Tested with benchmark project here

    View full-size slide

  54. .dex file size
    File size, MB
    0
    10
    20
    30
    40
    50
    DX D8
    * Tested with benchmark project here

    View full-size slide

  55. Code optimizations tools

    View full-size slide

  56. Code optimizations tools
    • Java bytecode
    • ProGuard
    • DexGuard
    • R8
    • dex
    • Facebook ReDex

    View full-size slide

  57. Input JARs
    Optimised code
    Optimize
    Shrunk code
    Shrink
    Obfuscated code
    Obfuscate
    Output JARs
    Preverify

    View full-size slide

  58. ProGuard Result
    No optimization Optimization
    Optimization
    +
    obfuscation
    .dex size 2.6 Mb 1.2 Mb 0.9 Mb
    Classes number 4827 2747 2715
    Method references 42664 22319 18922
    medium.com/google-developers/practical-proguard-rules-examples-5640a3907dc9

    View full-size slide

  59. External libraries usage

    View full-size slide

  60. External libraries usage

    View full-size slide

  61. External libraries usage
    • Remove unused Android Support Library parts

    View full-size slide

  62. External libraries usage
    • Remove unused Android Support Library parts
    • Try to don’t add libraries for 1 class/method usage

    View full-size slide

  63. External libraries usage
    • Remove unused Android Support Library parts
    • Try to don’t add libraries for 1 class/method usage
    • Reflection based libraries can’t be properly optimised and
    shrinked

    View full-size slide

  64. External libraries usage
    • Remove unused Android Support Library parts
    • Try to don’t add libraries for 1 class/method usage
    • Reflection based libraries can’t be properly optimised and
    shrinked
    • Don’t add debug libraries to a release builds
    • Facebook Stetho
    • LeakCanary
    • etc.

    View full-size slide

  65. Native libraries

    View full-size slide

  66. Remove unused ABIs
    android {
    defaultConfig {
    ndk {
    abiFilters ‘armeabi-v7a’, ‘arm64-v8a’, ‘x86’
    }
    }
    }

    View full-size slide

  67. Code Optimisations
    • Use D8 compiler instead of DX
    • Code optimizations & shrinker tools
    • Be careful about using external libraries
    • Remove support of unused ABIs

    View full-size slide

  68. Other Optimizations

    View full-size slide

  69. APK Structure

    View full-size slide

  70. APK Structure

    View full-size slide

  71. Packaging options
    android {
    packagingOptions {
    exclude "/okhttp3/**"
    exclude "/com/google/api/**"
    exclude "/kotlin/**"
    exclude "/com/applovin/**.java"
    exclude "/META-INF/*.version"
    exclude "/META-INF/**.pro"
    exclude "/META-INF/services/**"
    exclude "**/*.kotlin_module"
    exclude "**/*.properties"
    exclude "**/*.xml"
    }
    }

    View full-size slide

  72. Features
    • Special compression format

    View full-size slide

  73. Features
    • Special compression format
    • File-by-File updates

    View full-size slide

  74. Features
    • Special compression format
    • File-by-File updates
    • Lite Version

    View full-size slide

  75. Features
    • Special compression format
    • File-by-File updates
    • Lite Version

    View full-size slide

  76. Features
    • Special compression format
    • File-by-File updates
    • Lite Version
    • Multiple APKs

    View full-size slide

  77. Google Play Multiple APKs
    • OpenGL texture compression formats
    • Screen sizes and densities
    • Device feature sets
    • Platform versions
    • CPU architectures
    • Special APK for Android Go Edition

    View full-size slide

  78. Google Play Dynamic Delivery

    View full-size slide

  79. Google Play Dynamic Delivery

    View full-size slide

  80. Google Play Dynamic Delivery

    View full-size slide

  81. Android App Bundle

    View full-size slide

  82. Notes about App Bundle
    • App Bundle has limitations for now
    • Required Android Studio 3.2
    • Need to apply Google Play App Signing
    • Google Play internal test track for test App Bundle in few
    minutes
    • bundletool for build an APK from App Bundle

    View full-size slide

  83. Q&A
    Kirill Rozov
    Lead Android Developer

    View full-size slide