Most professional Android application developers are aware of the Guide to App Architecture, which is a great source of knowledge for building Android applications according to best practices. If you haven’t read that yet, I would suggest you do so before continuing on with this article.
You’ll want to be familiar with the Android framework and have a basic understanding of Android architectural components (Livedata, viewmodel, Room), Retrofit and Dagger2.
Once you’re familiar with the clean app architecture referenced in above guide, you might feel eager to implement the architectural patterns you learned in a new project. Keep in mind that building an architecturally sound app is a time-intensive process, and that you’ll likely spend more time dealing with issues with the interfaces and other things than you will building your business logic.
At AppIt Ventures, our mobile app developers are committed to developing each Android application with the best-suited architecture for that particular project. To speed up the Android application development process while still ensuring that you’re building quality code (code that is testable, maintainable and scalable and follows the best architecture patterns), our team has created a custom template that can be used as a starter package when you are creating a new Android application. In less than 10 minutes, you will have the clean app architecture applied to your new application.
Let’s do a quick overview on all the components we have used in the template so that when you go to build your Android application, you are aware of the purpose of each of them.
Activity or Fragment: The most common mistake beginners make is to write all of the code (UI, data, network) in an activity or a fragment itself. As activities and fragments are considered UI related classes, these classes should only contain logic that handles UI and OS interactions. By keeping these classes as lean as possible, you avoid many lifecycle-related problems, your code remains testable, and you can swap any business logic or data source.
Repository: Most of the application will display data to the user, so the data origin/source can be a local offline database or a remote server. To keep the UI classes lean, our UI shouldn’t be aware of the data source and it’s logic, so the repository classes are used to manage the data and data operations.
LiveData : LiveData is an observable data holder class. Unlike a regular observable, it is lifecycle-aware.
ViewModel: This provides data to the UI, acting as a communication center between the Repository and the UI. These objects are automatically retained during configuration changes (orientation changes).
Retrofit (Remote data source): Retrofit is a REST API Client. It is a simple library that is used for network transaction.
Room (Local data source): The Room persistence library provides an abstraction layer over SQLite to allow for more robust database access.
Dagger 2 (Dependency Injection Framework) : Dagger 2 automatically constructs objects by walking the dependency tree, and it provides compile-time guarantees on dependencies.
Recommended Architecture — you can find this diagram in Guide to App Architecture
Now that you know what we are trying to achieve and have knowledge of each component used, let’s move ahead and see how you can use our custom template to apply app architecture to your application.
AndroidX is new package structure for Android support libraries like support, databinding, design etc. To use AndroidX in your project, we need to set the targetSdkVersion for your project to 28 and add the two below-mentioned lines in gradle.properties file.
Sample app created using custom template
Now when you run the project, you can see the bottom navigation bar has two fragments.
Select Local (Room) — Local Fragment — click the InsertRow button to insert a dummy record in the room database and retrieve records from the database.
Select Remote (Api)— Remote Fragment — shows a list of data coming from a remote API using retrofit.
Now you know how to quickly create a new application with clean app architecture in place. For now, the template below just contains the starter package that will help you speed up your initial development. Later on, we can also create a custom template for activities and fragments, so when you want to create a new activity or fragment you can apply the architecture applied to these.