In this blog, we will implement the dagger 2 dependency injection and before we start we will take a look at annotation type which we are going to use in this blog.
Main dagger annotation Types :
- @Inject: Basically with this annotation we request dependencies. In other words, you use it to tell Dagger that the annotated class or field wants to participate in dependency injection. Thus, Dagger will construct instances of this annotated classes and satisfy their dependencies.
- @Singleton: Single instance of this provided object is created and shared.
- @Module: Modules are classes whose methods provide dependencies, so we define a class and annotate it with @Module, thus, Dagger will know where to find the dependencies in order to satisfy them when constructing class instances. One important feature of modules is that they have been designed to be partitioned and composed together (for instance we will see that in our apps we can have multiple composed modules).
- @Provide: Inside modules, we define methods containing this annotation which tells Dagger how we want to construct and provide those mentioned dependencies.
- @Component: Components basically are injectors, let’s say a bridge between @Injectand @Module, which its main responsibility is to put both together. They just give you instances of all the types you defined, for example, we must annotate an interface with @Component and list all the @Modules that will compose that component, and if any of them is missing, we get errors at compile time. All the components are aware of the scope of dependencies it provides through its modules.
Add dependency in your buid.gradle:
1 2 3 4 5 6 |
dependencies { implementation 'com.google.dagger:dagger-android:2.11' // dagger library implementation 'com.google.dagger:dagger-android-support:2.11' // if you use the support libraries annotationProcessor 'com.google.dagger:dagger-android-processor:2.11' annotationProcessor 'com.google.dagger:dagger-compiler:2.11' //for code generation } |
1-AppModule.class
AppModule class will be used to provide this reference. We will define a method annotated with @Provides @Singleton that informs Dagger that this method is the constructor for the String return type ( it is the method in charge of providing the instance of the String).
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Module class AppModule { private Context mContext; AppModule(Context mContext){ this.mContext=mContext;} @Singleton @Provides public String getPackageName(){ return mContext.packageName; } } |
2-AppComponent.class
In AppComponent class where we define the class where we will inject the modules it’s basically the bridge between Module and the class which injecting the module.
1 2 3 4 5 |
@Singleton @Component(modules = {AppModule.class}) interface AppComponent { public inject(MainActivity mainActivity); } |
Build your project, In your MainActivity class to inject the dependency add @Inject annotation on the String object.
1 2 3 4 5 6 7 8 9 10 |
public class MyActivity extends Activity { @Inject String mPackageName; public void onCreate(Bundle savedInstance) { // assign singleton instances to fields// DaggerAppComponent.builder().AppModule(this).inject(this); Log.d("PackageName","======>"+mPackageName); } } |
I hope it will help you to give the basic idea of how it will make your project component reusable and inject dependency of the object whenever it required by class.