Android Hilt Tutorial - Injecting Dependencies with Hilt

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi everyone this is Belal Khan and  you are watching simplified coding. In the last video we learned how to use Dagger2  to perform dependency injection in our Android   project and in this video we will use Hilt  instead of Dagger to do dependency injection. So the first question is what is Hilt? So here you can see in the official documentation  Hilt is built on the top of Dagger dependency   injection library providing a standard way to  incorporate dagger into an Android application. So the goals of hilts are to simplify  Dagger-related infrastructure in our application. So in the last video you might felt that it  is very hard or complicated to implemented   Dagger in the android project and that is  why we have hilt to simplify everything   and that is why it is the last video or I  am covering Hilt at last of this course. So Hilt simplifies Dagger implementation and Hilt  works with all the standard Android components. For example, we can use Hilt  in application ViewModel   activity fragment view service  and broadcast receivers. So currently Hilt supports all these   Android classes and it is very easy to use  Hilt in the android application as it removes   all the boilerplate codes that  we had to write with Dagger2. So let's use Hilt in our current project . So this is my current project and i have removed  everything that I had written in the last video. So right now our goal here is to inject  this ViewModel inside my login fragment   and to do this the first thing that we need  to do is we need to add Hilt in our project. So let's do it . So the first thing that we need is we need   this classpath and we need to added inside  our project level build dot gradle file. So I will copy this line the version is 2.35   let's go-to project-level build dot  gradle file and we will paste this line . So we have 2.35 after this we  need to add these two lines. So I will add these lines inside dependencies  block and it is inside my app-level build dot   gradle file and one more thing that we need  to add is this dagger hilt android plugin. So I will add this and it is also  inside app level build dot gradle file. So I will add it here like this. Now sync the project and now we can add hilt  or we can use hilt for injecting dependencies. Now let's see how we can do it . So the first thing that we need to do is we  need to create a custom application class and   we already have a custom application class inside  our project so it is my application that extends   or inherits application and this class is defined  inside the manifest file as you can see here. So this is our custom application class. Now to use hilt we need to annotate  this class as hilt android app. So we will write here HiltAndroidApp like this   and that's it we need to do inside  the custom application class. In case of dagger 2 we had to implement  an interface we had to inject a dependency   and we also had to initialize dagger but in  case of Hilt all these things will be done   automatically and you just need to  annotate your class with Hilt android app. Now our goal here is to inject AuthViewModel  and to inject AuthViewModel hilt needs to know   how to construct AuthViewModel  and to do this we need to tell   that hey you need to construct this  dependency and that is why we will   do the same thing as we did in dagger  we will write inject and constructor. Now hilt knows that I have to provide  dependency for this AuthViewModel   but hilt do not know that how to construct  this AuthRepository and that is why we need   to repeat this process to all the classes  that are possible to constructor inject. So let's do it I will copy this line   and I will go to AuthRepository  and I will paste it here like this. Now hilt knows that I have  to build this AuthRepository   but in case of AuthApi it is not possible to  constructor inject because AuthApi is an interface   and for this class that is user preferences  we can inject it using constructor. So we will write (at the rate)@ Inject constructor   and for the context we have a predefined  qualifier in hilt that is application context. So we will write here application context  like this and now we have the context   and hilt can build the instance for this  UserPreferences but what about AuthApi   as it is an interface we cannot inject it with  the help of constructor and we need to build this   AuthApi or we need to tell hilt that how to  build this AuthApi and many times we are not   able to constructor inject instances for  example we cannot construct an inject interface   and we cannot also constructor inject  the classes that we do not own. For example, some third-party library classes  and for the situations like this we need to   create a module and we already learned how to  create a module in the last video we created   module for dagger and here module is the same  thing with just an additional annotation. So let's create a module first. So inside my di package I will create a module. So we will create an object  and I will name it AppModule   and this time also we need to annotate  this object as module you can see this   annotation is from dagger and it  is the standard dagger module only   but we are using hilt and that is why we need  to use one more annotation that is InstallIn. Now this will tell hilt that where  we want to install this module   and we have some standard components  for hilt that are predefined. So if you go to the official documentation you can  see here at the top we have singleton component   then we have activity retained component activity  component fragment component and all the other   components and we can use these components to  tell hilt that we want this module at this place. Now for this example I will use the singleton  component and singleton component means the   scope of this component is the scope of  our application that means we can use this   component anywhere inside our application so this  is the top most place of the component hierarchy. Now let's go back to android studio and here  I will define singleton component like this. Now here we need to tell hilt  that how to construct AuthApi. Now to construct AuthApi we need remoteDataSource  and we can constructor inject this class. So here I will write inject and then  constructor like this come back to app   module and here we will define a function and  again we need to annotate it with provides. So that hilt will know that  from here I will get AuthApi. So I will define a function and I will name it  ProvideAuthApi this function will give us AuthApi   like this and inside this function  we will construct AuthApi. Now to construct AuthApi we use this function   that is buildApi and this  function requires Api and Context. So here we need an instance of  our remoteDataSource and context .  So I can write here Context like this  that is ApplicationContext context   and it is Context and I can define  the remoteDataSource like this. Let's import context   and now we have the function that will construct  AuthApi and here we can simply write return   remoteDataSource dot buildApi and AuthApi  class dot java and then Context like this. Now this function will provide us the  AuthApi and now everything is done   we have everything that is needed  to construct this AuthViewModel. Now we can simply inject  AuthViewModel inside our LoginFragment   but we cannot do it like  this as we did previously. Previously we written Inject  lateinit var ViewModel AuthViewModel   with the help of dagger but this  will not work in case of hilt   the reason is in all the previous video I  considered the View Model as a normal class. So we cannot just generate ViewModel like this  we cannot write this to create ViewModel okay   but in all the previous videos I did like this  because I wanted to make things simpler but in   case of hilt there is a compile time check and  you cannot generate ViewModel like this because   to create a ViewModel we need to use ViewModel  provider Api if you know about ViewModel. So we use this Api to construct ViewModel   but as we are using heilt we have a function  that we can use to inject or construct ViewModel. So what we will do here is we will write private  val viewmodel of type AuthViewModel by ViewModels   like this and we need to import this function and  now we have our ViewModel inside LoginFragment. So this is how we can inject or construct  ViewModel when we are using hilt   but for any other class we can inject it. For example, we want to inject  remoteDataSource then we can write here   inject lateinit var remoteDataSource like this. Now hilt knows how to construct  remoteDataSource and you will get   this removeDataSource here but right now we  do not need this class here we just need the   AuthViewModel and that is successfully injected  inside our LoginFragment but one more thing   that you need to do is you need to annotate  your LoginFragment with AndroidEntryPoint. So wherever you are injecting dependencies  you need to annotate those classes with   AndroidEntryPoint and as this LoginFragment   is inside AuthActivity you also need to annotate  AuthActivity as AndroidEntryPoint and that's it. Now it should work. So let's try running the application  to see if it is working or not. So let's run the app. So as you can see the application is crashing  and it is a very simple mistake that I   have done accidentally. So remember this thing that whenever you  create a ViewModel and you are using hilt   you need to annotate your ViewModel  with (at the rate)@HiltViewModel. So this is very important because you  are using hilt and you need to use this   annotation to tell hilt that it is  a ViewModel that you want to inject. So now the application should work. So let's run it again. So now you can see it is working absolutely fine. So let's try login in. So you can see it is working absolutely fine. So that is all for this video friends  I hope you found this video helpful   and learned something if you want the source code  then you can get it from the link that is given   in the description of this video meanwhile  if you Like this video then PLEASE hit on   that Like button subscribe to my channel  if you're not already a subscriber and you   can share this video with all your friends  learning android application development. So thanks for watching everyone  this is Belal Khan now signing off.
Info
Channel: Simplified Coding
Views: 23,030
Rating: undefined out of 5
Keywords: android hilt, android hilt version, android hilt viewmodel, hilt dependency injection, hilt codelab, dagger hilt-android github, android hilt viewmodel factory, android hilt example, android hilt viewmodel scope, android hilt github
Id: AzH18a0_Tt8
Channel Id: undefined
Length: 13min 23sec (803 seconds)
Published: Fri Apr 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.