Android Architecture with Kotlin, Room and more in 2021
In this article we will be covering Android development, including the best practices of MVVM with Retrofit, Coroutines, Room, Koin, and more.
We will be guiding you through a simple project on Android along with detailed demonstrations and explanations.
View and ViewModel
And how to start a new project.
Model-View-ViewModel is a structural-design-pattern architecture. For example, MVC which is used in web or MVP that was previously used in Android.
Initialization of the project: let’s start by setting the language to Kotlin and check the “Use androidx libraries” in order to comply with the latest Android trends.
Make application runnable: it will allow us to test our application quickly. So on the left, it’s an empty activity. And on the right, it’s declaring that activity has storing.
Add ViewModel and Data Binding. The ViewModel will only contain LiveData objects which are just a values that can be observed by the view with the observe method. Then, we will pass the ViewModel reference to the view using DataBinding. In the TextView, we can see that its text is set to the message from the view model observable object.
Instantiate ViewModel and set it to the layout using the data binding library. To do that, we have useful classes which are binded by jetpack libraries. So, what we are using right now is ViewModelProviders which creates an instance of view model and that uses data binding to hide the binding of the layout.
Coroutines and Retrofit (Model and Retrofit)
Add Retrofit and Coroutines libraries: For Retrofit, we have three libraries. First is that main library which allows you to perform requests on the REST API. The second one, converter-moshi which will help us to convert the JSON into one of the models of our application. And, logging-interceptor is just a library which will allow us to have some logs.
Add Retrofit service: Retrofit allows us to request the HTTP method of the API.
What is important here is the suspend keyword in the beginning of the method as it’s part of coroutines which will run our request in background. Since it’s an API core so it may take time. However, if you are using coroutines, you cannot use the suspend function directly in the UI thread.
Call API From the viewmodel, viewmodel scopes will be in detail which is provided by the lifetime ktx libraries. Then, we will do a launch which we need to do, in order to do a network call on the IO thread.
Finally, we get to the API service which is in red because it is not really a good practice. For example, if you have 10 models, you don’t have to instantiate every time the retrofit instancing each of them. That is why it is useful to use a dependency injection library.
Adding Koin Library to the build.gradle file of the module.
Add Network Module Now we want to provide our retrofit instance. To do so, we will just add network module value which will not be in any class. It will be a value with a provider method in a dedicated network module.
And this will have a single retrofit which means that this dependency will be single terms in order to not create a new instance every time we want to get a retrofit instance. Then, it is using the builder to use the base URL and the converter to validate the converter that can convert JSON to our model.
Also, we can use that instance to get a single term for our API service and retrofit instances so we can see how to use another provider. Type of object also needs to be set and then we can create it without an instance of the API service.
Start Koin We need to start Koin in the application class. By setting the context that we use in the data analysis which will be the application itself on the list of models. So when they have a network model, we will use that one.
Add MostViewed Module which will be a module to provide a MostViewed model. It is important to notice that Android knows when to instantiate the new model and when it is not needed. And you can use the perverse instance which is made directly by Android. So we can use the view model provider which will use exactly the same scenario for making a new instance or not.
Base Activity is where our activity will extend, just in order to set the modules in the constructor because we have the current modules in the application which we also need to have current models which are dependent on activities.
Back to MostViewedModule we need to load the modules or screen so we can share it in the constructor of the activity. We simply load the modules in the onCreate method of the activity, then the onDestroy, of course, unload it. We are sure that we don’t load too many modules. Moreover, on the bottom right, we have majority which it’s time based activity with a must give module which was created before and a viewmodel.
It has many advantages since our view model does not need to be late for its using binary modality, same as using by legacy and so on. It can be valid, and this will be instantiated only when we need it.
Call API now the API service is an argument of the constructor which also will be the property of the class which we use to get the bust fill out so we won’t need to generate it again.
Add Room Library So the room runtime is the main library. Also, KTX is the library which is needed for room to be able to work with coroutines. Because without a suspend function, we need to use light data. As well as KAPT, is the annotations processor.
Add DAO We will add the database access object. As you can see, with the first method which is inserted, we don’t use any SQL at all.
There are methods to insert your article and yet for updating an article. One will set an article as read. Another one just for updating the logical one, which we read from the API. This method will update the article if it needs to be updated unlike read methods.
Add Database Database will be an abstract class with a method to read out DAO classes.
Why is it an abstract class and not interface?
It is just to allow us to have some non abstract methods in order to populate class data of our database.
Add Repository In the new model, we gather data from the viewmodel using the API service. However, it is not a good practice because data can come from the network, from the API, sometimes from both of them and so on.
It is why we want to separate it to have some classes in which we can use all providers of data such as database instance. Then, in order to get the list of journal articles we use the convert method of the model to turn it into a list of objects.
Update Module We need to update the module in order to either repository module, which will take the retrofit instance in it.
Router allows us to move from an activity to another because it is a part which is logic. So, it must be in the viewmodel that actually an android moving from an activity to another one will need the context. In order to get that context, we will need a router class and the module.
Add Router class we will just have a router interface which will open the web view or ViewedArticle. Then, we will implement that interface with another class which will take as an argument activity. With that activity, you can override the openWebView method using the instance of the activity.
Add Module Start by using parameters of activity, so you should set it as an argument and then you will use it to instantiate what you need in activity.
Sign up today & start getting tech news.
Get the latest tech trends directly in your inbox each month. And get invited to exclusive events.