Firebase Authentication with MVVM and Hilt: PART I

A quick and simple guide to implementing Firebase authentication with the MVVM pattern and Hilt dependency injection.

Firebase Authentication with MVVM and Hilt: PART I

In this article, I am going to take you through a small tutorial on how we can create an authentication feature for an application using Firebase authentication SDK. The application architecture will be the MVVM pattern with a repository and the dependencies within the application will be injected using Dagger Hilt.

Why MVVM?

final-architecture.png

MVVM stands for Model View ViewModel and it is an android application architecture introduced by the Google Android team at the Google IO 2019 event. The architecture basically was designed to help developers structure their code and separate concerns by breaking down the structure of an application into layers each with its own functions. This leads to more clean code and avoidance of what is known as 'spaghetti code' within a 'god class'.

The term 'spaghetti code' refers to code that has very little to no structure. On the other hand a 'god class' is a class or in our case as android developers, single activity or fragment that contains every functionality required by the application. This can be network calls, running services, communicating with the database, etc. These such classes can make it difficult to test and maintain in the long term. Yeah, we have all gone through this phase during our Android newbie days

Benefits of using MVVM architecture:

  1. MVVM architecture enables developers to easily work with dependency injection libraries and in our case, what we are about to use, Dagger Hilt.

  2. The MVVM pattern also allows simplicity in unit and integrated testing. By using this pattern we can be able to create mocks and fakes of these classes which we can use in our tests. For example, a fake repository class that mimics communication with an API or database dao.

  3. The result is a well-structured code easy to maintain in the long term.

Further breakdown of this architecture check out the developers' documentation here

There are many design patterns and architectures out there like MVP, MVI to mention a few. In my opinion, no design pattern supersedes another it all boils down to the requirements of the project and your comfortability with the pattern. I have used MVVM in many of my personal projects as I am comfortable with it and it has worked for me 100% of the time. However, it is never a bad thing to at least have a little grasp of two or three patterns for the sake of your marketability.

Dependency injection with Hilt:

Hilt helps us to easily inject our dependencies where we need them. It does this by creating a graph of all our dependencies but first, you have to tell Hilt how those dependencies are created by using so-called provides functions. If a dependency is needed somewhere, Hilt just refers to the graph takes the necessary one then injects it into the required class or activity. Hilt handles all the unnecessary boilerplate code we would have to write if we were to inject these dependencies manually and reduces it to about a line or two. If you do not get the gist of dependency injection below find the link to a great YouTube tutorial series on dependency injection by CodingInFlow. He uses a great analogy of a car factory: link
Side note, Hilt is a new and better version of Dagger2. To simply put it Hilt was built around the Dagger framework to make it easy to use and implement dependency injection. Hilt provides an extra layer of protection, if I may say, to Dagger2. For every dagger in real life, you need it to have a hilt to shield you from cutting yourself. The more you know.

Building of the application is on part 2 of this tutorial. Here is the link: Part II