How to make Android application like professional

Do you ever wonder what professional Android application code looks like? Is there any specific architecture applied? What about third-party libraries?  If you ever asked yourself these questions you are looking at blog post just for you. We are going to write down some key features in our code design and give you short examples and explanations on how and why we use them. Also, if you’ve wondered about how do the professional Android applications look like – check out our Android development team’s work on our featured apps!

Let’s see what makes our applications special:

  • MVP architecture
  • Dependency injection with Dagger 2 library
  • ButterKnife library
  • Retrofit and GSON libraries
  • Realm database library
  • RxJava and RxAndroid libraries

As you can see there are many libraries, so let me show you why we use them and why they are awesome.

MVP architecture of the Android application

This is the only feature on the list that is not library, but is very important. It is important for few reasons: this architecture makes code easier to read, easier to maintain, makes it more modular and makes it testable.
MVP stands for Model-View-Presenter. Idea is to separate your activity code into this three categories which are actually three separate classes. View, which is activity itself, will be in charge of activity appearance and user interaction with application. Presenter is responsible for all business logic and in there should be just Java code without any Android specific code. Model is the place where data should be red form file, pulled from database or downloaded from web. These three classes communicate with each other trough interfaces and it looks like this:

How to make Android application like professional

Dependency injection with Dagger 2 library

Another thing that makes application testable is dependency injection. It basically handle object creation for you. It is necessary to show dependency injection framework how to create specific object once and every time you need that object it will be created and injected in activity, fragment or where ever you want. Not only your programing life will be easier but your activity or fragment will be more stable because object creation, which is not trivial operation, will be handled somewhere else.
Dagger 2 is one of the most popular dependency injection frameworks. It is not difficult to understand and is easy to use. All object creations are handled in “Module” classes. Every activity or fragment can have its own module or global module can be used. Can module be used on specific activity or multiple activities depends on “Component” interface which module is part of. Component is place where you can declare where module objects can be injected. Some components can be part of other more general components so then they are no longer components but “Subcomponents”. Following image should clear suspicions if you have some.

How to make Android application like professional
Objects created in Module 1, 2 and 3 can be injected in both Activities. On the the other hand, objects created in Modules 4 and 5 are only reachable for Activity 1. Same goes for objects in Module 6 and Activity 2.

ButterKnife library

Getting views from layout in activity or fragment sometimes can be boring and time consuming spetially if you have many TextViews, EditViews and Buttons to deal with. All those findViewById and casting functions looks ugly and take lot of space. Another think that looks ugly and take space is setting click listener or any other listener on your view. With ButterKnife library you can forget all about this. It will help you bind your Views (even resources) to your activity and still look cool.
Here is an example.

@BindView(R.id.tv_title)
TextView tvTitle;
@BindString(R.string.title)
String titleString;
 
@OnClick(R.id.button)
public void onButtonClick(View v) {
tvTitle.setTitle(titleString);
}

Retrofit and GSON libraries

Retrofit is well known REST client for Android. It is probably the best library of this kind. Framework that this library provides is really powerful and yet still easy to use. Performance and extensibility are top notch too. Speaking of extensibility, you can combine Retrofit with other libraries to achieve better productivity and save you some troubles. GSON is one of these libraries. You can make Java classes that describe structure of JSON response and GSON will parse that response for you. If necessary, parsing can be manually tweaked to fulfill needed criteria. Retrofit and GSON are commonly used by many programmers so there are bunch of examples on the Internet how to use them which is nice too. Another library that we use along Retrofit is RxJava which will be described a bit later.

Realm database library

If you ever worked with SQLLite database in Android you know how painful that can be even if database is really small. Every read, write and update operation most be manually written for every object type separately and usually this is a lot of code. Well in this case, Realm comes in rescue. No more ContentValues and Cursor objects to write and read data from database, all you need to do is tell Realm save me this object or give me that object and that’s it. In just few lines of code database is ready to use and just few lines is required to write or read some object to/from database. Realm is not just easy to use, it is also much faster than SQLLite and has smaller file size. Here is quick example of saving and reading objects to/from Realm.
Saving to Realm:

User user = new User();
user.setName(“Blue Factory”);
 
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealm(user); // for update:  realm.copyToRealmOrUpdate(user);
realm.commitTransaction();
realm.close();
 
Reading from Realm:
Realm realm = Realm.getDefaultInstance();
User users = realm.copyFromRealm(realm.where(User.class).findFirst());
realm.close

RxJava and RxAndroid libraries

As mentioned earlier, RxJava is a library that we use alongside Retrofit to solve some tricky situations. The library is based on Observable pattern and can be used for whatever. Anything you want to observe and make some action afterward can be done with RxJava. It works in a way that you make observable object and then subscribe on that observable. The best part is that library is capable to make observable out of any object. If your observable have to do some time-consuming work on background thread but you need the result on UI thread, no problem, this is possible too. One example of our RxJava usage is requesting some data from different sources (REST and database) or from more REST routes and receiving all data at once where we need it.

Explore next

Related posts