How to reduce Android APK size

How to reduce Android APK size

APK (Android application package) is the package file format for distribution and installation of Android mobile apps. It contains all of the application’s code, resources, assets, manifest file and other application’s files. APK is a type of archive file, specifically it is based on the JAR and ZIP file formats.

APK size really matters when going live to the Play store.

Step 1. ProGuard

ProGuard is the most prominent enhancer for Java bytecode. It optimizes bytecode and removes unused instructions. It renames the classes, fields, and methods using short meaningless names which makes code safer from reverse engineering.

This tool is used worldwide as shrinker, optimizer, and obfuscator.

So, what do these terms (shrinkage, optimization and ofuscation) means?

  • Obfuscation. When we compile bytecodes, all the debugging information like classes, fields, methods, and attributes etc. are included. This makes it very easy to decompile and reverse engineer the code. To stop this from happening, ProGuard uses the technique called obfuscator. Here, all the debugging information will be replaced by special characters making it very difficult to read and understand and at the same time the internal functionality remains the same.
  • Shrinkage. The unused resources and libraries can be removed automatically at the built-time when packaging the .apk file.
  • Optimization. Optimizing works on compiled classes to implement many small optimizations based on the java version. By default the proguard-android.txt that ships with the Android tools has optimizations turned off, but the proguard-android-optimize.txt has the presets if needed.

To enable proguard open build.gradle file in Android studio and add minifyEnabled true and shrinkResources true.

But remember shrinking process of code slows down the time of build. So you should only use it when you are going to release your app or if you are giving it for testing.

android {
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}

minifyEnabled is responsible for shrinking your code files (e.g. Java). It is the main feature of ProGuard, and helps to shrink your APK as well as making it difficult to reverse engineer.

shrinkResources is used to remove unused resource files (such as images and other assets). For example, if you are compiling with an Android library, but you are not using some images in that Android library, they will not be included in the final build.

The getDefaultProguardFile('proguard-android.txt') method obtains the default Proguard settings from the Android SDK tools/proguard folder. Android Studio adds the proguard-rules.pro file at the root of the module, which helps to add custom Proguard rules.

There are some problems with ProGuard stem from the inclusion of third-party libraries in your project. You can find solution in (android-proguard-snippets)[https://github.com/krschultz/android-proguard-snippets].

Step 2. Recommended Media formats

Some times applications are heavily reliable on images, audio or video, so another way you can reduce the APK size is by using certain media formats. It is highly recommended 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).

Know more about Supported Media Formats.

Step 3. Optimize PNG images

A great way to improve the performance of your app is to optimize the size of your images. Smaller image sizes require less memory usage, disk space, load time and most important reduce APK size. 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. You can also use some online image optimization service TinyPng.

Step 4. WebP format

Another great way of reducing the storage requirements for your graphics is to convert them to a WebP format. It is suitable for replacing both JPG and PNG files and often produces much smaller images.

Being quite a new format though, it is only supported in Android 4.0+. Newer features (such as transparency and lossless compression) are supported since Android 4.2.1+ only.

Step 5. Reuse resources

Reusing stuff is probably one of the first important optimization you learn when starting developing on mobile. It will not even reduce your APK size but also save your time to develop the same thing again and again.

For example, you can include a separate resource for variations of an image, such as tinted, shaded, or rotated versions of the same image. But it is recommended, that you reuse the same set of resources and customize them as needed at runtime.

Android provides several utilities to change the color of an asset, either using the android:tint and tintMode attributes on Android 5.0 (API level 21) and higher. For lower versions of the platform, use the ColorFilter class.

The following code snippet provides an example of turning an "expand" arrow into a "collapse" arrow icon by simply rotating the original image 180 degrees

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/ic_arrow_expand"
    android:fromDegrees="180"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="180" />

Step 6. Google Play Services

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 Map and Android Wear APIs, replace the following line in your build.gradle file

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

with these lines

compile 'com.google.android.gms:play-services-maps:10.2.0'
compile 'com.google.android.gms:play-services-wearable:10.2.0'

Here you can see explore more Api’s of Google Play Services.

comments powered by Disqus