Dependency Injection in Android
Dependency Injection(DI) is a technology widely used in Android development programming. By following the principles of DI, you can build a good architectural application.
Implementing dependency injections can give you the following benefits:
- Improve Reusability of Code
- Ease of refactoring
- Ease of testing
Fundamentals of dependency injection
Before covering dependency injection in Android specifically, this article provides you a more general overview of how dependency injection works.
What is Dependency Injection?
The figure above takes into account the Dependency Injection(DI).
Classes often require references to other classes. For example, a Car class might need a reference to an Engine class. These required classes are known as dependencies, the Bike class is dependent on having an instance of the Fuel class to run.
There are three ways to get the object that the class needs:
- The class constructs the dependency it needs. In the above example, Car would create and initialize its own instance of Engine.
- Grab it from somewhere else. Some Android APIs, such as Context getters and getSystemService(), work this way.
- Has it supplied as a parameter? These dependencies are provided when the class is created or they are passed through the application to the tasks that require each dependency. In the example above, the Car constructor would receive Engine as a parameter.
The third option is dependency injection! With this approach, you take the dependencies of a class and provide them rather than having the class instance obtain them itself.
You can do dependency injection 3 ways in Android:
- Injection using Constructor: This is the way described above. You pass the dependencies to the constructor of a class.
- Injection using Fields: This is also known as Setter Injection. Activities and fragments are instantiated by the system, so constructor injection is not possible, so you have to use field injection here. With field injection, dependencies are instantiated after the class is created.
- Injection using Method: to use method injection you have to use @Inject annotation with the method.
The figure above takes into account the flow of the Login feature with API calling using Retrofit in your application.
NOTE:-If a class contains all types of Injection i.e constructor, field, and method injection, the dagger will inject all types in the following sequence :
There are three major advantages of using Dependency Injection(DI) :
- Reusability: It improves Code reusability because of the inversion of control over the class, and classes no longer control how their dependencies are created, but instead work with any configuration.
- Ease of refactoring: The dependencies can be checked at the object-creation time and also at compile time of the app rather than being hidden as implementation details.
- Ease of testing: A class doesn’t manage its dependencies, so you can pass in different implementations to test all of your different cases when you’re testing it.
Popular Android Dependency Injection Frameworks
To make development easier, we have some Frameworks available for DI implementation.
you can implement view injection into your Android component using this library. It is a lightweight library, you have to use annotation processing to use this library. Normally you can tie a view using FindViewById (), but using Butterknife you don’t have to use FindViewById () because Butterknife does this automatically.
Dagger is a popular dependency injection library for Android Development using Java or Kotlin, which is created by Square but now maintained by Google. Dagger provides the facility of creating and maintaining the graph of dependencies using DI for your app.