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

Pruning Your App: Good Practices for Reducing A...

Pruning Your App: Good Practices for Reducing App Size

App size reduction should be a critical aspect of app development, and is especially necessary for end users who may have restricted device storage or limited data plans. However, it can be challenging for developers—especially those working with multiple contributing teams—to manage and visualize app size throughout the development flow.

Using the major telecom company app, Xfinity, as an example, we will take a deep dive into how to optimize app size. This app illustrates the complexities of size management: the app is used across the world in both high- and low-resourced contexts, requires multi-language support, and draws visual assets from regional partners.

This presentation will address the following tools and techniques, many of which have been used in the Xfinity development flow, and are broadly applicable to other development projects:
Image compression: Using lossless compression with modern formats (.webp, vector graphics)
Unused code and assets: Implementing code shrinkers (Google’s R8)
Modularizing app delivery: Dividing the app into smaller modules, downloaded on-demand as per user needs (Google’s Dynamic Feature Delivery)
Strings: Improving string usage, including considerations for localization
Analysis: Monitoring app size throughout the development process

Avatar for Chrystian Vieyra

Chrystian Vieyra

June 11, 2023
Tweet

More Decks by Chrystian Vieyra

Other Decks in Technology

Transcript

  1. Overview 1. Rationale: app size reduction 2. WebP: Lossless compression

    with modern formats 3. Google R8: Unused code and assets: code shrinkers 4. Play Feature Delivery: Modularizing app delivery 5. Monitoring: Tracking app size 6. Conclusion 3
  2. APK size: A study from Segment • Smaller APK sizes

    correlate with higher install conversion rates. • Impact of APK size on download rate: Mortgage Calculator Free • Linear decrease of downloads by 0.45% per 1MB in install conversion rate from 3–99 MB 4 https://segment.com/blog/mobile-app-size-effect-on-downloads/ 1. Rationale
  3. • Smaller APK sizes correlate with higher install conversion rates,

    especially in emerging markets. • Download completion rate of an app with an APK size of around 10MB is ~30% higher than an app with an APK size of 100MB. • Impact of APK size reduction is greater in emerging markets like India and Brazil compared to developed markets like Germany, USA, and Japan. 5 https://medium.com/googleplaydev/shrinking-apks-growing-installs-5d3fcba23ce2 APK size: A study from Google 1. Rationale
  4. Download vs. installed size 8 • APK Decompression: Apps are

    compressed for efficient distribution on the Google Play Store. Once downloaded, they are unpacked, increasing app size. • Code Compilation: Once installed, parts of the app are compiled into machine code for the device's architecture, increasing app size. 1. Rationale
  5. Installed size description 9 Description App size Total size of

    uncompressed app binary Data size Data files on the device (pictures, videos, databases, etc.) Cache size Temporary files on the device for faster access. (Similar to how web browsers cache images, HTML, CSS). 1. Rationale
  6. WebP image format: Benefits • Smaller file sizes à typically

    26% smaller than PNG • WebP supports lossless and lossy compression • Native support since Android 4 10 2. W ebP
  7. WebP image format: Downsides • Figma does not natively export

    to .webp • Design team may be reluctant to change their workflow to accommodate a plugin or manual file conversion. • iOS does not natively support .webp à can’t use the same format across both platforms. • Adobe DAM and other popular tools to store digital assets online not yet supported. 11 2. W ebP
  8. WebP scenario: Xfinity • 1.6 MB PNG file required optimization

    • Lossless conversion yielded a file of 46% the original size, resulting in a 831.2 KB WebP file • Further optimized the image by applying 80% encoding quality • Post-optimization, file size was 3% of the original size 48 KB 12 2. W ebP Source: Xfinity
  9. Converting PNG to WebP Right-click the PNG image and select

    'Convert to WebP...’ 13 Select lossy or lossless encoding 2. W ebP
  10. On-Demand Resources • On-demand resources not downloaded until the user

    needs them. • Help reduce app initial download size, as well as the amount required storage. • Host images on a CDN or service (Adobe DAM). • Use image loading libraries (Glide for Android) to efficiently fetch and render images. • Google Play On-Demand Delivery 15 2. W ebP
  11. Impact of .webp on Xfinity • Before .webp: 92.9 MB

    • After .webp: 82.9 MB • Savings: 10 MB (10.77% reduction) 16 2. W ebP
  12. What are Code Shrinkers? • Tools to reduce code base

    size. • Remove unused or unnecessary parts of the code: ◦ Optimizes for better performance ◦ Obfuscate code to increase security. 17 R8 3. G oogle R8
  13. Android Code Shrinkers ProGuard Open-source Widely used in the Java

    ecosystem, including Android. R8 Google's code shrinker. Offers more efficient code shrinking and optimization for Android. 18 R8 3. G oogle R8
  14. R8 minification and optimization • Dead code elimination: Removes code

    that is never executed. • Class merging: Merges classes that are not used independently. • Field and method inlining: Inlines fields and methods that are only used locally. • String constant folding: Folds string constants that are used multiple times. • Type inference: Infers variable types and expressions to avoid unnecessary type casts. 20 3. G oogle R8
  15. shrinkResources • Images • Layout files • Strings • Colors

    • Animations • Fonts • XML files 21 3. G oogle R8
  16. Missing classes and methods ClassNotFoundException or MethodNotFoundException at runtime indicates

    missing classes or methods due to ProGuard removal. 27 3. G oogle R8
  17. Understanding Proguard rules 28 3. G oogle R8 • proguard-rules.pro

    file • Rules for keeping specific classes, methods and fields from being removed or renamed
  18. Keep • The keep rule prevents certain classes from being

    shrunk, optimized, or obfuscated by R8. • Syntax: keep class <class_specification> • When R8 encounters a keep rule, it keeps the specified classes along with all their members. 29 3. G oogle R8
  19. keepclassmembers keepclassmembers • The keepclassmembers rule is used to prevent

    members of certain classes from being shrunk, optimized, or obfuscated. • Syntax: keepclassmembers class <class_specification> { <member_specification>; } • Keeps specified class members while allowing the class itself to be shrunk, optimized, or obfuscated. 30 3. G oogle R8
  20. keepclasseswithmembers keepclasseswithmembers rule • The keepclasseswithmembers rule is used to

    prevent classes that contain certain members from being shrunk, optimized, or obfuscated. • Syntax: keepclasseswithmembers class <class_specification> { <member_specification>; } Only classes that contain the specified members will be kept. If a class does not contain the specified members, it can still be shrunk, optimized, or obfuscated. 31 3. G oogle R8
  21. App size reduction • R8 is capable of significantly reducing

    the size of Android apps. • Case study: Google's 2019 I/O app with R8 shrinking: • Size 18.55 MB à 6.45 MB. • Methods 150,220 à 45,831 • Dex files 3 à 1 (65% reduction) 33 https://medium.com/androiddevelopers/shrinking-your-app-with-r8-909efac25de4 3. G oogle R8
  22. Play Feature Delivery • Play Feature Delivery is a feature

    of the Play Core SDK that customizes the delivery of app features. • Reduce your app's size and improve the user experience. • Conditional delivery: Delivers only compatible features. • On-demand delivery: Let users download features when they need them. 34 4. Play Feature Delivery
  23. Conditional Delivery • Conditional Delivery allows your app to be

    available under conditions: • Hardware capabilities • Android API level • Tailors app experience to the device's capabilities • Ensures compatibility and functionality of app on different devices • Reduces app size by delivering suitable modules for each device 35 4. Play Feature Delivery
  24. Conditional Delivery Use Case • Challenge: Adding AR mode to

    my personal app. • AR mode added ~5 MB to the total install size. • ARCore (the AR Framework) not compatible with all Android devices. • Google Play Conditional Delivery resolved this issue. 36 4. Play Feature Delivery
  25. SplitAPK • For a device like the ZFold4, which is

    compatible with ARCore, the ar.apk split APK will be included if the app utilizes AR features. • Users only download the components of the app that are compatible to their device 38 4. Play Feature Delivery
  26. On-Demand Delivery • Reduces app download size. • User installations

    only download the main app. • Users can request to download the feature not included in the main app. • Feature will be downloaded in the background, and the user can start using it as soon as it is downloaded. 39 4. Play Feature Delivery
  27. On-Demand Delivery • Enables apps to request and access on-demand

    feature modules. • Provides methods for requesting the download of a feature module. • Allows for uninstallation of modules that are no longer needed. 41 4. Play Feature Delivery
  28. Tracking the size of your app • Monitoring CI: Custom

    scripts to warn about size increases. • Android Studio APK Analyzer: Identify the files and resources that are taking up space. • Google Play Console: Track changes in app size over time and get suggestions about how to reduce it. • Emerge Tools: Gain insights into app size and causes. 42 5. M onitoring
  29. Implementing custom CI scripts • The script checks the app’s

    size • If the APK exceeds the limit, the build fails • Google Play has a 150 MB .apk size limit 43 5. M onitoring
  30. CI scripts: Ruler • Gladle plugin that helps you analyze

    the size of your Android app. • Displays how much each module and dependency contributes to app size. • Creates an HTML report. 44 5. M onitoring https://github.com/spotify/ruler/tree/main
  31. APK Analyzer • APK Structure Analysis: Gives an in-depth look

    at the APK's layout, including app code, resources, and compiled libraries. • DEX File Inspection: Enables viewing of DEX files, the compiled format of Java source code. • File Size Visualization: Offers graphical representation of APK file sizes to help identify the most space-consuming parts. 45 5. M onitoring
  32. Comparing APKs • Old Size: Displays the size of the

    original or older APK version. • New Size: Shows the size of the newer or updated APK version. • Diff Size: Highlights the difference in size between the old and new APK, assisting in understanding the impact of changes. 47 5. M onitoring
  33. Google Play Console: App Size Optimizations • WebP for images

    • App bundles • Code shrinking and obfuscation 49 5. M onitoring
  34. Emerge Tools: X-Ray 52 5. M onitoring Emerge Tools: Visual

    representation of the contents inside an APK.
  35. Emerge Tools: Insights • Identify and remove duplicate files and

    resources within the app. • Optimize image files by converting suitable ones to the .webp format. • Find and highlight large files in the codebase for potential reduction or elimination. 53 5. M onitoring