Mobile application development requires special attention to resource optimization, since the size of the APK file directly affects the speed of downloading the app from the store and the amount of space occupied on the userโs device. One of the heaviest elements in modern projects are graphic files, which without proper processing can take up to 60-70% of the total weight of the distribution. Reducing the size of images is not just a way to save megabytes, but a critical practice for improving performance and improving the user experience, especially when working with slow networks.
In the environment Android Studio there are many built-in tools and third-party solutions that allow you to effectively compress raster images without visible loss of quality. The optimization process involves choosing the right file format, changing the resolution to suit different screen densities, and using specialized plugins to automate the routine. In this article, we will analyze in detail each stage of working with graphics so that you can create a light and fast application.
Choosing the right file format for optimization
The first step towards reducing the weight of the application is to abandon outdated formats in favor of more modern and efficient compression algorithms. Traditionally, developers have used PNG for images with transparency and JPEG for photos, but the Android ecosystem offers more advanced alternatives. The format WebPdeveloped by Google provides significantly better compression while maintaining visual quality, supporting both transparency and animation.
Using WebP can reduce file size by 25-35% compared to PNG and 25-30% compared to JPEG at equivalent quality. Converting existing resources to this format can be done directly inside the IDE using the context menu or specialized command line utilities. It is important to understand that while WebP support is present in all modern versions of Android, very old devices (below API 17) may require creating fallbacks in PNG format.
โ ๏ธ Attention: When bulk converting images to WebP, be sure to check for compression artifacts on dark gradients, as aggressive quality settings may result in visible banding or noise.
In addition to raster formats, you should consider using vector graphics (Vector Drawables) for icons, logos and simple illustrations. Vector files describe an image using mathematical curves rather than pixels, making them independent of screen resolution and incredibly lightweight. A single XML vector drawable file can replace dozens of raster images for different screen densities, which radically reduces project size.
Use the Vector Asset Studio tool in Android Studio to import SVG files and automatically generate vector assets compatible with the library support.
Working with resources of different screen densities
Fragmentation of Android devices leads to the fact that the same image must be presented in several resolutions for correct display on screens with different pixel densities (DPI). The Android resource system automatically selects the required file from drawable-mdpi, drawable-hdpi, drawable-xhdpi and other folders depending on the display characteristics of a particular smartphone. Incorrect scaling or placing an image in only one folder can lead to the system stretching a small image, losing quality, or compressing a large one, wasting extra memory.
To effectively manage resources, it is necessary to strictly adhere to scaling factors relative to the base density (mdpi). If your base image is 100x100 pixels for mdpi, then for other densities the dimensions must follow certain proportions. Following this grid ensures that the image will occupy the same physical space on the screen of any device, be it a budget phone or a flagship tablet.
- ๐ฑ mdpi (baseline): 1x - the base resolution from which calculations are made.
- ๐ฑ hdpi: 1.5x - 50% increase for medium-density screens.
- ๐ฑ xhdpi: 2x - double resolution for modern smartphones.
- ๐ฑ xxhdpi: 3x - standard for most flagship devices.
- ๐ฑ xxxhdpi: 4x - maximum density for devices with 4K screens and higher.
Automatic generation of all necessary image versions can be done using online converters or plugins for Android Studio, such like Nine Old Androids or built-in import functions. Manual creation of each size is not only labor-intensive, but also fraught with errors in proportions. Optimizing each version separately before placing it in the appropriate folders res allows you to achieve maximum compression effect.
Using compression tools inside Android Studio
The Android Studio development environment provides powerful built-in tools for analysis and optimization resources that often remain underestimated by newcomers. The tool Image Asset Studio allows you not only to create application icons, but also to manage resources, offering a preview of how the image will look on different devices. However, for deep compression of existing files, the optimization function is often used through the context menu or plugin Android Image Converter.
To reduce the size of an already added image, you can right-click on the file and select the option to convert to WebP. In the dialog box that opens, the developer is asked to select the compression quality level and encoding type (Lossy or Lossless). Mode Lossy (lossy) allows you to set the percentage of quality, usually values in the range of 80-90% provide an excellent balance between file weight and visual integrity of the picture.
Path to the tool: Right mouse button on the image -> Convert to WebP
After conversion, the system will offer to create backup copies in PNG format to support older versions of Android, if your minSdkVersion lower than 17. Refusal to create backup copies is possible only if you are sure that your application will not be installed on outdated devices, which is becoming increasingly rare in modern realities. Using built-in IDE tools is preferable to third-party online services, as this guarantees compliance with the project structure and naming rules.
โ ๏ธ Attention: The interface of compression tools may change slightly in new versions of Android Studio (Giraffe, Hedgehog, Iguana), so the location of the buttons may differ from that described in old tutorials.
Analysis of APK size and identification of heavy resources
Even after manual optimization of individual files, hidden resources may remain in the project, which inflate the size of the final distribution. To identify such "heavyweights" in Android Studio, there is a tool APK Analyzerthat allows you to examine in detail the contents of the compiled application file. This tool shows the actual size of each resource in compressed and uncompressed form, helping you find files that take up a disproportionate amount of space.
The analyzer is launched through the menu Build -> Analyze APK, after which you will see a tree structure of all files inside the package. Sorting by column Raw Size or Download Size instantly brings the most voluminous images to the fore. It often turns out that some forgotten high-resolution mockups or logos added โjust in caseโ take up hundreds of kilobytes that could be saved.
| Resource type | Average weight (before optimization) | Average weight (after optimization) | Recommended action |
|---|---|---|---|
| Photos (JPEG) | 500 KB - 2 MB | 100 KB - 400 KB | Convert to WebP, reduce resolution |
| Icons (PNG) | 20 KB - 50 KB | 2 KB - 10 KB | Replacement with Vector Drawable |
| Background images | 1 MB - 5 MB | 200 KB - 800 KB | Strong compression, using 9-patch |
| Animation sprites | 3 MB - 10 MB | 500 KB - 2 MB | Using Lottie or WebP animation |
Regular audit of the project using APK Analyzer should be part of the release preparation process. This allows you to control the growth of the application size throughout the development cycle and respond in a timely manner to the addition of unoptimized assets by new team members. Dependency analysis Also important, since some third-party libraries may bring with them their own graphic assets that duplicate your own.
โ๏ธ Optimization checklist graphics
Configuring resource compression in Gradle
In addition to manual work with files, a significant reduction in the size of the final APK can be achieved by configuring the Gradle build system. In the module-level file, you can enable a function build.gradle module level you can activate the function shrinkResourcesthat automatically removes all unused resources from the final assembly. This option works in conjunction with minifyEnabled true (enabling ProGuard or R8) and analyzes the code to determine which images, lines or layouts are actually called in the application.
android {buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
}
}
}
Activating these options is critical for release builds, since during development we often create temporary images or duplicates that we forget to delete. The project builder will automatically detect that these files are not referenced in code or XML layouts and will exclude them from the final package. However, you should be careful: if you use dynamic loading of resources by file name (reflection), the collector may mistakenly delete the desired file, considering it unused.
For such cases, there is a mechanism for saving resources through a file keep.xml, where you can explicitly specify which files should not be deleted, even if static analysis does not find references to them. This allows you to flexibly manage the cleaning process while maintaining a balance between minimal application size and functionality. Proper Gradle configuration can reduce APK size by 10-20% without any intervention in the image files themselves.
Advanced techniques: 9-patch and dynamic loading
For background images, buttons and interface elements that need to stretch to fit different content, this format is ideal 9-patch (.9.png). This special PNG format contains a black border around the edges that tells Android which parts of the image can be stretched and which parts should remain the same (such as rounded corners or shadows). Using 9-patch allows you to use a single file instead of a set of images of different sizes, which saves a lot of space.
In cases where an application contains a huge number of high-quality images (for example, a product catalog or gallery), storing them all inside an APK may not be practical. This is where the dynamic loading technique comes to the rescue, when graphics are loaded from the server as needed. This allows you to keep the size of the application to a minimum, as well as update visual content without the need to release a new version of the app through the store.
โ ๏ธ Attention: When loading dynamically, be sure to implement caching mechanisms (for example, using libraries Glide or Coil) so as not to waste user traffic on repeated downloading of the same images.
It is also convenient to create 9-patch images in Android Studio using the tool Draw 9-patch, which is part of the SDK tools. It provides an interactive interface for marking stretch zones and content, immediately showing a preview of the result. Ignoring this tool and trying to stretch regular PNGs leads to blurred boundaries and loss of presentable interface on devices with non-standard screen proportions.
How does the R8 algorithm work?
R8 is a new code and resource compressor that replaced ProGuard. It performs shrinking, bytecode optimization, and obfuscation. Unlike its predecessor, R8 is faster and provides better optimization by analyzing the entire application rather than file by file.
The combination of WebP format, vector graphics, and the inclusion of shrinkResources in Gradle can reduce the size of an APK by more than half compared to an unoptimized project.
Frequently asked questions (FAQ)
Is it possible to reduce an image programmatically while the application is running?
Yes, this is possible using the class BitmapFactory and parameter inSampleSize. By setting this parameter to a power of two (2, 4, 8), you can load a smaller copy of the image into memory, which will prevent an error OutOfMemoryErrorfrom occurring. However, this method reduces RAM consumption, not the file size on disk or in the APK.
Why did some files increase in size after converting to WebP?
This can happen with very small images (less than 50x50 pixels) or images with a simple color palette. In such cases, the overhead of the WebP format may exceed the benefit from compression. For small icons, it is better to use vector graphics or leave PNG.
Do you need to delete the original PNG files after converting to WebP?
If you created backup copies of PNG to support older versions of Android (API < 17), you do not need to delete them, since the system itself will select the desired format. If your minSdkVersion above 17, and you are sure that WebP is supported on all target devices, the originals can be deleted to clean up the project.
How to compress images without opening Android Studio?
There are command utilities, such as cwebp from Google, which allow batch process images through the terminal. You can also use online services like Squoosh.app, but when working with the command line you get more control over compression parameters and the ability to integrate into CI/CD pipelines.
Does image compression affect the display quality on 4K screens?
With the right approach (using xxhdpi and xxxhdpi resources), the impact on quality will be minimal or invisible to the eye. The WebP format with a quality of 85-90% is visually almost indistinguishable from the original, but at the same time provides high clarity even on displays with high pixel density.