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

2aec47eb9a940c619f05972f0db5aa00?s=128

Kirill Rozov

August 07, 2018
Tweet

Transcript

  1. Kirill Rozov Lead Android Developer Optimize APK size

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

  3. None
  4. 64 MB

  5. What for? Download APK size 0 5 10 15 20

    25 Size from Google Play for Xiaomi Mi A1 Android 8.0
  6. Why it’s problem?

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

  8. Why it’s problem? • Support low-end devices • Low space

    on device memory
  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!!!
  10. 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
  11. 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
  12. Negatives Effects

  13. Negatives Effects • Lower install convertion rate

  14. Negatives Effects • Lower install convertion rate • Google Play

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

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

    Featuring problems • Google Play Security Checks problems • Problems with Instant App (4Mb limits)
  17. None
  18. None
  19. APK Structure

  20. APK Structure

  21. APK Structure

  22. Size related to app

  23. Size related to app • APK size

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

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

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

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

    size • On-device install size • Update download size
  28. Graphic Optimizations

  29. Image Optimizers

  30. PNGQuant ImageMagick PNGGauntlet PNGOut PNGCrush OptiPNG CryoPNG PNG Compressor Yahoo

    Smush.it PNGOptimizer PunyPNG TinyPNG PNGWolf Advpng DeflOpt Defluff Huffmix TruePNG PNGng-s9 zopfliPNG
  31. ImageOptim

  32. Optimization results 9.0 MB

  33. Optimization results 6.5 MB

  34. Optimization results 228 Images Save: 28% Average: 36% Max saving:

    98.6% 6.5 MB
  35. Android PNG Cruncher android { buildTypes { release { //

    Disable PNG optimisation crunchPngs false } } }
  36. SVGO github.com/svg/svgo

  37. Use WebP

  38. PNG vs WebP PNG, 958 KB WebP 100%, 286 KB

    -70%
  39. PNG vs WebP PNG, 958 KB WebP 75%, 29 KB

    -97%
  40. None
  41. None
  42. Vector graphics

  43. 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
  44. Vector graphics Size, bytes Optimized PNG, 5 files SVG VectorDrawable

    Android Shape RAW 10,023 359 416 232 In APK 10,023 - 371 241
  45. Glide + SVG github.com/kirich1409/SvgGlidePlugins

  46. POTrace PNG SVG potrace.sourceforge.net

  47. Reuse graphics

  48. None
  49. None
  50. Image tint <ImageView android:src="@drawable/ic_favorite_black_48dp" android:tint=“@color/blue" /> Drawable.setTint(int) Drawable.setTint(ColorStateList)

  51. Optimize images • Use image optimizer • Use WebP •

    Use vector graphics • Reuse similar images
  52. Resources
 Optimizations

  53. Fonts

  54. None
  55. None
  56. Downloadable fonts

  57. Unnecessary resources

  58. Resource shrinker android { buildTypes { release { minifyEnabled true

    shrinkResources true } } }
  59. Resource configurations android { defaultConfig { resConfigs ’ru’, ‘en’, ‘es’,

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

    ‘fr’, ‘ge’ resConfigs ’mdpi’, ‘hdpi’, ‘xhdpi’, ‘xxhdpi’ resConfigs ’normal’, ‘large’, ‘xlarge’ } }
  61. Resource Optimizations • Downloadable fonts • Remove unused resources •

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

    Reuse resources • Use recommended media formats
 AAC for audio, H264 AVC for Video and etc.
  63. 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
  64. Code Optimizations

  65. D8 dex compiler

  66. DX compilation time Time (s) 0 5 10 15 20

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

    40 50 DX D8 * Tested with benchmark project here
  68. Code optimizations tools

  69. Code optimizations tools • Java bytecode • ProGuard • DexGuard

    • R8 • dex • Facebook ReDex
  70. Input JARs Optimised code Optimize Shrunk code Shrink Obfuscated code

    Obfuscate Output JARs Preverify
  71. 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
  72. External libraries usage

  73. External libraries usage

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

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

    • Try to don’t add libraries for 1 class/method usage
  76. 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
  77. 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.
  78. Native libraries

  79. Remove unused ABIs android { defaultConfig { ndk { abiFilters

    ‘armeabi-v7a’, ‘arm64-v8a’, ‘x86’ } } }
  80. Code Optimisations • Use D8 compiler instead of DX •

    Code optimizations & shrinker tools • Be careful about using external libraries • Remove support of unused ABIs
  81. Other Optimizations

  82. APK Structure

  83. APK Structure

  84. 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" } }
  85. Google Play

  86. Features

  87. Features • Special compression format

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

  89. Features • Special compression format • File-by-File updates • Lite

    Version
  90. None
  91. 1.4 MB

  92. Features • Special compression format • File-by-File updates • Lite

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

    Version • Multiple APKs
  94. 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
  95. Google Play Dynamic Delivery

  96. Google Play Dynamic Delivery

  97. Google Play Dynamic Delivery

  98. Android App Bundle

  99. 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
  100. None
  101. None
  102. Q&A Kirill Rozov Lead Android Developer