Skip to content

This repository contains simple example covering Dagger2 concepts with MVVM android architecture

Notifications You must be signed in to change notification settings

waqas028/Dagger-Hilt-MVVM-Retrofit-Rest-API-RoomDatabase

Repository files navigation

Dagger-Hilt-MVVM-Retrofit-Rest-API-RoomDatabase

This repository contains simple example covering Dagger2 concepts with MVVM android architecture

Prepared and maintained by Muhammad waqas who is having experience of Android developers.

Hey there! I'm M.waqas 👋

A Passionate Android Developer From Pakistan

GIF

👨🏻‍💻 About Me

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 building

🤝🏻 Connect with Me

Getting Started:

ezgif com-video-to-gif

Why do we need Dependency Injection?

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.

What is Hilt?

Hilt provides a standard way to incorporate Dagger dependency injection into an Android application.

The goals of Hilt are:

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).

Adding dependencies

App-Level Module

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"
}

Project Gradel

buildscript {
    dependencies {
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
    }
}

Hilt Annotations:-

These are some of the annotations provided by Hilt

@HiltAndroidApp:

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.

@AndroidEntryPoint:

with this annotation Hilt will generate a DI Container for each Android Component so respective components can add the dependencies.

@Inject:

with this, we can do Constructor & Field Injection or we can inject any method. Dependencies injected with this annotation will get added into respective.

@AndroidEntryPoint:

Fields can not be private.

@ViewModelInject:

It will create a dependency for ViewModel and Hilt will return that later.

@Module:

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).

@Singleton:

It will create a singleton instance for a dependency, which will be shared across the application.

@ApplicationContext:

Can be used to access already defined Application Context, the same way we can also use @ActivityContext to access activity context.

About

This repository contains simple example covering Dagger2 concepts with MVVM android architecture

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages