Tips to Reduce Your Android APK Size

App size really matters when going live to the play store. I would like to share the few approaches which i use to reduce the app size.

1. Proguard

Code shrinking tools like proguard helps to significantly reduce the code foot prints. After applying proguard to the code, test the app properly as it replaces the symbols. You can learn more about proguard from sourceforge.

http://proguard.sourceforge.net/

2. Vector Drawables

With a release of 23.2 Support Library, Android is supporting the vector drawable for older version of the android devices. Android Studio provide a way to simply convert SVG to Vector Drawables. Here we shouldn’t have to worry about the different device DPI’s. This approach will significantly reduce the app size.

http://android-developers.blogspot.in/2016/02/android-support-library-232.html

3. Apk splits

The Split mechanism allows applications to be built for some forms of multi-apks more efficiently than using flavors. This approach allows you to create the apk for specific density and ABIs.

4. Google Play Services

Select the compiling APIs extensively.

In versions of Google Play services prior to 6.5, you had to compile the entire package of APIs into your app. In some cases, doing so made it more difficult to keep the number of methods in your app (including framework APIs, library methods, and your own code) under the 65,536 limit.

From version 6.5, you can instead selectively compile Google Play service APIs into your app. For example, to include only the Google Fit and Android Wear APIs, replace the following line in your build.gradle file:

compile 'com.google.android.gms:play-services:8.4.0'

with these lines:

compile 'com.google.android.gms:play-services-gcm:8.4.0'
compile 'com.google.android.gms:play-services-wearable:8.4.0'

5. Optimize png images

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.

http://optipng.sourceforge.net/

6. Remove unused resources

You can make use of android-resource-remover is utility that removes unused resources reported by Android Lint from your project.

7. 9-patch 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.

http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

8. Compress JPEG images

Reduce jpeg files as much as possible. You can make use of tools like Paint.NET.

9. Removable 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.

10. Avoid duplication

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

11. Use lint extensively

Proguard works on the Java side. Unfortunately, it doesn’t work on the resources side. As a consequence, if an image my_image in res/drawable is not used, Proguard only strips it’s reference in the R class but keeps the associated image in place.

Lint is a static code analyzer that helps you to detect all unused resources with a simple call to ./gradlew lint. It generates an HTML-report and gives you the exhaustive list of resources that look unused under the “UnusedResources: Unused resources” section. It is safe to remove these resources as long as you don’t access them through reflection in your code.

Lint analyzes resources (i.e. files under the /res directory) but skips assets (i.e. files under the /assets directory). Indeed, assets are accessed through their name rather than a Java or XML reference. As a consequence, Lint cannot determine whether or not an asset is used in the project. It is up to the developer to keep the /assets folder clean and free of unused files.

12. Reuse resources whenever possible

Reusing stuff is probably one of the first important optimization you learn when starting developing on mobile. In a ListView or a RecyclerView, reusing helps you keep a smooth scrolling performance. But reusing can also help you reduce the final size of your APK. For instance, Android provides several utilities to re-color an asset either using the new android:tint and android:tintMode on Android L or the good old ColorFilter on all versions.

You can also prevent packaging resources that are only a rotated equivalent of another resource. Let’s say you have 2 images named ic_arrow_expand and ic_arrow_collapse :

You can easily get rid of ic_arrow_collapse by creating a RotateDrawable relying on ic_arrow_expand. This technique also reduces the amount of time your designer requires to maintain and export the collapsed asset variant:

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

P.S. Thanks for reading this far! If you found value in this, I’d really appreciate it if you recommend this post (by clicking the ❤ button) so other people can see it!.