Android Development Best Practices

  • Amit Shekhar
    Amit Shekhar
    Published on
Android Development Best Practices

I am Amit Shekhar, a mentor helping developers in getting high-paying tech jobs.

Let’s explore some of the best practices in designing and developing Android apps.

For starters, when you develop an Android Application, you should prepare it to be run on a wide variety of devices and under circumstances.

Every Android device is a family in itself. When one member of the family goes haywire, it means the rest of the family is also likely to be affected.

Did you know that when any single application asks for memory, if there isn’t any free memory, Android will respond by closing other apps so it can provide that memory? Then if the user switches to that application, it will have to be restarted.

When any single application takes up more memory, using more CPU and GPU, or consuming more battery, every other application suffers as a result. And this has the potential to wreck the user experience for the entire device. Hence, performance matters.

Beware of little expenses. A small leak will sink a great ship. — Benjamin Franklin

Here are some of the best practices you should follow when building Android apps:

When you start the Android Development, you do not maintain the coding standard and do not follow any architecture. After adding more and more features, you start facing the following problems:

  • Difficulties in the addition of new features.
  • Introduction of more bugs.
  • Overall slow development.

These problems can be avoided by following the coding standard and following some architecture in your Android project. Learn about the Android Application Architecture from the following resource:

Always maintain the code quality

Quality matters a lot. It’s not only about MVP/MVVM/MVC/etc., but also about each piece of code in each part of your app. Learn about the Android Code Style And Guidelines.

Read other's code: Start reading other's code to learn the best practices. Believe me, reading and learning from other's codes is the best way of improving yourself.

Read open-source project to learn:

  • Coding style
  • Language specific features
  • Architecture Design
  • Utility function
  • Naming convention
  • Optimization done

Detect and Fix memory leaks in Android App

Learn to detect and fix memory leaks.

Use Proguard in your release version

This will remove all your unused code, which will reduce APK size.

Use debugging tools

I highly recommend using Android Debug Database. This library will be your best friend. It is a powerful library for debugging databases and shared preferences in Android applications. It’s a very simple tool for viewing databases and shared preferences directly in your browser.

Use strings.xml

Adding text as string resources are always useful in the long-run, especially when support for new languages needs to be added.

Create separate layouts for UI elements that will be re-used

Use the include tag in xml. Another handy tag is the merge tag. It acts as a pseudo parent and helps get rid of an unneeded root ViewGroup.

Place launcher icons in mipmap-folders

When we build separate APKs for different densities, for the APK of the particular density, the drawable folders for other densities get stripped. This will make the icons appear blurry on devices that use launcher icons of higher density. Since mipmap folders do not get stripped, it’s always best to use them for including the launcher icons.

Use shapes and selectors instead of images as much as possible

Using shapes and selectors will further reduce APK size.

Avoid deep levels in layouts

A deep hierarchy of views makes your UI slow, not to mention making it harder to manage your layouts. Deep hierarchies can mostly be avoided by using the correct ViewGroup. Use Constraint Layout.

Use HTTP library

Use 3rd party HTTP like Retrofit or Fast Android Networking, depending on your use case.

Use the Parcelable class instead of Serializable when passing data in Intents or Bundles

The serialization of an object that implements the Parcelable interface is much faster than using Java’s default serialization. A class that implements the Serializable interface is marked as serializable, and Java serializes it using reflection (which makes it slow). When using the Parcelable interface, the whole object doesn’t get serialized automatically. Rather, you can selectively add data from the object to a Parcel using which the object is later deserialized. Similarly, you can consider the Parcelable in Kotlin.

Perform file operations off the UI thread

File operations should always be performed on a background thread, typically by using RxJava, Kotlin-Coroutines. They take time, and if done on the UI thread can make the interface feel sluggish. In situations where they block the UI thread for 5 seconds, an Application Not Responding warning will be triggered and shown to the user.

Understand Bitmaps

As it takes a huge amount of memory, it can lead to OOM easily. Users love content! Especially when the content is well-formatted and looks nice. Images, for instance, are extremely nice content, mainly due to their property of conveying a thousand words per image. They also consume a lot of memory. A lot of memory!.

Understand the Context in Android

Learning what is the Context in Android and using it correctly is important to avoid memory leaks in Android. Learn from here.

Use styles to avoid duplicate attributes in layout XMLs

If you want to avoid duplicate attributes, use styles.

Use Activity LifeCycle correctly

When you use the Activity lifecycle correctly, you can solve most of the problems in Android App Development.

Always include unit tests

This is the most important part of application development. I recommend running unit tests on the JVM because it’s much faster than running them on the Android device itself or an emulator. If you need to mock some objects while testing, use Mockito. Use the Dependency Injection Framework(Dagger) in your Android Project to make testing easier.

Always include functional UI tests

Functional tests check the functionality of your app from the user’s point of view. They launch your app and test its functionality. Here you can use Android Instrumentation if your application is not going to interact with other applications, as it runs only with your application. If there is an interaction with other apps use UIAutomator for testing this functionality.

Optimize your build speed

Long build times slow down your development process. Start optimizing for the build speed.

Secure your Android Application

As you all know that, any Android app can be easily decompiled, reverse-engineered. It’s your responsibility to make your Android app secure as much as you can. By making your app more secure, you help preserve user trust and device integrity.

Make your Android application bug-free

Always integrate a crash reporting library like Firebase Crashlytics in your application. Develop the ability to read through the crash reports to evolve a better and bug-free app for the users.

Use proven libraries instead of your own solutions

In general, use proven libraries instead of your own solutions.

Add Analytics to Your Android Application

Check if you have added the correct analytics at all the places where you had meant. The best way to check is that just think of what data you need and what are the metrics you want to measure, see if you can get those data and metrics from the analytics that you have added or not. If you are able to get all of those then you are good to go.

Develop for all devices

Do not develop for only one device. Develop for all devices.

Test on various OS versions

And again, finally, keep testing on various OS versions.

Show your love by sharing this blog with your fellow developers.

Master Kotlin Coroutines from here: Mastering Kotlin Coroutines

That's it for now.


Amit Shekhar

You can connect with me on:

Read all of my high-quality blogs here.