App optimization: Strategies for reducing your APK size

app-optimization-how-to-shrink-code-in-android

The size of your APK has an impact on how fast your app loads, how much memory it uses, and how much power it consumes. Users are less likely to install your app if the size is too large. If they manage to install it, it will probably be the first app they will uninstall once their storage begins to run out. App optimization is important as it impacts your application’s adoption.

One of the simple ways to make your APK smaller is to reduce the number and size of the resources it contains. In particular, you can remove resources that your app no longer uses, and you can use scalable Drawable objects in place of image files. you could also minimise the use of third party libraries and use use APK size analysers. This post discusses these methods as well as several other strategies for reducing the sizes of your APKs.

Remove unused resources

Unused resources are artefacts in your project’s res/ folder that do not have a reference in your code. These include layout files, images, drawables etc. To remove unused resources from your project, you must be able to figure out which resources are unused. Luckily, the lint tool, a static code analyser included in Android Studio, detects resources in your res/ folder that your code doesn’t reference. When the lint tool discovers a potentially unused resource in your project, it prints a message like the following example

res/layout/settings_2.xml: Warning: The resource R.layout.settings_2 appears to be unused [UnusedResources]

Libraries that you add to your code may include unused resources. Gradle can automatically remove resources on your behalf if you enable shrinkResources in your app’s build.gradle file.

Minimize resource use from libraries 

When developing an Android app, you may use external libraries to improve your app’s usability and versatility. For example, you might include the Retrofit network library or Picasso image loading library to improve the user experience and speed up development time for your app. Often times, libraries you include contain many objects, methods and resources of their own that your app doesn’t need. Their size implication is not worth the value you get by using them. You could edit the source code of the library, provided the license allows you. You could also use an alternative library. And you could avoid it all together and invest some time in building the specific functionality yourself.

Use drawable objects and Vector graphics

You can use vector graphics to create resolution-independent icons and other scalable media and Drawables to dynamically draw the image at runtime instead. Using these graphics can greatly reduce your APK footprint.

Reuse resources

You don’t have to maintain separate resources for the upright, rotated, tinted, shaded versions of the same image. Instead, reuse the same set of resources and customize them as needed at runtime.

For example, you can change the colour of an asset either by using the android:tint and tintMode attributes on Android 5.0 (API level 21) and higher by using the ColorFilter class for lower versions of the platform.

You can also omit resources that are only a rotated equivalent of another resource. The following code snippet provides an example of turning a “thumb up” into a “thumb down” by pivoting at the middle of the image and rotating it 180 degrees:

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

Shrink and obfuscate your app

You should activate shrinking in your release build to remove unneeded code and resources in order to make your app as small as possible. When you enable shrinking, you also get obfuscation, which reduces the size of your app by shortening the names of its classes and members, and optimization, which uses more extreme methods to reduce the size of your app even more. Read more about shrinking and obfuscation

Reduce native and Java/Kotlin code 

You must understand the footprint of every line of code you write, and autogenerated code too. Excessively verbose code will contribute towards the size of your APK. There are several methods you can use to reduce the size of the Java/Kotlin and native codebase in your app, but I will leave this to you.

Use app bundles

This new app serving model has changed APK delivery. You can gain significant APK size reduction simply by following this strategy alone. Your APK might contain content that users download but never use, like additional language files or per-screen-density resources. When you use app bundle, Google Play will build the APKs and ensure users download only the resources that they need.

You may also like...
[instagram-feed]