Skip to content

Start Playing with MVVM + Hilt on Android Development

Architecture is one of the important things in all types of development. Agree or disagree with my subjective opinion, you will feel the difference after you try to develop an application between using the architecture or not. Especially if you are working on a large project / product.

Here I will give a simple example (template) about using MVVM + Hilt when you build an android application. I know (perhaps) it’s not best practice, but I think it’s better practice if you don’t use architecture in previous development.

General Knowledge

MVVM (Model – View – ViewModel)

There are some people who still misunderstand about design patterns and architectural patterns, but here we will not discuss about that. Currently MVVM is the default pattern that appears when we create projects in android development. The main thing in MVVM is in the management of data flow management which is no longer executed at the view generation layer. Data management will be left to a separate viewModel class.

This is just a preface, for more details, you can read in Android Document Development


I will not chatter much, I will only quote from the official android developer page about hilt.

Hilt is a dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project. Hilt provides a standard way to use DI in your application by providing containers for every Android class in your project and managing their lifecycles automatically. Hilt is built on top of the popular DI library Dagger to benefit from the compile-time correctness, runtime performance, scalability, and Android Studio support that Dagger provides.

For more information, see Hilt and Dagger.


To using MVVM and Hilt, we need some related dependencies. So you can add this list into your gradle dependency:

Afterward add this into root dependencies

And don’t forget to import plugin depndency on top root gradle file,


Create our Base Project Configuration

On this step, we will providing base service that will call on next our repositories. Usualy on most examples, many developers including this on di folder, but for my self prefer using service for folder naming to this configuration. So whatever it is, let create new class with name BaseService.kt inside that folder like this,

Next, we need to create ApiServices.kt to provide our service path. For the eample I just give a POST api call service with ResGlobal for global model. Why for global model? Yap! because usually for a service they have same structure for the json result.

And this is example for ResGlobal model

Okkay, our base service is already done. For the next step, we will try to create ViewModel and repository, and call the view model on our class.

Implementation View Model & Repository

First, we need to create the repository that will be used on view model calss. So, create new class and give class came with UserRepository.kt. You can also give another name regarding to your needed.

On above class, you can see ResultApi class for handling the result. Why I using that? We need confirmation on the UI(View) about the result is success or not. In this case, i want to showing the error message on the view side, so i bring error message if any and give that as null if the response is success. So this is for the ResultApi class that usually i use.

Our repository class is already done, next we will create the view model class. So create new class and call class with UserViewModel.kt.

Yap, the view model is just a simple class. On this class, we execute function on the repository using viewModelScope that will execute class on repository asynchronously.

Setup View Model In View Class

On this step, we will setup view model on our view class. We will setup how to call the endpoint and manage the result regarding view model and repository that already prepare before. On this example,

Don’t forget to add @AndroidEntryPoint on the top our class. And for using hilt, dont forget to set hilt application and set that on the manifest file. For example here i will create class with name MyApp.kt

And put class name on mainfest file

Now, just runing the app and Cheers 🥂

Give your comment if you have a hightlight with this article 🙌

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.