The Best Android Libraries Use Code Generation (VLOG)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you didn't already know some of the most popular android libraries that exist today some of the most well-known well-used well-tested you couldn't imagine the android ecosystem without these libraries those libraries all use code generation or at least a lot of them use code generation things like retrofit dagger hilt sql delight basically anything that uses annotations chances are that is looking at those annotations generating some code and making it easier for you as a developer so you don't have to write as much code by hand so i decided to start playing around with code generation kind of figure out what it's all about how to get started with it because i think that if i want any chance of developing any kind of library that a lot of people are going to use or is going to be very popular or useful in the android ecosystem probably i'm going to need to generate some code or it's at least at the very least not a bad skill set to add to my toolkit so currently to me it looks like the easiest way to start generating code on android is use this third party library created by square of course they create all the best libraries this is called column poet and essentially it makes it a little easier for you to start generating code so i created a playground and i started playing around with it a little bit this playground is called kotlin poet dash playground and by the way i don't suggest you like copy this code or use it in production or anything it's strictly a playground there's a lot of flaws this is what i would call very shitty code but that's what a playground is all about it's about figuring it out figuring out how it works and building something that uh does something specific so you can start to understand how you know how it works so let me show you what i actually built so i sort of applied a dagger kind of flavoring to this little playground basically what i wanted to do was i want to create like an annotation like a module annotation uh create like a provides annotation and have the code generator go through my code find those annotations and build the dependencies and add them to a single file that i can easily access so obviously this is not like exactly what dagger does this this would be like a very very small piece of what it does and you know a very shitty very very shitty version of it but just to go through the process of you know creating annotations how to look for annotations generate code uh after discovering what those annotations are like marking and actually get something that works so let me kind of take you through what i did and i encourage you to maybe it's this is not going to be a very technical video i'm just going to take you through kind of what i did and highlight some main points so that maybe you can go check out kotlin poet check out my playground and start playing around with code generation so i built this very simple app that just says you know hello mitch hello blake blake is my brother if some of you didn't know that i know it's just you know my brother always comes to mind is like the first name when i'm like okay what kind of a dummy name should i put here but anyway so um getting the dependencies from the documentation is the first thing that you want to do everything's in here i'm not going to like go through that obviously because this is just like a high level overview of what you know this is all about but essentially you need three things to get started with this you need to build an annotation you need to build a processor for those annotations you need to mark up your code that's with those annotations so the processor has something to look at and generate code for so those are three main things you need to build an annotation a processor to process the annotations and then use the annotations in your code so that it does something so the best way to do this that i think and all the examples that i've seen is what they do is they create modules for they create a module for where your annotations are going to live they create a module for where your processor is going to live then you have like your modules with your you know your source code for your project that's going to be the app module here by the way if you don't know how to build modules how to make multi-module projects on android i have a course currently this is my newest course on codingwithmitch.com it's called modulizing android apps i take you through kind of how to get started with modulization we build a real project i take you through like you know why it's important why you should do it um what like what does it really do for your code um i don't want to talk too much about it but if you want to know about modulization if you're an android developer you probably should know about modulization because if you have a job the code base is going to be huge and there's going to be modules so you might as well go and learn it if you haven't already learned it first module that you're going to want to build is an annotation module this is where you build the annotations so if you take a look here i have two annotations that i built a module annotation and it provides annotation like i said i'm going for a dagger-ish feel to this let me actually crank up my font size so you can see this a little better all right got a bigger font size so i created an annotation called provides it has a single argument i called argument name i'll talk more about that in a second and you know this is going to be an annotation that you use on functions i'm not going to talk too much more about it if you want to know more explore yourself now we have a module annotation the difference that you'll notice here is that this is going on a class this one is going on a function so two different annotations one for a class one for a function once you have those annotations you can start building your processor so inside of the processor module here i have a bunch of stuff i'm not going to talk about anything except for this my processor file right here so let's open this up and what this thing is is think of it as like it's like a worker class that is going to scan your code look through your code and you can tell it what to look for so again all this is in the column poet documentation or you can look at you know many different examples online also my code but essentially this has a process function inside the process function is where you write the things that you want this processor to do so the first thing i want to do is generate a factory class the reason i'm generating a factory class is because if you look at my uh maybe i'll talk about it later because it's not going to make sense anyway but we'll i'll talk about why i have the factory class later and then we have this other function called generate provides factories first let's actually look at the filter modules function this is the one that's going to filter through my code and look for those annotations so filter modules what this does is it looks for the module annotations again this code is very bad it doesn't do any kind of checks any kind of type checks it just it's very easy to this would break very easily but i just wanted to get it working essentially so just making sure that you don't like throw this into production or throw this into something that you're going to show somebody it would break very easily so you have to very finely walk in the lines of what you can do if you walk slightly outside of that the app is definitely going to crash so just so you know that so it looks for all the modules the module annotations and then it goes through there and finds the provides functions inside of them once it does that let me scroll back up to the top here it's going to pass those modules to this generate provides factories function and then it takes the the modules and it then looks for the provides annotations within those modules and then what it's going to do is it's going to generate factories that build the things that i declared so that sounded confusing this is all going to be very confusing so don't like feel bad about it i think the the language that kotlin poet uses to generate files is also very confusing like i still don't feel like i have a very good grasp of it i just barely got enough done here to get something working so just hang on if you're confused ignore what i just said let's go take a look at i like the modules so let's go into the app module and take a look at how i use these annotations so the goal here is i wanted to generate i probably didn't say this at the beginning i wanted to generate a module just like i want to use a module just like dagger and provide dependencies in that module just like dagger so what we have here is we have an app module i've annotated with my module annotation i've used my provides annotation i've wrote a function called provide person here i'm providing a person named mitch who weighs 200 pounds and giving him an argument name of mitch i have another provides function with an argument name of blake it's called provide another person this will also return a person but this one's named blake and he weighs a little bit less so like i said with my processor it's going to go through all of my code find the the module annotations then inside of the modules it will find the provides annotations and grab this code and add it to a single dependencies file so what i'm going to do is i'm going to actually build this so you can take a look and just see what's generated all right project has been built let's go into the app module here and go into the build directory go into generated go into source go into kotlin capped and then take a look at the files that were generated so the first thing here is the factory this is that factory i said that we generate with the processor so if i scroll up to the very top here this generate factory function it generates this interface right here the reason i generated this is because you can't it's not it's much easier to generate something and then reference that class than it is to actually put it in your source code and reference it in the processor i don't even actually know if no you can't even do that because in if you did that you'd have to pull in a module that's part of your project code into the processor module which doesn't really make sense you wouldn't want to do that so it's better like if you have these simple classes you might be thinking oh i could just put it in like my app module but don't do that because i need to bring that into the processor it's better just to have the processor generate it and then use it so that factory that's generated then it gets used to generate factories for the the provides annotations so like here we have app module underscore provide blake factory this is a factory so it's using that factory interface that i showed you right here it's providing a person which person is it providing it's providing another person so notice that it copied the name of this function provide another person so if you look here it says provide another person person it's referencing the app module and calling that function provide other person which is blake so that's how that gets that gets generated likewise the same thing is with provide mitch so it's like exactly the same other than it calls the app module and does provide person since mitch's function here is called provide person and then the last file that gets generated is this app module dependencies file where both of those uh both of those dependencies are added so it's just a single file where i would have every single dependency so in this case we only have two people that are generated mitch and blake so now there's functions in here to access those objects so then how would you use this well let's go into uh main activity so open up main activity and you can see here now that this is generated i can create that app module dependencies instance so it's an instance of this class right here and then i can access the objects so like here i'm going dependencies.mitch getting mitch's name dependencies.blake getting blake's name so obviously with dagger it's much more sophisticated you can inject things like you would do at inject late in it var you would probably do mitch person and inject the the person like that as opposed to just generating a single file with all the dependencies and then accessing them that way so dagger like i said is a much more sophisticated more well tested more it's just obviously way better this is like what i just did is for babies um but uh it gave me a little taste of code generation kind of an idea of at least how it would be done you know you create an annotation create or create annotations create a processor add the annotations to your code generate the code so again if you want to take a look at column poets and code generation i'll put some links down below that you can check out you can check out my repo my playground that's probably the best starting point because what i have here is very very simple i guarantee that you will struggle with the processor part just because the the objects in the processor that you use to generate the code i don't think that they're super intuitive the documentation was kind of hard to read personally i think so my my project is definitely a good place to start now the last thing i want to talk about in this video is the sponsor of this video as most of you know i started working at square because i started working at square they sent me a new computer they sent me a macbook they sent me an imac basically what that meant to me is i need to get a new desk because this is full i have a desk here with my kind of recording setup i got my windows pc back there there's no room on there so i needed a new desk and very conveniently flexispot actually reached out to me and said hey do you want us to send you a desk just do a short little demo on it and talk a little bit about it you know what's funny i actually reached out to flexispot like i don't know probably six months ago or something like that and i said hey do you want to send me a desk i'll do a video i got no reply back but randomly they sent me an email and said hey do you want a desk so it kind of worked out nicely for me and these desks are actually the ones that i was looking at because they're probably like the highest quality for the best price that i've seen out there like for example that desk back there that i use for my pc that desk i bought i don't know four or five years ago or something and that was when standing desks were just starting to become popular and it was very expensive like i think i spent a thousand bucks on it or something like that even this one that this debt that this computer is sitting on right now i bought on amazon for like 500 bucks and i thought that was a great deal but flexispot has way cheaper desks and they're just like they're there's no different than the one that i'm sitting at right now so let's go take a look at what it looks like and i'll do like a quick little um kind of like a couple shots of me building the desk what it looks like and uh see what my setup looks like on it so i got like the smallest one possible so this is the box size i know you can't really tell like how big that box is from this video here's all the parts that it comes with there's not a lot and i'm gonna go through the assembly of this thing so first of all you stand the posts up you put them on next as you attach these kind of side things on there then you put the table top together slap it on top and all of this took i don't know it maybe took me like an hour or something i took a little longer because i goofed on the setup those side pieces i put on backwards and upside down so i had to take them off twice and and rebuild it i should probably just read the instructions next time i'm really bad for that i never read the instructions so anyway once i got it all set up this is what it looks like here's kind of just a shot of it standing in my office and now here's what it looks like with my new imac sitting on it and i think it looks great it's kind of this is the smallest one they have like i said but it's perfect for what i'm going to use it for because this is going to be like a square only kind of workstation because i'm not allowed to work on my own stuff obviously on the square machine that doesn't make sense that they sent me a computer that i would work on you know coding with mitch stuff on so that's not allowed so this is a square only machine so it's just gonna sit right there and when i'm you know doing my day-to-day square work that is where i'm gonna sit now here i'm gonna show you a demo of me just kind of uh pushing it up to as high as it can go i'm about 511 i'm not quite six feet tall but you can see like me standing here i wouldn't want it any any higher i guess if you were like six two or six one uh maybe like six two six three actually you might want a higher one but you know i don't i don't really know you'd probably be um you're probably still looking up i don't know depending on whatever is the most comfortable for you i'm not really sure so now we get to the best part about this desk and that's the price they're actually doing a sale i think till the end of september um the desk that i got i think is this one right here and it's only 152 or 153 euros so in us i think that's about 180 u.s that is very cheap like like the desk that i'm on right now like i said i bought from amazon which is pretty much identical to the flexi spa desk it cost me 500 bucks so that's far far cheaper 500 compared to what's u.s i guess so that would probably be like 200 and i don't know 220 230 canadian compared to around 500 canadians so it's like half price so these are uh quite um competitively priced and again the quality is not different than this 500 desk that i bought at amazon so like i said they're doing a deal up until i think the end of september i'll put a link down below if you want to go check out that deal flexi spot desk thanks for sponsoring the video i i like the desk and it's gonna work out perfect for my square workstation that's gonna be it for this video go check out code generation kotlin poet and maybe check out flexispot's website if you want a nice desk that is great quality and has a good price i'll see you next time
Info
Channel: CodingWithMitch
Views: 12,202
Rating: undefined out of 5
Keywords: kotlin poet, android code generation, code generation android, kotlin poet android, codingwithmitch
Id: _EldLP774_M
Channel Id: undefined
Length: 16min 15sec (975 seconds)
Published: Thu Sep 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.