Now Reading
Reduce Application Size with Android studio

At Naukri, we always target to keep the app clean regarding size.

Size can be measured as:-

-APK Download size: Users often avoid downloading apps that seem too significant, particularly in emerging markets where devices connect to often-spotty 2G and 3G networks or work on pay-by-the-byte plans.

-App size on the phone : Smaller the app size, more are the chances when competing with remaining apps for staying on the user’s phone for a long time.

We regularly analyze the components which we can optimize or remove to reduce both. Some of the best practices we use are:

  1. Remove unused language resources
  2. Remove repeated multi-color pages with SVG
  3. Used aar files and remove unwanted 3rd party libs parts
  4. Remove remaining resources
  5. Running lint to check remaining resources and delete them

Remove unused language resources

Usually apps do not care but the 3rd party libs they are using carry multi-language resources with them which is of significant size. So first we identified the list of words our app supports and added that list in build.gradle of our app. This removed the other language resources included in our app directly or through 3rd party libs.

When we did this the app size reduced by a whooping 700 MB. Following is the line we have to add in our build.gradle

resConfigs “en”

You can add multiple languages by separating it with a coma.

Remove repeated multi-color pages with SVG

Earlier we had to add multiple pages with different colors of the same png, and it is hard to maintain and also increased the app size. When Android introduced the support of SVG, we starting picking it up with our revamps and replaced all pages with SVGs.

We had to do some extra work to support this SVG in OS versions below 4.4 and after that, the app size reduced by around 800kb to 1mb.

Now we use only on SVG and keep on changing the color at runtime to use it at multiple places.

Used aar files and removed unwanted libs

We analyzed that while including a library, we are including some parts that are not even getting touched in our app. So we started using aar files, and that included only the part we needed. For e.g., Instead of including whole Google play services library we added an aar for analytics we included:

compile ‘com.google.android.gms:play-services-analytics:9.6.1’

Some times we include 3rd party libs with in our projects that increase the size and method limits.

We removed one 3rd party lib that was helping us implement some feature but also had many other classes and method that we weren’t using that made our app surpass the number of process limit. So we made our custom library that is only giving the feature required that decreases not only the process but also decreased our app size.

Remove unused resource

Proguard is an incredibly powerful tool that obfuscates, optimizes and shrinks your code at compile time. One of its main feature for reducing APKs size is tree-shaking. Proguard goes through your all of your code paths to detect the snippets of code that are unused. All the unreached (i.e. additional) code is then stripped out from the final APK, potentially radically reducing its size. Proguard also renames your fields, classes, and interfaces making the system as light-weight as possible.

We used Progaurd in our app that helped us remove unused resources and shrunk our code at runtime thus reducing the apk size.

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

android {

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

Running lint regularly

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 its reference in the R class but keeps the associated image in place.

Lint is a static code analyzer that helps you detect all available 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 the reflection in your code.

Now we run lint before every release to check the unused resources, and that helped us twice by identifying unused images put while development. Deleting them reduce the size by 200 to 400kbs.

Conclusion

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.

APK/App Size is equally important as application performance, and we should never neglect that. We keep these best practices in our checklist to ensure Naukri Android apps are lean when it comes to size. These practices helped us to remove the unused resources and the total app size reduced by two MB.

What's your reaction?
Love It
100%
Like It
0%
Want It
0%
Had It
0%
Hated It
0%
About The Author
AndroidAgent Team

The Best Exclusive Source for All Android updates, Reviews, Latest News, Tech Reviews, How-to, Top10, Extreme V/S, and Much More.
Don’t Forget To Bookmark US!

Leave a Response