‫Hello, my friends.
‫In the previous video, we have learned about manual dependency injection.
‫Now we are going to use a framework called Dagger in order to allow us to make a fast dependency injector
‫for Java and Android.
‫So Dagger is a compile time framework for dependency injection.
‫It uses no reflection or runtime byte codes.
‫Generation does all its analysis at compile time and generates plain Java code.
‫So it's very important at runtime.
‫So let's add that the dependency of the dagger in our project.
‫So we are going to transform our project into into dependency injection but with using the help of dagger
‫framework.
‫So 2.41 is the latest version.
‫I have took it from the green bulb that we see in the GitHub.
‫Okay.
‫Let's sink the project and let's see.
‫If it is at and it's successfully at?
‫No.
‫In the previous video, we have learned that the mobile depends on battery memory card and CPU, and
‫in order to create a memory sort of mobile, you need to create a battery memory card and CPU and past
‫them and inject them by a constructor injection.
‫Again, the mobile hierarchy will be the battery memory card and sip you and CPU depends on processor
‫type which which is a component of it in order to create a sip you.
‫So we have two dependencies that sip you depends on processor and the mobile depends on the processor,
‫battery and memory card.
‫Now all the constructors of all of these classes should be injected and annotated by at inject method.
‫So this is a sorry at inject annotation.
‫The mobile method incipio and the processor.
‫These are the dependencies of and memory card.
‫These are the dependencies of the mobile.
‫So I need to do an app, inject annotation to construct.
‫Okay.
‫So use inject annotation and data constructor that docker should use to create instances of class when
‫a new instance is requested.
‫Dagger will obtain the required parameters, values and invoke this constructor.
‫So let's see again.
‫Let's start with the first class, which is the mobile I put in your jacket and import the package of
‫facts that inject thought into the memory card here also.
‫Inject.
‫That's if you and the constructor I put in, Jack.
‫And the battery here and check.
‫I will show you all the possible errors that you may encounter inside when using the dagger framework
‫and how to solve it.
‫Okay.
‫So now we have added for the mobile CPU battery and processor and the memory card constructor's.
‫Here we are telling that girl that we need to inject this constructor, which is for the mobile with
‫other constructors with which is the battery memory card.
‫SIP you and the process.
‫Now.
‫Dagger junior Aztec codes for the dependency injection to use these codes we need at or to us.
‫Dagger generates calls for dependency injection.
‫So we need to create an instance or interface to connect all of these annotations.
‫So we need to create an instance annotated with components.
‫So the component interface world behaves as that grouping and the dealer with that dagger class has
‫generated the classes.
‫So think about you are making that using dagger and we need to connect with it telling that we need
‫that mobile memory card batteries CPU processor are the classes that to be injected how to tell it by
‫using that component component annotation.
‫We need to create a interface mobile component which is that tab what level of dependencies and we create
‫mobile don't get mobile.
‫So this is how we use the component.
‫Until now we have inject and take that constructors of the class where the mobile depends on with at
‫inject annotation.
‫And then we need to create mobile component which is which is an interface that is annotated with that
‫component.
‫Now if we go to resource to Java Generated and we click on Clean Project and to Rebuild Project, new
‫generated classes will be founded using that dagger framework.
‫Okay.
‫And there are some errors here.
‫I need a song that I missed to create an instance that allows and may make it mandatory for for the
‫actors to perform this building of the classes.
‫So I will create an instance of mobile component framework called Dagger Mobile Component.
‫And here you see guys that I started with that dagger and the name of the construct at that interface.
‫So here we start by dagger.
‫Mobile components that create, but we have an error.
‫These classes are not found, so we need to initialize it at first with no, then make a rebuild in
‫order to dagger to generate the classes here at Java.
‫And here are some errors.
‫We cannot inject it at memory, size and request.
‫So we need to remove all these methods so that the sorry, the parameters because inject is only for
‫empty constructors.
‫And later on we will see how to make the injection with with the and with the attributes, with the
‫found of attributes and the parameterized constructors.
‫Okay.
‫So we need to remove all of these.
‫Attributes.
‫And here.
‫Also from the.
‫Processor.
‫Battery processor.
‫I need to make it as an empty construct.
‫Okay.
‫Memory card.
‫I should remove the memory card.
‫This.
‫The memory card.
‫Underscore factory.
‫We'll hear about the factories later on.
‫So you see guys that here are the junior rated classes and you see battery underscore factory incipio
‫underscore factory memory card, mobile processor underscore factory.
‫So I would clean the project first and then rebuild it.
‫In order to see these classes generated classes.
‫Okay.
‫Now, also, there is an error here.
‫I need to.
‫I.
‫Oh, sorry.
‫I have missed this.
‫The processor.
‫And here are the generated.
‫As I told you, the processor underscore factory memory card battery and others.
‫Okay.
‫No.
‫Let's go to the processor.
‫I have the things I need to remove the past parameters.
‫Okay.
‫Now let's clean also the project and then rebuild it in order to see that there is no process or attributes.
‫And here is you guys java that generated this are that generated classes also during the run time.
‫Okay.
‫And these are what we are going to deal with.
‫The dagger.
‫Mobile component.
‫You see mobile dagger.
‫Mobile component.
‫This is the class that it is generated during the run time and implements the mobile component interface
‫that we have created.
‫And also these are the methods called build that we are going to use.
‫So thanks for dagger, for allowing us to make such like this, this thing.
‫So make a mobile component interface in order to contact with dagger.
‫And I am making an instance of this by specifying the mobile dagger component.
‫More dagger mobile component could create.
‫So we use the method here, which is called create that returns the mobile component and calls the builder
‫dot build method.
‫So these are the functionality of about my dagger interface.
‫So I'm using this interface in order to create an instance from my interface mobile component, which
‫is called dagger mobile component and.
‫Here we create an instance from mobile and connecting it by making mobile equal mobile component interface
‫to get mobile.
‫So this is how we connect that component with the mobile interface and telling it to inject it during
‫the interface that runtime.
‫Here I will create a new method called public void.
‫And what I will name it make mobile or me or ring ring ring now.
‫And this will tell us that it is ringing so that mobile is ringing.
‫It's just the calling of the method that allows us to to display ringing and logcat.
‫So mobile dot ring now.
‫Let's clean the project and then run it.
‫Okay.
‫And waiting for the emulator.
‫And here we go.
‫This is our application waiting to be launched.
‫And let's see.
‫In the logcat, it's launching without any error.
‫That's good.
‫Let's see.
‫And.
‫Ron.
‫And here we go.
‫It's playing for us.
‫Ringing message.
‫Okay.
‫So everything is working fine.
‫This is how we transform our application from manual dependency to you in dependency injection with.
‫With the use of that.