What is Dependency Injection (DI) in Android development?
Dependency Injection is a design pattern that helps provide objects that a class needs (its dependencies) from outside rather than creating them inside the class. This makes code easier to test, maintain, and reuse.
Click to reveal answer
beginner
What is Hilt and why is it used in Android?
Hilt is a library built on top of Dagger that simplifies dependency injection in Android apps. It provides standard components and annotations to reduce boilerplate and make DI easier to implement.
Click to reveal answer
intermediate
Explain the role of @HiltAndroidApp annotation.
The @HiltAndroidApp annotation is placed on the Application class. It triggers Hilt's code generation and creates a base class that manages the app-level dependency container.
Click to reveal answer
intermediate
How do you inject dependencies into an Android Activity using Hilt?
You annotate the Activity with @AndroidEntryPoint. Then, you can use @Inject on fields or constructor parameters to get dependencies automatically provided by Hilt.
Click to reveal answer
intermediate
What is a Hilt Module and how do you define one?
A Hilt Module is a class annotated with @Module and @InstallIn that tells Hilt how to provide certain dependencies. Inside, you define functions annotated with @Provides that return the objects to inject.
Click to reveal answer
Which annotation do you use to enable Hilt in your Application class?
A@AndroidEntryPoint
B@Inject
C@HiltAndroidApp
D@Module
✗ Incorrect
The @HiltAndroidApp annotation is required on the Application class to start Hilt's code generation and setup.
How do you tell Hilt to inject dependencies into an Activity?
ACall inject() manually in onCreate()
BAnnotate the Activity with @AndroidEntryPoint
CUse @Provides inside the Activity
DAnnotate the Activity with @HiltAndroidApp
✗ Incorrect
Annotating the Activity with @AndroidEntryPoint allows Hilt to generate the necessary code to inject dependencies.
What is the purpose of a Hilt Module?
ATo store UI layout files
BTo replace the Application class
CTo annotate Activities for injection
DTo provide instructions on how to create dependencies
✗ Incorrect
Modules tell Hilt how to create and provide dependencies using @Provides functions.
Which annotation is used on a function inside a Hilt Module to provide a dependency?
A@Provides
B@Inject
C@Singleton
D@EntryPoint
✗ Incorrect
@Provides marks a function that returns an object Hilt can inject.
What scope annotation would you use to make a dependency live as long as the Application?
A@Singleton
B@FragmentScoped
C@ActivityScoped
D@ViewModelScoped
✗ Incorrect
@Singleton scope means the dependency lives as long as the Application instance.
Describe the steps to set up Hilt in an Android app and inject a dependency into an Activity.
Think about annotations on Application, Activity, and Modules.
You got /5 concepts.
Explain how Hilt manages the lifecycle of dependencies and what scopes are available.
Consider how different parts of the app need different lifetimes for objects.
You got /5 concepts.
Practice
(1/5)
1. What is the primary purpose of using @Module and @Provides annotations in Hilt?
easy
A. To tell Hilt how to create and supply dependencies
B. To mark an Android Activity for injection
C. To automatically generate UI components
D. To define navigation between screens
Solution
Step 1: Understand the role of @Module
@Module is used to group methods that provide dependencies to Hilt.
Step 2: Understand the role of @Provides
@Provides annotates methods inside a module to tell Hilt how to create specific dependencies.
Final Answer:
To tell Hilt how to create and supply dependencies -> Option A
2. Which of the following is the correct way to inject a dependency into an Android Activity using Hilt?
easy
A. Annotate the Activity with @AndroidEntryPoint and use @Inject on the field
B. Use @Module on the Activity class
C. Call Hilt.inject(this) inside onCreate()
D. Add @Provides on the Activity constructor
Solution
Step 1: Identify how to enable injection in Activity
Hilt requires the Activity to be annotated with @AndroidEntryPoint to generate necessary code.
Step 2: Inject dependencies with @Inject
Fields or constructor parameters annotated with @Inject receive dependencies automatically.
Final Answer:
Annotate the Activity with @AndroidEntryPoint and use @Inject on the field -> Option A
Quick Check:
@AndroidEntryPoint + @Inject field = inject Activity dependency [OK]
Hint: Use @AndroidEntryPoint on Activity, then @Inject fields [OK]
Common Mistakes:
Trying to use @Module on Activity
Calling non-existent Hilt.inject() method
Adding @Provides on constructors
3. Given the following code snippet, what will be the output when MyViewModel is injected?
class MyRepository @Inject constructor() {
fun getData() = "Data from repository"
}
@HiltViewModel
class MyViewModel @Inject constructor(private val repo: MyRepository) : ViewModel() {
fun fetch() = repo.getData()
}
medium
A. "fetch() method not found"
B. NullPointerException at runtime
C. Compilation error due to missing @Provides
D. "Data from repository"
Solution
Step 1: Understand constructor injection in MyRepository
MyRepository has an @Inject constructor, so Hilt can create it automatically.
Step 2: MyViewModel receives MyRepository via constructor injection
Hilt injects MyRepository instance into MyViewModel, so calling fetch() returns repo.getData() string.
Final Answer:
"Data from repository" -> Option D
Quick Check:
Constructor injection returns data string [OK]
Hint: If constructor has @Inject, Hilt creates instance automatically [OK]
Common Mistakes:
Assuming @Provides is always needed
Expecting runtime null errors with constructor injection