Android Development Best Practices

  • Amit Shekhar
    Amit Shekhar
    Published on
Android Development Best Practices

I am Amit Shekhar, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

Join my program and get high paying tech job:

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.

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 different 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

Use the Android Architecture Components

The Android Architecture Components provide a set of guidelines for organizing your app's code into a clean, modular, and testable architecture. This includes the ViewModel, LiveData, and Room components, which can help you write more maintainable and reliable code.

Detect and Fix memory leaks in Android App

Android devices come in all shapes and sizes, with varying levels of performance, memory, and storage. To ensure your app performs well on all devices, you should optimize your code for performance, minimize resource usage, and avoid memory leaks. 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

Testing is an essential part of the development process, and it is especially important for Android apps, which run on a wide range of devices with different configurations. Use both unit tests and instrumented tests to ensure your code works as intended, and make sure to test your app on a variety of devices and Android versions. 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. It is easy to get carried away and over-engineer your app with complex architecture patterns, custom views, and endless abstractions. While these can be useful in certain circumstances, they can also make your codebase challenging to maintain and slow down the development process. Stick to simple, easy-to-understand solutions whenever possible.

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.

Design for Multiple Screen Sizes and Resolutions

One of the biggest challenges in Android development is the wide range of screen sizes and resolutions supported by the platform. To ensure your app looks good on all devices, you should design your UI using flexible layouts and take advantage of the Android Support Library to provide compatibility for older versions of Android. You should also test your app on a variety of devices to ensure it looks and functions as intended.

Use the Latest SDKs and Libraries

Keep your development environment up-to-date by using the latest Android SDKs, libraries, and tools. This will ensure your apps are built with the latest features and optimizations and take advantage of bug fixes and security updates.

Test on various OS versions

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

In conclusion, following these best practices will help you develop high-quality, reliable, and maintainable Android apps that provide a great user experience. However, it is important to keep in mind that every app is different and may require a unique approach. Stay flexible, experiment with new tools and techniques, and keep learning to continue improving your skills as an Android developer.

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

Prepare yourself for Android Interview: Android Interview Questions

That's it for now.


Amit Shekhar

You can connect with me on:

Read all of my high-quality blogs here.