A Simple Android App using MVVM & Dagger HILT
A article that will explain and help you understand how you can use Dagger HILT in your next project be it big or small 🙌
What is MVVM?
A simple explanation,It is google's recommended architecture which practices SEPARATION OF CONCERNS , what this means is , every layer (Model / View / ViewModel) is independent from each other.
You should never write all your code in one activity or fragment, to avoid lifecycle related issues. We also do this so that everything is easily testable and readable.
The parent has direct relation to the child, but the child has no direct relation to the parent.
What is Dagger-HILT?
If we need to use a method from a different class we usually create objects of that class in the class we need that in, but with DI we outsource this work to (in this case) to Dagger HILT . You can think of DI as the middleman in our code who does all the work of creating the preferred object and providing it to the class which needs it.
Ok let's get started
We will be accessing a open source api called https://github.com/ajzbc/kanye.rest which will return a new quote of kanye west as a string and display in our activity.
Full project github repo- https://github.com/zaidzak9/MediumExample
Prerequisites
A basic understanding of coroutines and liveData/StateFlow will be needed along with this(ignore this if you already do👍)
My Article on coroutines- https://zaidzakir.medium.com/coroutines-in-android-explained-8f00a37c8528
Article on liveData & StateFlow-https://developer.android.com/topic/libraries/architecture/livedata https://developer.android.com/kotlin/flow/stateflow-and-sharedflow
- Add these plugins & dependencies in your build.gradle(:app)
2. Add this classpath in your build.gradle(:project) inside dependencies
classpath "com.google.dagger:hilt-android-gradle-plugin:2.28.3-alpha"
3. Create a MainApplication.class and declare it in your manifest
What did we do here? @HiltAndroidApp- We create a application class which hilt uses to setup a lot of its own classes that can use dependency injection,the annotation lets hilt attach itself to the application lifecycle.
4. Create a AppModule Class
First i create a constant val with value of BASE_URL that we will be accessing to retrieve the data from , please note that this is not best practice, usually there will be a dedicated class for this, but since this project is created for simplicity of explaining HILT i will declare it here.
@Module- denotes that this class are containers for dependencies that has to be injected Example: RetrofitInstance, RoomDBInstance , and also we specify amount of time that this will live in application lifecycle.
@InstallIn()- tells dagger how long the scope of the module should live for , Here are some lifecycle scopes for InstallIn()
You can figure out the scopes of each of the component through Created at and Destroyed at columns . Also you have to make sure the component and scope matches each other. Ex: SingeltonComponent -> @Singleton .
I used the singleton component and scope so that we are able to use the object of retrofit and provideRepository throughout our app lifecycle.
@Provides- denotes to dagger that we will need to provide the following function as injectable dependency to any class which needs it.
5. Inject your dependencies
Using the @Inject annotation we tell dagger HILT to inject object of type KanyeWestApi which contains the retrofit instance in AppModule, so dagger now knows that it has to be injected in this repository class.
We do the same thing to the viewModel but only difference is we have to use an extra annotation called @HiltViewModel , to tell its parent activity the data received from will be a viewmodel.
6. Finally Add @AndroidEntryPoint to the activity.
@AndroidEntryPoint
class MainActivity : AppCompatActivity() { }
This Marks the activity to be setup for injection with the standard Hilt Dagger Android components.HILT will create a dependency container that will attach itself to this class lifecycle,Currently this annotation supports activities, fragments, views, services, and broadcast receivers.
Full project github repo- https://github.com/zaidzak9/MediumExample
Dagger HILT documentation
For this simple example these are the functionalities that i have used to incorporate Dagger HILT in the project, but it doesn't stop here , there are many other annotations which could be used to expand a project .