Developer’s Commute Newsletter #3

Hitesh Kohli
4 min readOct 10, 2023

--

Android Core Concept

Android Interview Huh?

If your interviewer asks “ Explain Dagger Hilt? “

Here is how you do it:

What is Dagger Hilt?

  • Dagger Hilt is another library that allows us to implement Dependency Injection
  • Dagger Hilt is easier than Dagger 2
  • The hilt is just a wrapper around Dagger 2
  • For now, it has become a standard way to implement DI
  • Behind the scenes, it generates Dagger code
  • Knowing about Dagger 2 will make it easier to understand, But you don’t necessarily need it

Why is it needed?

  • Simply because Dagger 2 has a lot of methods and implementations
  • Hilt just streamlines it

To implement Hilt inside your app

  • You need to add dependencies and some plugins like Kotlin-Kapt
  • Kapt -> Kotlin Annotation Processing Tool
  • Dagger works similarly to Dagger 2 with Annotations

How does Dagger Hilt streamline Dagger 2?

  • Annotations provided by Hilt are standardized
  • You don’t need to implement your own annotations like in the case of Dagger 2
  • These standard annotations form a layer on top of other annotations
  • Therefore one annotation acts as the parent of another and it becomes its sub-annotations that inherit from the parent
  • To understand more pls refer to the documentation

What is @ HiltAndroidApp ?

  • This annotation is always placed in the application class (application Context)
  • Other annotations take the application context from this annotation
  • It simply generates the Dagger code behind the scenes

What is @ AndroidEntryPoint?

  • This annotation will be on top of your main activity
  • It tells Dagger about the Entry point of the injections
  • It simply tells us where the dependency needs to be injected
  • An important thing to remember is if you use @ AndroidEntryPoint on a class
  • And if other classes depend on that class
  • Those classes also need to have @ AndroidEntryPoint

What is @ Inject?

  • In order to give a dependency to AndroidEntryPoint (of Repository, services etc.)
  • We use @ Inject
  • It simply means in order to construct this object please run his constructor

What is @ Module?

  • Suppose you want to access an object that is using a third-party service (Like calling API from Retrofit)
  • Hilt does not know how to create that object because it does not have any @ Inject constructor
  • Here comes the usage of @ Module
  • It tells us how to create that third-party object
  • We use @ Provides to create a third-party object (Like this retrofit call)

Now what is InstallIn(SingletonCompoenent::Class)?

  • Here is where Hilt shines over Dagger 2
  • Remember there were many components in Dagger 2
  • Now these components are layered on top of each other making it easier to access
  • We need to provide Singleton because we need to tell Dagger to please not make the same object again and again
  • When activities are created or destroyed
  • Because it will become expensive (especially in the case of Retrofit call)
  • Therefore we use Singleton which allows us to restrict object creation
  • Singelton Component is applicable in the Application Context
  • We can also create an Activitycompoenent or fragment Component if we need to

How does the hilt work in this case?

  • First, it checks if is there any @ Inject constructor to build the object — NO
  • Secondly goes to the activity component is there any @ provides
  • here — NO
  • Thirdly goes to the activity retained component are there any @ provides — No
  • Fourthly goes to the Singelton component — Here he finds the @ provides
  • Constructs the object and injects it where it is needed

Daily Coding Meme

--

--

Hitesh Kohli
Hitesh Kohli

Written by Hitesh Kohli

Hi, my name is Hitesh Kohli, I work at Geeks for Geeks as an Android developer. I love messing around with apps and games.

No responses yet