banner



Best Android App Code Design

Android Development Best Practices

Android Development Best Practices

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 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. If you want to learn more about Android performance, start here.

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:

Use the recommended Android architecture

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 resources:

  • MVP Architecture
  • MVVM Architecture
  • Mastering Design Patterns in Android with Kotlin

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 from here.

Detect and Fix memory leaks in Android App

Check our resources to detect and fix memory leaks:

  • Practical Guide To Solve OutOfMemoryError in Android Application
  • Detecting and fixing memory leaks in Android

Use Proguard in your release version

This will remove all your unused code, which will reduce APK size. Master Proguard in Android from here.

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. Also, check the new Build Analyzer Tool in Android Studio.

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

Then 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. Read here about it.

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. Learn from the following resources:

  • Part -1
  • Part -2

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! Learn from here.

Learn How The Android Image Loading Library Glide and Fresco Works?

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. Learn from here.

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. Learn how to use a dependency injection framework from here.

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. Learn to optimize your build speed from here.

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. Check the App security best practices.

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.

Happy Learning :)

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

Click here to share this blog on Twitter.

Also, Let's become friends on Twitter, Linkedin, Github, Quora, and Facebook.

Best Android App Code Design

Source: https://blog.mindorks.com/android-development-best-practices-83c94b027fd3

Posted by: hayesaltylets.blogspot.com

0 Response to "Best Android App Code Design"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel