You are viewing our old blog site. For latest posts, please visit us at the new space. Follow our publication there to stay updated with tech articles, tutorials, events & more.

Lean Apps

0.00 avg. rating (0% score) - 0 votes

At Naukri we always target to keep the the app lean in terms of size.

Size can be measured as:-

-APK download size: Users often avoid downloading apps that seem too large, 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 phone : Smaller the app size, more are the chances when competing with remaining apps for staying in 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 pngs with SVG
  3. Used aar files and remove unwanted 3rd party libs parts
  4. Remove unused resources
  5. Running lint to check unused resources and delete them

Remove unused language resources

Normally 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 languages 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 kbs. Following is the line we have to add in our build.gradle

resConfigs “en”

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

Remove repeated multi color pngs with SVG

Earlier we had to add multiple pngs with different colors of same png and it was difficult 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 pngs with SVGs.

We had to do some extra work to support these SVG in OS versions below 4.4 and after that the app size actually 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 analysed 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 included 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 increases 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 actually made our app surpass the number of method limit. So we made our own custom library that is only giving the feature required that not only decreases the method but also decreased our app size.

Remove unused resource

Proguard is an extremely 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 basically goes through your all of your code paths to detect the snippets of code that are unused. All the unreached (i.e. unnecessary) code is then stripped out from the final APK, potentially radically reducing its size. Proguard also renames your fields, classes and interfaces making the code as light-weight as possible.

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

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.

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

Lint is a static code analyzer that helps you 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.

Now we run lint before every relese to check the unused resourses 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 smart phones 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 are 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 2 mbs.