TIPS FOR REDUCING APK FILE SIZE
Tips for reducing APK file size
Are you having problems getting under the current 50 MB upload limit imposed by Android Market™? Don’t let it be a roadblock in preparing your Android™ app for market. Instead, use these excellent tips for reducing the file size of your APK. David Karlsson, a software architect at Sony Ericsson, will get you all the details. Read more after the jump.
David Karlsson, Sony Ericsson software architect
Hi, I’m David Karlsson, a Sony Ericsson Software Architect. My work mainly involves giving architectural guidance and technical support to our development partners and third party vendors. One important developer topic that comes up frequently is how to reduce the APK file size of an app. Developers who are submitting apps to Android Market™ sometimes discover that their APK file exceeds 50 MB, the current upload size limit. But you don’t have to accept the APK file size as is. There are ways you can reduce your APK file size.
What is an APK file?
An APK is an Android application package file. Each Android application is compiled and packaged in a single file that includes all of the application’s code (.dex files), resources, assets, and manifest file. The APK file is basically a .zip file, so there’s no way of compressing its size any further.
Why you should consider keeping files as small as possible
Memory space on smartphones is often a competitive area, now that most users are storing music, video, messages are more, in addition to downloading apps. The smaller you make your APK, the better it is for the user, and that may be a deciding factor on whether to download your app, versus a similar app that takes up more phone memory.
Tips for reducing file size
Here are a number of recommendations for reducing static footprint. You can choose the ones that fit best for your own particular needs.
ProGuard
A tool for code shrinking, like ProGuard, will significantly reduce the static foot print. The tool is available at sourceforge. Note that it is very important to re-test all of the application after applying ProGuard since it may change the application behavior. As ProGuard replaces the application symbols, to make the code difficult to read, it is important that you retain the symbol mapping, so that you can translate a stack trace back to the original symbols if you have to investigate a crash in your application.
Removal of debug information
We recommend that you remove all debug-related functionality from the application. The application generally does not see or use this data, and the Android operating system does not require it to run the application. Hence, the debug information only wastes space, and should be removed.
To accomplish this, all debug related functionality must be enclosed in conditional blocks, like below:
static final debug = false;
if (debug) {
Log.v(TAG, “Debug …”);
}
It is important that the debug flag is set at compile time (i.e. declared as static final) for the compiler to be able to completely remove all debug functionality. Creating your own debug method like the one shown below is not a good idea because the call to myDebugPrint() is not enclosed by a conditional block, which means that the compiler must retain information about myDebugPrint() in the calling class.
public void myDebugPrint() {
if (Debug) {
Log.v(TAG, “Debug …”);
}
}
…
myDebugPrint()
…
Removal of debug symbols from native libraries
Using debug symbols makes sense if your application is still in development, and still requires debugging. But if debug symbols are still appearing when you compile a release build, and if you want to remove them, then we recommend that the Debug symbols be removed from native libraries (.so files). This is done using the arm-eabi-strip command, from the Android NDK.
Recommended media formats
If your application relies heavily on images, audio or video, another way you can reduce the APK size is by using certain media formats. We recommend that you use the following media formats for images, audio and video:
■ Images: PNG or JPEG. Use PNGs; since it is a lossless format it is very suitable for
textures and artwork as there will be no visual artefacts from the compression. If there
are space constraints, use JPEGs or a combination of PNGs and JPEGs. A high quality
JPEG image may work fine for large photo-realistic images, which the JPEG compression
scheme is optimised for.
■ Audio: AAC Audio is recommended for all audio resources. AAC achieves better
compression at a given quality, compared to mp3 or Ogg Vorbis. Raw formats such as
WAV should never be used. The common rational for using the WAV format is that
decoding compressed audio streams usually means high latency at playback. However,
Android provides the Sound Pool API which enables applications to use compressed audio
streams without the penalty of high latency.
■ Video: Use H264 AVC. Encode the video to a resolution no larger than the screen
resolution of the target device (if known).
Optimise PNG sizes without losing quality
If you use PNG format, PNG images can be reduced in file size without losing quality. To do this, use a tool such as OptiPNG or PNGCrush. Both are great for reducing PNG file size while still ensuring image quality. PNGcrush is an open-source program that iterates over PNG filters and zlib (Deflate) parameters, compresses the image repeatedly using each parameter configuration, and chooses the configuration that yields the smallest compressed (IDAT) output. On the other hand, OptiPNG performs the trials entirely in memory, and writes only the final output file on the disk. Moreover, it offers multiple optimization presets to the user.
Use 9patch PNGs to scale your images
A 9patch png is a special format for PNG that can be used for backgrounds. Nine patch images are especially useful when designing buttons. Instead of defining a regular bitmap to be used as a background, a 9patch defines nine segments – for each of the four corners, the four edges, and the centre. (See illustration).
Segments defined
for 9patch PNG.
When you use a 9patch image, the image is subject to the following conditions:
■ The corner segments are never scaled.
■ The edge segments are repeated along the edge of the in order to fill the edge
between the corner segments.
■ The centre segment is repeated to fill the centre of the background between the four
edges.
■ The illustration below shows two buttons. The background of each button is generated
from the same 9patch.
Example of buttons generated from
the same 9patch file.
A single 9patch PNG can be used to replace many background images, as it can scale to fill the size of the control. Equally, a single 9patch image can be used on all devices of the same dpi screen, removing the need for screen-dependent backgrounds.
Remove unused resources
Another potential group of space wasters to consider for removal from your APK file are unused resources in your res directory, such as unused layouts, drawables and colors. To detect unused resources in your APK which might be able to be removed, use the android-unused-resources tool. Android Unused Resources is a Java application that will scan your project for unused resources.
Avoid duplication
Making sure that your application doesn’t have duplicate functionality or duplicate assets is an obvious way to avoid having unnecessary files in your APK. It is important to understand which Android APIs you use, and the full functionality that each provides. It could be that one Android API is already doing the work of another API. Duplicated assets (strings, bitmaps etc) are also a waste of space, and can be easily avoided. To a lesser extent, duplicated code will also unnecessarily increase the size of the delivered binary.
***
If you have any questions on how to reduce APK file size, feel free to leave a comment and we’ll get back to you with an answer as soon as possible.
Category: Android, Android tips Text, Apk
0 comments