Dependency Injection based Android interview questions and answers

WHAT IS DEPENDENCY INJECTION? DO YOU USE ANY DI LIBRARY IN YOUR PROJECT? CAN YOU NAME A FEW OF THEM, AND WHY ONE IS BETTER THAN
OTHERS?
Dependency Injection is a design pattern to implement inversion of control, and to
resolve dependencies. Dependency Injection (DI) eliminates boilerplate code (for example, by removing listener) and provides a much cleaner and effective code.

There are a few DI libraries used in Android development:
-Dagger
-ButterKnife
-RoboGuice

ButterKnife and Dagger do not use reflection, but rather compile time annotations. They are therefore faster to develop with.

What is dependency injection?

Is a pattern for resolving dependency. It is an implementation of dependency inversion principle (DIP). Some of the libraries to enable dependency injection for are,
  • AndroidAnnotations
  • Butter Knife
  • Dagger
  • Dagger2
  • RoboGuice
Dagger

Component Vs Subcomponent Component dependencies — Use this when you want to keep two components independent. Subcomponents — Use this when you want to keep two components coupled.

Can a subcomponent have multiple parents: No

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.



Aaaaaaaaaa

Comments

Popular posts from this blog

Jetpack Compose based Android interview and questions

Null safety based Kotlin interview questions and answers

CustomView based Android interview questions and answers