Dagger-Hilt Setup - MVVM Running Tracker App - Part 6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome back to new video so in this video you will start to set up everything we need for already panacea injection and initially I wanted to use dagger Android for dependency injection for this running app here but recently Google announced the new dagger hilt library which is very popular right now because it is much much easier so I tried it out and converted my running app from the old Tiger 2 dagger hilt and it just got a hundred and sixty lines of code less so initially this project had about a thousand eight hundred lines of code but after I switched from the old tiger library to dagger hilt it now has about a thousand six hundred a little bit more but that's about ten percent decrease in lines of code just by switching a library so I just decided to use this dagger hit library in this project which really solves a lot of problems much easier than the old dagger library well it's it's still an alpha but I didn't encounter any major issues with that so I will just go on with it and everything will work fine so let's first start to add the dagger health dependencies to our built at Gradle app file so let's open that and yes scroll down to your dependencies blog here and below the dagger android blog I will just paste the dagger heal dependencies so those are the dependencies for tiger hill and that is an additional dependency for activity KTX for view models as you can see that will just make it very easy later on to inject stuff into view models and as usual you can get those dependencies from this video's description that you can find a link to my github repository for this project and then you can just copy and paste those so you don't need to write them off of course then we will scroll up and we need to apply another plugin here which i will also paste that dagger hilt plugin and we also need to add another class path in our built at Gradle project file so let's open that as you can see we already have that class path for navigation components but we will also need to add this dagger hilt classpath here so that plug-in will actually be recognized that we added here in our builder Gradle ad file but if you've done that we can simply click on sync now and Gradle will do its job so when using dagger hilt the first thing we need to do in every every use it is to actually tell our app that it should use dagger healed as dependency injection tool and for that we need to create an application class let's go to a root package and create a new codling class here call it base application select class here and this will inherit from application and the only thing we need to do to mark this application as injectable with dagger hild is we need to add the annotation at hilt Android app and somehow it doesn't show suggestions here but you can see we can import that annotation let's press alt + Enter to do that and now it recognizes that annotation another set dagger is compile-time injected so that means when we launch our app it is already clear which dependencies will be injected into which classes so how can it actually happen that we just need to annotate this application class to mark our application as injectable and the answer for that is that the true power of dagger is in the generated code because we only see this annotation here about behind the scenes dagger will generate a whole bunch of code files so Java files in this case that handle the actual injection and if we rebuild our project then we will generate those code files so let's do that go to build and click on rebuild project and after the rebuild is finished we can go to our project hierarchy and open that Java generated folder you can see we have to come folders here I think that is the correct one yes you can see we have some classes that are generated by dagger hild for us for example that dagger base application hilt components hilt base application and we can open those classes you can see all that is code that the dagger hilt library generated for us so of course you shouldn't touch that code or change it but I think you should be aware of the fact that dagger actually generates those code files behind the scenes for us and that those code files are the actual reason why that dependency injection works here and just by annotating our base application with AD hilt Android app we just tell dagger that this is our application class and there needs to know that to actually generate those code files for us but of course that is not everything we need to do for injecting stuff still weather tag arild it's much less that we need to set up here to actually be able to inject dependencies into our classes but what we also need is called a module so let's say we want to be able to inject our running database into our repository that we don't have yet but later on we want to do that and if we want to inject that running database then dagger of course needs to know how to create that so it somehow needs a manual by us how to create a running database so it can inject that into our repository later on and because only we know how to create that running database instance we need to tell the dagger and give dagger kind of that manual so dagger also knows how to created and these manuals are put into so called module files or module classes and for that I want to create a new package in our root package called di for dependency injection and inside of this di package I will create a new Kotlin follow class and I will select object here because in dagger hilt we use objects so Singleton's for those modules most of the time and this module will be called AB module and what I call the manual here is actually nothing different than a function because in that function we will simply create that object that we want to be able to inject and then simply return it and those functions will be put into those module classes here first of all what we need to do with every module we have is we need to annotate it with add module but the single annotation for this module class is not enough instead we also need to add the add install in annotation and in the parenthesis here we need to put application component double colon class so what does that actually mean that install in an application component here in the old tagger we actually had to create those components by ourselves and that really was a lot of complicated code but in the new dagger hilt we don't need to do that anymore because those components are used to determine when the objects inside of our app module are created and when they are destroyed so what this means in particular here is we tell Android studio or our app that it should install this module inside of this application component class so that means because we use application component here if you take a look into into our base application class here it would mean that in the oncreate function of our base application class all of our dependencies that we declare in our app module so all of our manuals all of our objects that we want to be able to inject those will be created in the oncreate function of our application so that means they will exist during the whole lifetime of our app and when our app is destroyed again so when the user quiz that app those dependencies are destroyed too so you probably know the behavior of a singleton because that is exactly what this will do here we of course don't need to overwrite these functions by ourselves because dagger will handle that for us but we don't only have that application component here we also have for example the activity component that would mean that this app module will be installed into the activity component which means that the dependencies inside of this app module will only be available during the lifetime of a particular activity so that means when the activity is destroyed then also those dependencies inside of that app module will be destroyed so they won't live until the application dies instead they will only live as long as the activity lives and for each major Android component we have such a component class by dagger so we also have a service component for example we also have a fragment component and so on but for this module here I will use that application component again because in this module we will declare our database dependencies and we of course want this database instance to be a singleton we only want to have a single instance of that reference we don't need multiple instances of that to access our database a single instance is enough but let's actually define such a manual such a function in which we will create our running database so you actually see how that works let's define that as a function and I will call it provide running database so that function will provide a running database for a class that will use that one in database and that will be equal to room dot database builder and here you can see for that database builder we need the context but since we're inside of this module class here we don't have access to that context by default but what we can do here is we can go into the parameters of that function and create that context so we can use it in that room database builder here let's actually call that F which is a context and of course just by writing app which is a context dagger doesn't know from where it should get that context the solution to that problem is that we annotate this with application context so what this will do is it will just insert the application context for this app where will this app object here and in the old Egger this really was a lot more code than just annotating this context object here with application context so that is also something that is now much easier with that new dagger hilt library but since we now annotated this context with application context we can use that app in our room database builder so you can see when you to pass that context here and we can simply pass our app and dagger will actually insert that context here behind the scenes for us so we don't need to call that function by ourselves and as a second parameter here we need to provide the class of our database which is running database double colon class a Java and as a third parameter we need to provide a name for our database which I will create in a constants file which I will create right after I write running database name let's go to a root package create a new package here called other for all kinds of classes that don't belong into any other package but just for organization I created a new package for that and inside of this other package we will create a new cotton file class I will select object here since that's a singleton we only have constants in that file and I will call that class constants and in this object I will just have a console running database name and I will call this running underscore DB then we can go into our app module and here we need to import that constant now and after that we are good to go and to actually create the instance now we also need to call dot build afterwards but right now this is just a function that doesn't do anything because we also need to annotate that function and tell dagger that this function should actually provide something for us and the annotation for that is actually the same so we write ad provides to tell the Gir that the result of that function can be used to create other dependencies and can also be used to be injected into our classes but what is now very important is that if we leave this function like that then that means whenever we want to inject the database into somewhere into some classes of us then it will create a new instance of that database each time so that means if we have two places in our app in which we want to inject the database then each place will get its own instance but that is of course not what we want here because we want to have an application write singleton instance of that database and the solution to that problem is we need to attach a scope to this function and such a scope is for example the singleton scope so we annotate this with ADD singleton so that will just mean now that each class in our app that needs that running database will get the same instance and not multiple instances and generally we don't really need access to that database class instead we need access to its dao object because with a Dao object we can access all stata based functions that we need in our app and for that reason I will create another function here that will actually provide our dowel object and to actually show you how dagger will manage that for us because now it has kind of that manual how to create running database and it needs that manual to be able to create that Dao object for us because it can only create a Dao if it knows how to create a running database and for that I will also annotate the new function with add singleton and also add provides and I will actually make a little bit space here and that new function will be called provide run Dao and as a parameter to this function we now need to pass our running database so we pass DV which is of type running database and this will provide our Dao by writing DB get Rhonda so just the Dao object of our running database and now the cool thing about dagger is you can see we provided that running database here as a parameter and dagger will automatically recognize how it can construct that DB object here because we also gave dagger that manual here how to create a running database so we don't need to pass that by ourselves instead dagger will manage that for us and in general dagger will call all those functions by itself so we only define those functions here but we don't actually call them by ourselves that will instead be done in the classes that dagger generates for us and what we shouldn't forget now is to actually mark or a class or application class as the main application class of our app to do that we need to go into our manifest file and in the application tag we simply specify a name property here and pass our base application and now we actually did everything we need to do to be able to inject our running database or our run into some classes of us and to actually test that let's go into our main activity I just want to test if everything is working and inject our run down here of course this is not the place where you should do that just to show you that everything is working I will do it in this main activity but in the next video we will actually do that in the repository we create them so if you watch my previous video where I explained dagger in general then you already saw that add inject annotation I showed you there so what we want to do here is we want to correlate an adverse called run Dow which is of type run Dow and the only thing we need to do to get that one Dow is to add the add inject annotation so this annotation will tell dagger now that we want to get that Rundle object from one of our modules then it will look in our module in our app module and it will see okay we have a function here that provides such a doll object and I can create such a doll object with that DB reference but I don't have that DB reference yet so let's take a look how I can create that then it will see that provide running database function and it will see that this actually provides that DB reference that is needed to create a dau object so dagger has actually everything it needs to create our run Dow so behind the scenes it will actually inject that run Dow in to our activity class here and whenever you want to inject something in an android component class with dagger field so for example an activity a fragment the service or so on then just writing add inject is not enough to do that instead we also need to annotate this whole class with ADD android entry point and now that is really enough to be able to inject that run down into our main activity and just to test that I will add a simple off statement here I know we have that logging library timber actually but I didn't set that up yet I will do that in the next video so just write log D I will import log here give it a tag off run Dao and I will just print run Dao : and here I will use that run Dow dot hash code of course that won't tell us anything important here but if it prints something that just means that our around our is actually an object and it didn't crash so that means our injection was successful so let's actually run our app and hopefully everything is working take a look at our lock ad and search for run Dao and you can see here's our log that the hash code of our Rondo so that means our Rhonda was instantiated and successfully injected into our main activities so we you can see we have a late init var here and we didn't instantiate that by ourselves but dagger did it for us we can still access that instance so everything is working perfectly fine so I know that was a lot of new stuff and because of that I just want to go through everything we did here just as a quick little recap let's start in our base application class we annotated this class first with add hilt Android app to tell our app that we want to inject dependencies using dagger hilt then we created our app module which will actually have here in which we will create all of our manuals our functions that provide the dependencies for us so we annotated that module with add module just to tell daga that this is a module we will use here because it somehow needs to find our functions inside of our modules and we also annotated this module with ADD install in so we told agar that we want to install this module in our application component class which means that the dependencies the objects we create in this module live as long as the application does so there they will be created when our application is created and they will be destroyed when our application is destroyed and maybe it confused a little bit what the difference between that application component is and that singleton here because that singleton is a scope and that application component is not really a scope the difference is that that application component determines when those dependencies will be created and when those dependencies will be destroyed and that singleton means that only a single instance of those dependencies will be created at a single time so theoretically we could have several instances of our running database that all live inside of that application lifetime but each of those is its single instance but we don't want that of course so we attach that singleton scope which will mean we only have a single instance during the whole application lifetime so yeah then we have those provide functions here which we needed to annotate with add provides and those functions are the manuals I talked about those just determine how our objects that we want to inject are actually created and when a provides function needs another object that was already created then we can simply pass that object as a parameter to that function and if we take a look at our main activity we need to annotate that with at and Android entry point because that is an Android component class and activity then we always need to do that and we want to inject the one Dao so we add the add inject annotation and dagger will automatically find the right dependency with the type run Dao and inject it here so I really hope this was somehow useful for you and understandable if so please let me know that below that would make me really happy and really don't mind if you have any questions about dagger hilt just ask them below I will take my time to go through them and if I can answer them this can really be confusing for beginners here but trust me this is so much easier than the old dagger library so just be happy that dagger hilt was made and that it was made right on time so I can actually use it in this tutorial here have a good day see you next video bye bye [Music]
Info
Channel: Philipp Lackner
Views: 24,579
Rating: undefined out of 5
Keywords: tutorial, android, development, learning, programming, programmer, kotlin, beginner, mvvm, running, architecture, clean, android studio, android studio 4.0, androiddevs, android devs, navigation components, dagger, dependency injection, 2020, google maps sdk, google maps, maps, location, location tracker, polylines, viewmodel, livedata, repository, coroutines, service, foreground service, room, database, tracking, fitness app, health app, sports app, app, application, dagger hilt, hilt, di, dagger-hilt
Id: ZjL-rpACPS0
Channel Id: undefined
Length: 21min 27sec (1287 seconds)
Published: Fri Jun 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.