Dependency Injection based Android interview questions and answers
What is dependency injection?
- AndroidAnnotations
- Butter Knife
- Dagger
- Dagger2
- RoboGuice
What are the annotations that you used in Dagger2?
Dagger2 is the most popular framework to implement dependency injection in Android. Dagger2 is a compile-time library, this gives us two advantages — no runtime errors and we no longer need to maintain dependency injection manually.
Coming to the question, it depends on your hands-on experience with Dagger2. But there will be few annotations that you should know such as — Module, Provides, Inject, Component, and Singleton. Read the following article to learn about them. Apart from that, you should also learn about advanced annotations like-named, Qualifier, and more.
What is the difference between provides and binds?
The end goal of both annotations is to provide the necessary dependency object. Provides can have as many parameters as required but binds can only have one parameter and it should be equal to its return type
Provides generates factory class for each function whereas binds won’t do it. Imagine how many factory classes and their methods will be created and become problematic with DEX size. Typically by using binds we can reduce 40% of code generation.
What is named annotation?
Named is an advanced annotation in Dagger2. When you’ve more than one provides function with the same return type then dagger throws compile time exception. This is where named annotation comes to light. We can give a different name for each provider function so that the dagger can differentiate them while injecting. Have a look at the code for a clear idea:
@Module | |
class CatModule { | |
@Provides | |
@Named("Death") | |
fun provideDeathByChocoDonut(): Donut = Donut("DeathByChoco") | |
@Provides | |
@Named("Alive") | |
fun provideAliveByChocoDonut(): Donut = Donut("AliveByChoco") | |
} | |
//Usage | |
@Inject @field:Named("Death") lateinit var |
What is a Qualifier?
The qualifier is used to distinguish between objects of the same type but with different instances. The qualifier is behind the scenes guy which gives us the power to create annotations like named. To learn more about it read the following article.
What is a scope?
The scope is an annotation class to define the life span of a component. The inner components and dependencies created in it are bound to the life span of that component.
Singleton is the best example of scope in Dagger. If use singleton annotation on any of the components it’ll be created once and stay in the memory throughout the application life span.
Apart from this based on the company and their usage of dagger2 you might also face complicated questions related to Dagger2 in the multi-module.
Why do we use the Dependency Injection Framework like Dagger in Android? - Learn from here
How does the Dagger work? - Learn from here and here
What is Component in Dagger? - Learn from here
What is Module in Dagger? - Learn from here
How does the custom scope work in Dagger?
Comments
Post a Comment