Getting Started with HILT (Dagger2)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to my new series on hilt which is a tool built on top of dagger for dependency injection on android in this video I am going to get started setting up tilt sorry hilt in Android studio we're gonna go to the website check out the documentation we're gonna go to the Android developer website check out the documentation and basically just get started we're gonna build a project and I'm going to take you through everything you need to get started using hilt and this is the first video in a series there's probably gonna be you know I would think 9 to 13 issue videos I'm gonna take you from all the way from like absolute sort of bare-bones how to get started using hilt all the way up to like the more advanced concepts which is like using in view models different types of injection doing tests running doing dependency injection and test how hilt makes testing easier on Android all those kind of things but yeah let's I'm not gonna talk too much more about it let's just get started and open up Android studio ok so I know I said we're gonna start an Android studio but let's actually start on the hilt website so if we go to dagger dev this is just standard dagger website there's a new section up here called hilt and this is where it tells you all about hilt sin like why they built hilt so from the first paragraph here it says Hill provides a standard way to incorporate dagger dependency injection into an android application the goals of hilt are to simplify dagger related infrastructure for android apps to create a standard set of components and scopes that you set up read it you set up a readability understanding and code sharing between apps to provide an easy way to provision different bindings to various build types testing debug or release so basically the aim here is they're they're making they're trying to make dagger easier for Android developers this is sort of like what they tried to do with dagger Android but in my opinion dagger Android I would call dagger Android a failure this was definitely not a failure this actually does make things easier for Android developers so we don't really need to spend any more time in the hilt documentation we can actually go to the Android documentation go to the kind of best practices section here go to dependency injection and go to dependency injection with hilt and here's the link up at the top here so what I'm going to be doing is just kind of going through this and we're going to be setting up hilt and a brand new Android project so first things first we of course need to get our dependencies so I can copy this and this is actually for the build.gradle project file so we need to add the hilt Android Gradle plugin so here I have a brand new project all I did was you know go to new project I created a empty activity went to next gave it a name and click finish that is literally all that I've done here nothing is different so once you've created a brand-new project let's get started here so it's going to build Gradle I'm going to paste that classpath in here and I can get that dagger hilts Gradle plugin now I'm gonna go back to the documentation and it's going to tell me what else I need so then apply the Gradle plug-in and add these dependencies to your app build Gradle file so we're gonna grab these two plugins gonna go to the App level build.gradle file so build up Gradle up here and paste these to a whoops sorry paste those two plugins at the top so caps for the annotations Kotlin annotation thing that you need to use annotations with Colin with capped and then dagger hilt android plug-in going back now let's grab these two dependencies so the first one is of course to use hilt and the second one is to use the annotation processor and that's why we have capped up the front here so let's sync that there's still some more stuff we need to add but I'll sync that and just see if it gives me any complaints I thought you actually had to use maven central here but maybe I'm wrong it didn't it didn't give me any warning so I'll just leave it the way it is next we need to hilt uses Java 8 so we need to enable Java 8 so this also goes into the build up Gradle app file so copying both of those going into the Builder it'll app file pasting those in and we need something else too so if I go over to my notes that I have here we also are going to use this Collin options thing and add this to our build Gradle file so paste that in so JVM target we're telling it what we're using Java 8 I can't remember if you can get away with not using it I had it in my notes so I'm just gonna make sure that I add it I don't want to forget something alright so next going back to the documentation again that was not the documentation this is the documentation so now it gets into how to get started with it so that should be all the dependencies that we need but again I'm going to double check my notes just to make sure it says here I wanted myself I wanted to remind myself to grab the retrofit dependency so we don't like we're not actually gonna be using retrofit until way later in this video series but I'm gonna be using it for kind of example purposes so make sure to copy the retrofit dependency if you don't know what retrofit is well I'm assuming most of you do know what retrofit is it's just it's an HTTP library for Android you can do HTTP request it's kind of the most popular one for doing HTTP request I'm going to be using it later to actually do HTTP request but in these first kind of videos it's gonna be just four examples and you'll you'll see more about that later so just copying that pasting that in and I'm actually going to copy this too and get the extract out the version from hilt and paste over this so this is exactly the same as what was just there the only difference here is I took the version out and I passed it as a variable here that's the only difference and then the last thing here is I have this correct error types I can't remember what this is for this is something to do with hilt I can't remember it's gonna come to me at some point when I'm explaining this later to you but I'm just gonna add it for now and let's hit sync and that is it for our dependencies okay so next to get started using hilt I'm just following along in documentation here it says all apps that use hilt must contain an application class that is annotated with hilt Android app so I'm gonna I'm gonna come on camera here and explain this to you really quick so we know that all of you guys who are used to using diary you know that you you needed a custom application class before you needed to if you're using dagger Android you had to extend it by a certain thing but basically what the application component was for was for holding a reference to your app component back when we use dagger or dagger Android we had to have this thing called an app component which was like a I guess like a mechanism for keeping a reference to something that holds dependencies that will live for the lifetime of the application so things that no matter what as long as the app is alive those dependencies would be alive so hilt has simplified this greatly so now all we need to do which you'll see in just a second here is we need to just add a single annotation to the application class and we're good so that that's kind of the first thing that I want you to know about hilt is it does like all the that used to build before the app component or if you had any sub components any components at all hilt automatically generates them and then you installed the dependencies into those generated components now that sounds confusing but actually you don't have to do anything all you need to do is declare the dependencies and say install it into this component and then it's good to go so those of you who are confused about what I just said which is probably most to you don't worry we're gonna be looking at examples and I'm gonna revisit this idea later just know that the the application class is greatly simplified which you'll see in just a second here okay so let's go back to Android studio we're going to create that application class so right-clicking on the main package directory and it's just going to call this like my application you can call it whatever you want it's gonna be a class yes sure let's add that to get and this is going to extend application so just like standard if you were building a custom application class on Android you would extend it by application notice I'm not extending it by anything have to ask anything to do with with Android now if I look at the documentation it tells me that I want to annotate this with at hilt Android app so that's what I'm gonna do so annotate this with at hilts whoops at hilt Android app and this is giving me a warning telling me that I need to declare this in the manifest so I'm gonna go do that going to the manifest type the name parameter and just reference my application now when I build this so if I just go to build and rebuild the project what what hilt is going to do is it's going to generate that app component and kind of prepare everything we need to use hilt in our application so I just wanna I want to point out like the specific savings here like what what did we actually tangibly save here compared to what dagger was like before well before we had to build the application class for sure we but we had to build also the app component we had to build modules for the for the component which we haven't built any modules yet but don't worry we're going to get to that we had to use either the Builder the factory pattern but we had to build that component then we saved a reference to the component inside of the application class then we would have to rebuild the project because that app that component would today your app component would not be generated yet so we need to generate then we needed to go back into our application class and initialize it because now we have it generated from dagger so it's like there's a lot of steps there right and and it gets more complex - as you get more components different scopes different modules there's a lot of things extra that you need to do there notice that we did one wheel added one line it come with their one annotation we have the app component it's coupled with the application we didn't have to instantiate we didn't do anything it's just ready to like use so there's a lot of savings there I just want to make sure I pointed that out okay so that's gonna be it for this video in the next video I'm going to I'm just looking at my notes here we're gonna go through some field injections so what is field injection and we're gonna just kind of build from the ground up like what is dependency injection how would we use it how do we declare dependencies how do we inject them all that kind of stuff from the ground up for beginners you
Info
Channel: CodingWithMitch
Views: 44,681
Rating: undefined out of 5
Keywords: dagger2, dagger-android, dependency injection android, android dependency injection, dagger android, hilt android, android hilt tutorial, android dagger hilt, android dependency injection with dagger2, dagger 2 kotlin, dagger 2 kotlin tutorial, dagger 2 kotlin mvvm, dagger 2 kotlin android, dagger hilt
Id: zTpM2olXCok
Channel Id: undefined
Length: 10min 9sec (609 seconds)
Published: Tue Jul 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.