Dependency Injection using Dagger-Hilt in Android Studio using Kotlin | Android Knowledge

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to my channel in this video we will learn about dependency injection using dagger H the topic is kind of tricky but don't worry I'll try to make it as simple as I can also dependency injection is a very vast topic so one video is not enough for it hence in this video I'll only cover the basics of it later in some other video we will go in detail about it okay now let's understand what exactly dependency injection is so consider a code where we have multiple classes dependent on one class which makes the entire code tightly coupled tightly couple means classes are dependent on each other and ultimately it becomes very difficult to manage and test hence to overcome this issue dependency injection was introduced as the definition says that allows a separation of object creation from their usage by making code more modular and testable means it makes the entire project Loosely coupled and removes the dependency from the code also as the last line says by providing required dependency to classes from external sources rather than having to create their own here external sources means using dager next this is a general diagram where we have class A and Class B here Class A depends on Class B which makes the class B as dependent of Class A okay as I previously said there will be multiple classes depending on one class here for the general example I have only one class depending on another class but there could be multiple classes like Class B Class C Class D depending on Class A got it now next in dependency injection we have two types manual dependency injection di means dependency injection okay and automatic di let's have a detailed look at them so in manual di we create the object manually without using any external resource although it is not recommend to use manual di because we have a better and Advance option available that is automatic di which we will see in upcoming slides but first let's see what are the types of manual di but first let's see what are the types of manual di we have two types in it construct injection and field injection so in Constructor injection we pass the dependency of a class to its Constructor simple then in failed injection dependency are instantiated after the class is created we will cover both of them practically as well so wait for it but for now let's have a look at automatic di so in automatic dependency injection we have two types dagger and Hil basically in automatic we don't have to create each object for every class instead dagger or Hilt will make it for us and that's what automatic means right hence it is highly recommend to use now let's have a look at each one of them in detail so first what exactly is dagger I kept the definition very simple to understand so it says manual dependency injection can be problematic depending on the size of your project obviously see means having three to five classes depending on one class that is fine to handle right but the there will be project where we will have hundred of classes depending on one class now that is difficult to handle right and that's when dagger comes into the picture so basically dagger generates the same code what we would have manually written But Here Comes The Twist that it internally creates a graph of objects that it can refer to provide an instance of a class basically it generates a factory type class that I'll show you when we will create the example project okay now all of this is done using annotations let's have a look at them module is used on a class that construct object and provide dependencies provides is use on a method in the module class that will return the object inject is used on field Constructor or method to indicate the dependency are requested component is used on a component interface which act as a bridge between module and inject singl ton is used to indicate only a single instance of dependency object is created and that's it then next is Hil again it makes the manual dependency injection easy to use by reducing boiler plate code basically H provides container for every class and manages their life cycle automatically so we usually use dagger and Hil together but for now not to make the project very complicated I'll be using dagger only but later in upcoming video we will cover Hilt as well now let's have a look at our example project so this is what a project representation looks Alik we have one class C and two classes that is Class A and Class B so Class C depends on Class A and Class B which makes them dependent of Class C basically every time we want to access Class A and Class B we need to create an instance of Class A and B in class C that makes it tightly coupled right hence we will be using dger to remove the dependency I have divided the project in four case in first case we will write the code without any use of dependency injection then in second case we will write the code using manual field injection then in the third case we will write the code using manual Constructor injection and then in the fourth case we will write the code using dagger got it let's go to Android studio and create it so first let me quickly create the three classes that is Tas a Class B and Class C and done now in class A it's an example project so I'll be simply printing a line in locket nothing complicated right so here first I'll create a function named as start Class A and inside it log as tag and message as Class A started and that's it similarly I'll do it in class B as well function start Class B and and inside it log as tag and message as Class B started and that's it as I previously said Class A and Class B are dependent of Class C so go to class C and this is where we will create an instance of both of them first case without using dependence injection so inside Class C I'll call Class A and Class B and done then we will create a function as start Class C and inside it I'll create an instance of class a DOT start class method and Class B dot start Class B method then write a log for Class C started and that's it see Class A is independent Class B is independent but Class C depends on Class A and B right then lastly call Class C in main so go to main activity I will here call Class C then create an instance of it as Class C dot start Class C and that's it this is what the code looks like without dependence injection I know you will be wondering it looks so easy nothing complicated but think as a professional developer then you will realize this is all tightly coupled which makes your code very difficult to manage and test probably not in this case because there is only three classes but imagine a project with 100 or even thousands of classes that's where all these Advanced topics are used I'll show you the output see in loat we have tag as Class A started Class B started and Class C started that is very basic I'm not going to show output in every case because it literally is same what important is our code because that is different in every case now let's move to our second case using manual field injection Class A and Class B will be as it is only class C and Main will change so let me quickly comment all the previous code and done now see here as the field injection says dependency are instantiated after the class is created hence instead of making instances private here I'll be using lat in it so let me quickly write it and then lat in it modifier declares the property without initializing them immediately and we will initialize them but not here in main okay and that's what field is right then where means the value can change letter and then similarly from the previous code we will write start classy function as it is and then go to main activity here I'll call Class C instance and then as we have used is Lattin it in class A and Class B so we need to initialize them over here hence I'll write here as Class C dot Class A is equal to class a object similarly for class B as well and then once it is initialized then start Class C method and done this is manual field injection looks like it is not tightly coupled but still there is a lot of code to write hence it is not recommended to use next let's have a look at third case that is manual Constructor injection I'll come on the previous code and done now in Constructor we usually have parameters assigned so class C is a Constructor and inside it I'll write private well Class A as Class A and then private well Class B as Class B both of them are our Constructor parameters then we will write start Class C function as it is and then now come to main activity here I'll call both the classes instance Class A and Class B then I'll call Class C instance as well but as Class C is a Constructor so obviously it will require its parameters as well so I'll mention here as Class A and Class B as its parameter then simply call start Class C method on Class C instance and that's it this is what our manual Constructor injection looks Al easy right but there is something more easy than that which is dagger so now let's move to our fourth case that is using dagger this is very important so Focus as we are us using dagger so obviously we will require dependency so go to graded module here add both the dependency I'll mention them in the description box also as we are using kpt so we will require kept plugin as well so inside plugins mention the K plugin then next I'll change compile sdk2 34 and that's it now click on sync now and done then go to class A here we will add inject annotation with a Constructor remember I said inject can be used with field Constructor and Method so that's why we'll be using your Constructor so wherever we mention inject annotation it requests for its dependency like I have mentioned inject for class A so it will provide us with class a dependency likewise we will do it for class B as well here I'll add inject annotation with a Constructor and done then go to class C here also I'll mention inject annotation with the Constructor as it's a class where all the other classes are dependent so we do have parameters inside it then write start Class C function as it is and that's it now listen to me carefully to use tagger we need to create a module object and a component interface that we will access in main so first let's create module object right click on it new class name it as class AB module and choose object I'll mention module annotation here let's have a look at its definition so module annotation is used on class that construct object and provide dependencies in our case it is Class A and Class B right then inside it we will create two functions that will return the object and to do that we need to mention provide annotation right let's have a look at its definition it says provides is used on a method in the module class that will return the object and that's what we are implementing right so I'll mention provides annotation and then create a function called as provide Class A by calling Class A that will return Class A and that's it similarly I'll do it for class B as well and done our module object class is ready next we need to create component interface we create component interface for the class on which every other class class is dependent on so in our case it is Class C right right click on it create a new class name it as classy component and choose interface here we will mention component annotation let's have a look at its definition it says component annotation is used on a component interface Spas which act as a bridge between module and inject we have already used inject in every class and also we have already created a module object for those injected class right so let's mention it here modules as class AB module and then next inside the interface create a function as get Class C instance that will call the class C and that's it now our component interface is also ready then go to main activity rebuild the project and then remember in start I mentioned about some Factory type Clans which dagger uses I'll show you that see here a new directory is created called as Java generated in this directory we have all the factory type of classes basically Factory classes are used to create instance of objects all of them are generated by dagger let's have a look at them you're not supposed to modify or edit it [Music] okay now here write dagger see the component interface will create it own dagger component the dagger prefix followed by a component interface name is a common convention for dagger generated components then dot create method and then dot get Class C instance which will obtain the instance of Class C and then dot start Class C method which will start its functionality which in our case including starting class a and Class B as well and that's it our code is ready now let's run the app the output will be same only see Class A B C started that's not even the concern Our concern is writing code using dagger I know dagger may seem complex at first but its Advantage become more apparent as your project grows and evolves the benefits of improved code organization maintainability and testability often out with the initial setup like you'll be wondering why is there a need of module object then component interface and all it's all extra good no they are not they are all your initial setup so that letter your code becomes easy to test and manage it's like working hard in your 20s to leave a stable life in your 30s also I'll be releasing this video in Hindi as well on my second channel so please do subscribe it and for more updates you can follow us on Instagram or join a telegram group Link in the description box so yeah that is it for the video if you're new to this channel then please consider subscribing to my channel and I'll see you in the next [Music] video
Info
Channel: Android Knowledge
Views: 1,361
Rating: undefined out of 5
Keywords: dependency injection android, dependency injection, dependency injection java, dependency injection simple, dependency injection simple explanation, dependency injection simple example, dependency, understanding dagger 2 android, dagger 2 android, dagger 2 simple example android, dagger 2 android tutorial, android, inject, understanding dagger 2, dagger 2 explained, dagger 2 easy example, dagger 2 simple tutorial, dagger 2 simple example, dagger 2 tutorial, hilt., constructor, example
Id: TQuV3B0uXV8
Channel Id: undefined
Length: 20min 43sec (1243 seconds)
Published: Wed Nov 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.