0
0
Android Kotlinmobile~15 mins

App size optimization in Android Kotlin - Deep Dive

Choose your learning style9 modes available
Overview - App size optimization
What is it?
App size optimization means making your Android app smaller in file size. This helps the app download faster and use less storage on users' devices. It involves techniques like removing unused code, compressing images, and splitting the app into parts. Smaller apps improve user experience and save data.
Why it matters
Without app size optimization, apps become large and slow to download, which can frustrate users and cause them to uninstall. Large apps also take up more device storage, limiting who can install them. Optimizing app size helps reach more users, especially those with limited data or storage, and improves app performance.
Where it fits
Before learning app size optimization, you should understand basic Android app structure and Kotlin programming. After this, you can learn advanced performance tuning and user experience improvements. App size optimization fits into the broader topic of app performance and distribution.
Mental Model
Core Idea
App size optimization is about removing or reducing everything in your app that is not essential to make it smaller and faster to install.
Think of it like...
Imagine packing a suitcase for a trip. You only take what you really need and leave out extra items to make the suitcase lighter and easier to carry.
┌─────────────────────────────┐
│       Android App            │
├─────────────┬───────────────┤
│ Code        │ Images        │
│ (Kotlin)   │ (PNG, JPEG)    │
├─────────────┼───────────────┤
│ Libraries  │ Resources      │
│ (3rd party)│ (Layouts, etc) │
└─────────────┴───────────────┘
       ↓ Remove unused parts
       ↓ Compress and split
┌─────────────────────────────┐
│    Smaller Optimized App     │
└─────────────────────────────┘
Build-Up - 7 Steps
1
FoundationUnderstanding app size basics
🤔
Concept: Learn what makes up an Android app's size and why it matters.
An Android app includes code, images, libraries, and resources. Each part adds to the total size. For example, large images or many libraries increase size. Knowing these parts helps you see where to optimize.
Result
You can identify which parts of your app contribute most to its size.
Understanding the components of app size is the first step to knowing what to optimize.
2
FoundationMeasuring app size
🤔
Concept: Learn how to check your app's size and what tools to use.
Use Android Studio's 'Analyze APK' tool to open your app's APK file and see size breakdown. You can also use 'Build > Analyze APK' to inspect code, resources, and libraries. This helps find large files or unused parts.
Result
You get a clear report showing which files or parts take the most space.
Measuring app size precisely guides your optimization efforts effectively.
3
IntermediateRemoving unused code and resources
🤔Before reading on: do you think unused code and resources increase app size or have no effect? Commit to your answer.
Concept: Learn how to remove code and resources that your app does not use.
Use ProGuard or R8 in your build process to shrink and obfuscate code. These tools remove unused classes and methods. Also, enable resource shrinking to remove unused images and layouts. This reduces app size without changing functionality.
Result
Your app's APK size decreases because unnecessary code and resources are removed.
Knowing that automated tools can safely remove unused parts prevents manual errors and saves time.
4
IntermediateOptimizing images and assets
🤔Before reading on: do you think compressing images affects app quality or only size? Commit to your answer.
Concept: Learn how to reduce image sizes without losing visible quality.
Convert images to modern formats like WebP which compress better than PNG or JPEG. Use tools to resize images to needed dimensions only. Avoid including multiple copies of the same image at different sizes unless necessary.
Result
Images take less space, making the app smaller while keeping good visual quality.
Understanding image formats and compression helps balance size and appearance.
5
IntermediateUsing app bundles and dynamic delivery
🤔Before reading on: do you think delivering the whole app at once or in parts affects download size? Commit to your answer.
Concept: Learn how to split your app so users download only what they need.
Android App Bundles let you upload a single package to Google Play. Google Play then delivers optimized APKs for each device, including only needed code and resources. You can also use dynamic feature modules to let users download features on demand.
Result
Users download smaller, device-specific versions of your app, saving data and storage.
Knowing how app bundles work helps you design apps that adapt to user needs and devices.
6
AdvancedAnalyzing native libraries and dependencies
🤔Before reading on: do you think all included libraries are necessary or can some be trimmed? Commit to your answer.
Concept: Learn to inspect and reduce native libraries and dependencies that increase app size.
Check your app's native libraries (.so files) for unused architectures. Use ABI splits to include only needed CPU architectures. Review dependencies and remove or replace heavy libraries with lighter alternatives.
Result
Your app excludes unnecessary native code and heavy libraries, reducing size.
Understanding native code and dependencies prevents hidden size bloat and improves performance.
7
ExpertAdvanced code shrinking and resource optimization
🤔Before reading on: do you think code shrinking can break app behavior or is always safe? Commit to your answer.
Concept: Learn advanced techniques and pitfalls of code shrinking and resource optimization.
Use R8 with custom rules to fine-tune what is removed or kept. Be careful with reflection and dynamic code, which can break if removed. Use resource aliasing and vector drawables to reduce resource duplication. Test thoroughly after shrinking.
Result
Your app is as small as possible without breaking functionality.
Knowing the limits and risks of shrinking tools helps avoid bugs and crashes in production.
Under the Hood
When you build an Android app, the build tools compile Kotlin code into bytecode and package it with resources and libraries into an APK or app bundle. Tools like R8 analyze the code to find unused parts and remove them. Resource shrinking scans resource usage to remove unused images or layouts. App bundles allow Google Play to generate device-specific APKs by including only needed code and resources.
Why designed this way?
Android apps run on many devices with different CPUs, screen sizes, and languages. To avoid forcing users to download everything, Google designed app bundles and dynamic delivery to optimize downloads. Code shrinking tools evolved to automate removing unused code safely, balancing size and app correctness.
┌───────────────┐
│ Kotlin Source │
└──────┬────────┘
       │ Compile
┌──────▼────────┐
│ Bytecode (.class) │
└──────┬────────┘
       │ R8 Shrinking
┌──────▼────────┐
│ Shrunk Bytecode│
└──────┬────────┘
       │ Package
┌──────▼────────┐
│ APK / App Bundle│
└──────┬────────┘
       │ Google Play
┌──────▼────────┐
│ Device-specific APK│
└─────────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does removing unused code always guarantee a smaller app size? Commit yes or no.
Common Belief:Removing unused code always makes the app smaller.
Tap to reveal reality
Reality:Sometimes removing code can increase size due to added metadata or changed compression.
Why it matters:Assuming shrinking always reduces size can lead to ignoring other optimization areas.
Quick: Do you think all image compression reduces quality noticeably? Commit yes or no.
Common Belief:Compressing images always makes them look worse.
Tap to reveal reality
Reality:Modern formats like WebP compress images with little or no visible quality loss.
Why it matters:Avoiding compression due to fear of quality loss misses big size savings.
Quick: Is it safe to remove any library you don't directly use? Commit yes or no.
Common Belief:Unused libraries can be removed without checking dependencies.
Tap to reveal reality
Reality:Some libraries are required indirectly; removing them can break the app.
Why it matters:Blindly removing libraries can cause runtime crashes and bugs.
Quick: Does delivering the whole app at once always provide the best user experience? Commit yes or no.
Common Belief:Delivering the entire app at once is simpler and better for users.
Tap to reveal reality
Reality:Splitting the app and delivering features on demand reduces initial download size and improves user experience.
Why it matters:Ignoring dynamic delivery can lead to large downloads and user drop-off.
Expert Zone
1
Some code shrinking optimizations can interfere with debugging and crash reporting if not configured properly.
2
Dynamic feature modules require careful design to avoid user confusion and ensure smooth feature downloads.
3
Resource aliasing can reduce duplication but may increase build complexity and require maintenance discipline.
When NOT to use
Avoid aggressive shrinking and splitting for very small apps or apps with simple features where overhead outweighs benefits. In such cases, focus on clean code and simple assets. Also, avoid dynamic delivery if your app must work fully offline immediately after install.
Production Patterns
Many production apps use app bundles with ABI splits and dynamic features for optional modules like premium content. They combine R8 shrinking with custom ProGuard rules to protect reflection code. Image assets are converted to WebP and vector drawables are used for icons to minimize size.
Connections
Data Compression
App size optimization uses data compression techniques similar to general data compression.
Understanding compression algorithms helps optimize image and code compression in apps.
Supply Chain Management
Both optimize delivery by only sending what is needed when it is needed.
Knowing how supply chains reduce waste helps understand dynamic delivery in apps.
Lean Manufacturing
Both focus on removing waste to improve efficiency.
The principle of eliminating unused parts in manufacturing parallels removing unused code and resources.
Common Pitfalls
#1Removing code without checking for reflection usage.
Wrong approach:Applying R8 shrinking with default rules on code that uses reflection without adding keep rules.
Correct approach:Add ProGuard keep rules for classes and methods accessed via reflection before shrinking.
Root cause:Misunderstanding that reflection code is invisible to shrinking tools causes runtime crashes.
#2Including all CPU architectures in the APK unnecessarily.
Wrong approach:Building APK with all ABI splits included: abiFilters = ['armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'] without splitting.
Correct approach:Use ABI splits to generate separate APKs for each architecture to reduce size per device.
Root cause:Not knowing that including all native libraries bloats APK size for most users.
#3Using large PNG images without compression or resizing.
Wrong approach:Adding full-size PNG images directly into drawable folders without optimization.
Correct approach:Convert images to WebP and resize to needed dimensions before adding to the project.
Root cause:Assuming image quality is always better with original formats leads to large app size.
Key Takeaways
App size optimization improves user experience by making apps faster to download and lighter on device storage.
Measuring app size and understanding its components is essential before applying optimization techniques.
Tools like R8 and resource shrinking automate removing unused code and resources safely.
Using app bundles and dynamic delivery tailors app downloads to each device, saving data and space.
Advanced optimization requires careful handling of reflection, native libraries, and image assets to avoid bugs.