My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Android Development best practices.

Android Development best practices.

John Paul Seremba's photo
John Paul Seremba
·Feb 3, 2018

Architectural Patterns

One of the most important things that you should focus on while developing a world-class app is the separation of concerns. It is good to separate and define clear roles for each component in your app. A class shouldn’t be a multi-tasking component. Many developers make a mistake of writing almost all their code in an Activity or Fragment. Any code that doesn’t handle a UI or operating system interaction should not be in those classes.

Having a good design pattern saves you lots of problems. A good pattern ensures that all your code is organized and thoroughly covered by Unit tests. It makes debugging easier, since you know where to look for a particular problem. It also greatly improves the maintainability of your code.

There several architectural patterns, but the most popular being;

  • MVC (Model View Controller)
  • MVP (Model View Presenter)
  • MVVM (Model View ViewModel)

Libraries

Ben Jakuben, in his post about Android Libraries clearly states that;

“A good developer knows to never reinvent the wheel, unless you plan on learning more about wheels!”

There are tons of open source libraries that developers can freely use in their apps. Some of these are so helpful that you shouldn’t start any App without them. One line of code in your app’s build.gradle is sufficient to include any library of choice automatically. Below, I’m going to casually introduce some must know libraries that make your Android development life fun and much easier.

1. Retrofit

This is my favorite library. It is the best library for making REST API calls. It makes HTTP Requests easy through annotations and automatically handles JSON parsing through the use of POJO classes. All you need to do is to define an API interface and a Retrofit class that automatically generates an implementation of your API interface. In your interface, you use annotations to describe the HTTP request.

2. GSON

GSON is a library that coverts Java Objects (POJOs) into their JSON representation. It can also be used to convert a JSON string to an equivalent Java Object. The library provides simple toJson() and fromJson() methods to convert Java objects to JSON and vice versa. However, when using the Retrofit library, you never have to make direct calls to GSON. The Retrofit class handles all that automatically when you add GSON as the converterFactory. More information about the same can be found here.

3. ButterKnife

Butterknife uses annotations to inject views by creating boilerplate code for you. It is small, simple and lightweight. It makes your code more readable on top of being easy to use.

4. Dagger2

Dagger2 is a dependency injection framework, built on a simple concept called Inversion of Control. According to that concept, a class should not configure its dependencies statically, but should be configured from the outside. In Java, a class has a dependency on another class, if it uses an instance of that class. This is referred to as class dependency.

Since dependency injection in most world class Apps is a must know, Dagger2 is a true solution for problems that may arise during providing dependencies to particular classes. Ideally, classes should be as independent as possible from other classes.

Dagger2 uses code generation and is based on annotations. The generated code is very easy to read and debug. The annotations used are; @Module, @Provides, @Inject and @Component among others.

For more information about dependency injection, Vogella has got you covered here.

5. Glide

If you have used Picasso before, trust me, Glide is a better option for fast and efficient image loading. Glide is a fast and efficient media management and image loading framework that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface.

In simple terms, Glide takes up the burden of managing your media files especially if you are loading files from a network to itself. It automatically manages memory consumed by the image loading process and also automatically does caching for you, in a guise of reducing the pinch on network resources.

6. Room

In order to build world-class apps, you need a clean ORM mapping library. Room was introduced during Google I/O 2017 as a persistence library aimed at cleaning up architecture in developing Android Apps.

Room in simple terms is one of the best SQLite object mapping libraries. It provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

Persisting data offline greatly improves the experience of your users in cases where the internet connection is saggy. The goal of every app developer is to ensure that users have a seamless experience when using your App. Room hence bridges the gap by providing the easiest mechanism for data persistence.

7. RxJava

RxJava is one of the best libraries for enabling Reactive Programming in Android development. It is a framework for simplifying concurrency or asynchronous tasks. Multi-threading most times is a pain to developers and if not correctly implemented, it can cause some very difficult bugs to fix. RxJava comes in to make your life easier and help you avoid the nasty memory leaks.

Do’s & Don’ts

Do’s,

  1. Design for multiple screens
  2. Consider supporting multiple languages
  3. Provide your users with a seamless user experience
  4. Do performance tests
  5. Use user analytics tools
  6. Do Performance & Memory usage monitoring

Don’ts

  1. Develop for you device
  2. Reinventing the wheel
  3. Not using intents
  4. Not using fragments
  5. Blocking the main thread
  6. Not assuming success

For more information, here is the full article; medium.com/the-andela-way/billion-tips-for-..