How to build an android app in a professional way — Set it up in a professional way
Tags: android
Published: 2020-01-08 10:50:54

Android developers with some hands-on experience and familiarity with the android framework usually look forward to how to write their android application in a professional way and also how to write code with the most minimum amount of bug. Here i'll be listing what an android developer should do on an android project to make it professional and with less bug.


When I say professional, I don't mean that it's the way every android developer in the industry writes their android application, but that if you write it this way, your code can be easily understood by most android developers and makes it very maintainable such that anyone can jump onto your code base and make a contribution easily without much hassle.


1. <b>Architectural pattern</b>
This is just about how you structure your project files and how you write your code. Most likely you've heard of patterns such as MVC, MVP, MVVM, MVI… You must be wondering which one is best, well there's no one that is best. The pattern you choose should depend on your team and project, however the most adopted pattern in android is the MVVM pattern.
[Here](https://codelabs.developers.google.com/codelabs/android-training-livedata-viewmodel/) is a lab tutorial showing the use of MVVM.

2. <b>Follow SOLID principle</b>
If you are familiar with OOP concepts, then you should know the SOLID principle. It is an abbreviation for
S - <b>Single Responsibility Principle</b>: A class must have one and only one responsibility.
O - <b>Open Closed Principle</b>: A class should be open for extension but closed for modification.
L - <b>Liskov Substitution Principle</b>: Objects in a program should be replaceable with instances of their subtypes without altering the correctness of the program.
I - <b>Interface Segregation Principle</b>: An interface should fulfil only the purpose of its implementation class.
D - <b>Dependency Inversion Principle</b>: High level objects should not depend on low level objects but on abstraction. Abstractions should not also depend on details but details should depend on abstraction.<br>
[Here](https://android.jlelse.eu/solid-principles-the-definitive-guide-75e30a284dea) is a good article showing implementation examples of SOLID principles.
3. <b>Dependency Injection (Dagger/Koin)</b>
This is about providing the dependencies a class needs without having to initialize that dependency in that same class. We can implement dependency injection in our project manually, however there are libraries that have been created to help abstract away complexities of implementing dependency injection. Dagger and Koin are popular libraries that help with dependency injection in an android project.


4. <b>Crashlytics</b>
When you release an app to users on play store, there are likely chances that your app may still crash on some users device probably due to memory issues, device compatibility or some parts of the user actions not handled by a developer. In order to know about crashes that happen to your users, it is best to setup crashlytics in your project so you can track the causes of crashes from your users and know how to deal with them in your next release.
To setup crashlytics, there are helpful libraries such as Firebase crashlytics, App center crash reporting and so on that can be setup. When a crash occurs, it will appear on your crashlytics platform dashboard with the crash information.


5. <b>Testing and Code coverage</b>
It is advisable to write tests(unit, ui and integration tests) for your project to help prove the correctness of your code. Android has as default, JUnit and Android Instrumentation Runner to help with running tests on android.
It is important that you write tests for all cases of your functionalities and this can be confirmed by using having a good code coverage library. A library to help with code coverage in android is Jacoco. [Here](https://www.youtube.com/watch?v=RgRiMPu_1A0) is a youtube tutorial on how to use jacoco in your android project.


6. <b>CI/CD (Continuous Integration and Continuous Deployment)</b>
CI/CD tools help developers maintain good code quality, successful project builds and successful delivery of their product to their users.
Platforms such as appcenter, nevercode.io, fastlane, bitrise helps with this CI/CD processes. When we commit an android code to a repository, it helps build the project and test the application on different devices and then publishes it to play store, depending on how we setup the platform we intend to use.


7. <b>Logging(Timber)</b>
During development we usually need to log some information(e.g Log.d("imageview loaded")) on the console, however we need to take them away before releasing for production. Having to clear up every Log.d in every file in the project can be cubersome especially when the project is very large. The Timber android library is one that helps to solve this issue, you just need to add the dependency to your project then set the timber library to be used in only debug mode during the initialization of your application in your code.
You can then log with the Timber library (e.g Timber.d("imageview loaded")) and when you build for production, the library will disable logging in production automatically.
[Here](https://www.androidhive.info/2018/11/android-implementing-logging-using-timber/) is a tutorial on how to use Timber