I am an Android Developer. I have over a year of experience in developing android applications using Android Studio. I will prefer to use new technologies for development. I worked on many projects like Quiz Apps, Restaurant Management Apps, Smart E-Tailor Design Recommender apps, Finman App, etc. Kotlin | OOP | MVVM | Room Database | Firebase | REST API | Kotlin Flow | XML | Professional UI buildingThis repository contains simple example covering Dagger2 concepts with MVVM android architecture
Well, According to Uncle BOB’s SOLID principles, D stands for Dependency Injection which says that- Any Class should not configure its dependencies itself, but should be configured by some other class from outside. Means dependencies for a class to use should be passed by any other class, and this process is called Dependency Injection. Dependency Injection makes our code loosely coupled, which is the main important advantage of using it. Once code is loosely coupled we will be having these benefits:-
- Easy Code maintenance
- Easy Code Testability
- Easy Refactoring of our code.
Hilt provides a standard way to incorporate Dagger dependency injection into an Android application.
To simplify Dagger-related infrastructure for Android apps. To create a standard set of components and scopes to ease setup, readability/understanding, and code sharing between apps. To provide an easy way to provision different bindings to various build types (e.g. testing, debug, or release).
plugins {
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
dependencies {
def hilt_version="2.44"
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
}
buildscript {
dependencies {
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
}
}
These are some of the annotations provided by Hilt
we need to apply these annotations to our Application class, It will trigger the Hilt code generation and in the process will create our App Component.
with this annotation Hilt will generate a DI Container for each Android Component so respective components can add the dependencies.
with this, we can do Constructor & Field Injection or we can inject any method. Dependencies injected with this annotation will get added into respective.
Fields can not be private.
It will create a dependency for ViewModel and Hilt will return that later.
It is used as we used in Dagger Modules, when we want to create an object for any component dependency, mostly used for the third party or where we can not do constructor or field injection(classes which we don’t own).
It will create a singleton instance for a dependency, which will be shared across the application.
Can be used to access already defined Application Context, the same way we can also use @ActivityContext to access activity context.