Reducing_APK_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

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).

9Patch PNG

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.

    9Patch PNG examples

    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.

More information

Comments 12

Sort by: Newest | Oldest | Most popular

  1. By ashish tandon

    #1

    i have two libmupdf.so files which are of 6 mb each in my app which is making the apk size bigger. can you suggest any steps to reduce the file size of these “.so” files?

  2. By Majid Ghanadi

    #2

    Thanks D.Karlsson for useful article.
    I think there are some things that maybe affect APK file size .
    one of them, it can be coding architect. I see many mobile developers that they have behavior in mobile app as same as desktop or web application, that often this is the biggest problem. for example using images or shapes instead of standard buttons is one of these faults. Also we MUST certainly implement reusable code and avoid program of same code, or same dependency.

    • By Anna Aleryd

      #3

      Hi Majid,

      I’m glad you like the article. And good input, thank you!

      Best regards,
      Anna from Developer World

  3. By Melissa Bailey

    #4

    In case of Unity 3d, it’s built size even in empty scene is more then 7mb. Which means that without any assets it gives a built of 7mb. Is there any way to reduce that?

  4. By Syed Amjad

    #5

    Thanks for article, but how can I use Pro Guard? where I have to put these files? Help!

  5. By Mark Peters

    #7

    Thanks for the article also. I tried OptiPNG, but despite reducing the total size of the images by a couple MB, that made essentially no difference to the size of the apk (which was about 6.8MB before this) or of the on device footprint.

    I suspect that the compression done by the build tools does just a good a job of compression as OptiPNG. That is probably true with PNGCrush or any other tool as well.

  6. By Hemant Shah

    #8

    Very Nice & Tganks for the Tips.
    Helped us a Lot in our apps.
    Thanks Team.

    Regards,
    Gfx Bandits/Devs

  7. By srinivasan v

    #9

    thanks for the article, especially the png optimisation part. It helped me reduce my api size by more than half! I want to experiment with Proguard, just a little worried it may cause instability as it modified byte code. SInce android is so fragmented not sure if it is worth the risk. Since I’m a small developer ,do you think checking the build after using pro guard will ensure that the problem does not arise in the other 2200+ android handsets?

    • By Joe Padre

      #11

      Hi Karthik,
      Which sections did you want examples of?
      Thanks and best regards,
      Joe from Developer World

1-12 of 12 comments.