How to Build & Publish An Android Library

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to your video in this video i'm going to show you how you can build your very own a library in android studio and also how you can publish that so that that other people in the end can include that in their project just like this here so you can see i created that library and i included it in this project it will just give me a very simple composable here of course that can contain more complex code but just to demonstrate that here how that works i use this image preview preview composable which just in the end looks like this a very simple image card with a text and a little gradient and that's going to be what we will publish to a jetpack so that you and that others can also include that in their project by just adding such a dependency here in the gradle file okay so your situation now should be that you have some kind of composable or any any kind of code that you actually want to put in your library that doesn't need to be composable can be anything um of course i it doesn't make any sense if i show you that here how to make this image preview because that's not the focus of this video you can choose any composable if you just want to take a simple text to learn how to do it you can do that but of course you can choose anything if you want this image preview check the code in this description so all you basically now have is such a very simple composable in your root directory here of your android studio project and that's it you don't have any library you don't publish you didn't publish anything all you have is this composable the first step when building a library is to to do that locally so what we will do is we will create a so-called module if we take a look here in in our project hierarchy then we already have one module in our project and that is our so-called app module that is the main module which will if you have many different modules it is used to just combine all these to use in the final executable app however when we build a library we of course don't want to share our main activity or anything else that's now in our app module no we actually want to kind of separate that from our from our main app so what we'll do is we will right click on android library um that's my project name so we go to new and we create a new module and then this window will open up we have a bunch of templates here and now for building a library um the the two major ones are the two templates that you will use the most frequently will either be android library or java or kotlin library what's the difference well an android library can contain android dependencies so we want to use um we want to make a library here with a composable which is an android dependency so we can only use composables in an android project so we need to select android library if however you have a library that only needs kotlin code or only needs java code and no android dependencies like the context like activities fragments then you're also good to choose the java or cotton library the advantage here is that you can also use this in for example kmm projects kotlin multi-platform because then you can share this code between android and ios which isn't possible if you choose android library but usually you pick android library because then you also have things like resources um yeah but you need to think about what do you need for your purpose so we select that we need to give our module a name which i will choose image dash preview for then it will create a new package name the rest is fine and we can click finish and now gradle will create that module for us important to know is that each single module in our in our project has its own build.gradle file so you might remember that if we take a look in the android view here there is a build gradle module app file a build.gradle project file and a build.gradle um module image preview file now so we now have an additional gradle file that was generated for our new image preview module which you can see here so this new module is now independent of our app module so we have our own gradle file we can specify our own dependencies that we need only for our module which we actually want to do now so we want to open um first of all let's open the build.gradle project file which just contains configuration that applies to our whole project so to all modules that's why we only have one of this project file but what you want to make sure is that you use a compose version 1.0.3 that is what i use here and it will work with the dependencies i use depending on when you watch this video you can also increase that but yeah if you if you choose this version with these dependencies then it won't lead to any version issues you also want to make sure that you choose the kotlin version 1.5.30 so you need to replace that for the kotlin gradle plugin and then you're already good to go then we can jump into this android library dot image preview module because right now if we would like to take our image preview composable and put it in our image preview well let's actually do that let's open the image preview package here which is just empty we can take our image preview here from our app module cut it out paste it here in this image preview module click refactor and there we go now we have that in here we can replace the package name here pressing alt plus enter and enter again and you see we have a bunch of dependency issues here the reason is because as i said each module has its own set of individual dependencies so we need to include these compose dependencies for our new module our image preview library again in its own gradle file so if we take a look here that's the image preview gradle file then these are the dependencies android studio automatically included but as you can see there are no composed dependencies it it still uses the material design dependency app combat core but no compose ones so we can actually remove all of these we don't need these instead we want to include some compose dependencies so we open our build.gradle app file in which these are already included and we just want to have this compose ui and compose material dependency we copy that go to image preview and just paste it here also what we need here to build a compose project is um are actually some gradle options here if we take a look in build.gradle app then you will see there are some more options like build features compose set to true and this use ir for example is set to true so we basically want to copy this stuff so these three option blocks copy these go to image preview and simply paste this here curtain options is a duplicate now so let's remove that one but that is pretty much it already you will also see up here for the plugins block that it now says id com android library that will now indicate that this gradle gradle file will belong to a library if we take a look here in the app module it will say id com android application so it will specify this gradle file as the gradle file of the application module all in all if you build a library you should always make sure that it only really contains the configuration it needs that it only contains the dependencies it needs because if you include more dependencies in your libraries and you then include the library in your in your actual project your actual project will contain all dependencies the library needs of course because otherwise the library wouldn't work so really only use the dependencies the library really needs so right now as you can see we just separated our image preview composable here um which still doesn't know these dependencies so let's click sync now we we have this file with our image preview composable and that is now just separated from our actual app module here so if we collapse all that you can see we just have a separate image preview module which is our library and that contains the code our library should also include in other projects now if we go back to our main activity which is located in our app module and we would like to use our image preview composable you will notice uh there is no there are no suggestions we can include that image preview composable even though we we have that in a module and the reason why that doesn't work is because we didn't include our module so that will now be treated already as a library and we need to include that library using our built-in cradle app file because that belongs to our app module scrolling down and this time we don't enter such a dependency string here or url not really the url um yeah to include that because it's not published yet instead it's only local here for our project so what we want to do is want to say implementation project and then here in this project block we can specify the module name we want to now include in this app module and that is always prepended with a colon and then the actual module name which is image dash preview if we then click synchronize now and go back to main activity now you will notice that there is actually an image preview composable that we can now include because we told our builder gradle app file that we want to include our local image preview library so far so good but right now other other people wouldn't be able to include this because they don't have this library and these library files on their machine to actually publish your library now to a repository so remote repository from where other people can easily include that with such a dependency here yeah for that we actually need to push it to a repository there are multiple repositories which i'm sure you've already heard of for example maven central which is the most trustworthy one but also the most difficult one to to push something to because they really want to make sure that that you are the person who you say you are and then you need to to do some verification stuff maybe verify your domain that you actually own this um domain you you actually say you own so in the end your package name for me it would be pl coding.com and yeah that that's difficult and not what i want to do in this tutorial instead i want to show you how you can use a jitpack.io to publish your library that is the easiest way to publish it all you really need to do for that is to to push your project to a github repository which i hope you know how to do but i will show you this step by step here to publish that there is an amazing plugin here in gradle that will do that automatically for us so we want to actually scroll up actually not here in the image preview build.gradle file scroll up to the plugins and we want to add another one which will be maven dash publish and now we need to configure that plugin that we just entered we can do that down here below our dependencies block in our library gradle file and you want to paste this and it needs a final curly bracket you can just write this off or simply copy and paste it down below from my github repository and of course replace your group id and artifact id so group id will just be your will just become github dot your github username so just replace philip lucknow with your github username and the artifact id will be yeah how you want your project to be called if you did that you want to click synchronize now and yeah that's already it for this gradle file a final thing you need to do before actually pushing this stuff to github now is we want to switch to the project view and we want to create a new file here so in the root package new file and it will be called ejetpack.yml that will just be a configuration file for jitpack because i needed to include that this stuff here essentially to specify that jetpack should use the open jdk 11 to build this stuff because for some reason it always used a different sdk and it didn't use the the one specified in gradle so i needed to do that so the build doesn't fail on jitpack so just paste the same and jetpack will then automatically use that configuration file to adjust its build settings now so a little recap we just included this android module here which contains our image preview composable so just a file with that image preview composable and we didn't push it yet to github that is what we want to do next so we want to go to vcs version control system and click share project on github then you need to give your repository a name that name will also be contained in the actual dependency string afterwards in the implementation blah blah blah i will just leave it at android library um yeah it's okay just for those demonstrational purposes here we want to click on share make sure it's not private and then it will create that github repository of course you need to have your github account linked to android studio then we click add here make sure it also adds this jpeg rml file and it will basically just push your whole project to github now you can see successfully shared project on github so the next step is to actually open github in your browser so here i am in my github repositories you can see there is the new repository android library we want to click on that and right now um we wouldn't be able to actually include this image preview module in other apps what we need to do for jit for jetpack to actually build this is we need to create a release here on github you can see there is a releases block we're going to click create a new release so for each version of your library you want to create a new release and that will then decide about the version number that will yeah that will just be appended at the end of your of your library when you include that so here for example in build.gradle app you see there's always a version number at the end and for each release you do you just choose a different version number that you just append after this last colon so here in github we want to choose a tag and this tag is the version number i talked about so let's just say 1.0 for the initial version press enter we want to use the master branch for that we only have that we need to give this release a title um yeah i already have one here compose image preview for example and you can then describe this release i can just say uh this is for demonstrational purposes on youtube so far so good you can then click publish release to actually publish that there you go there's your release you can also download the source code of this release as a zip file we don't need to do that instead what we want to do is we want to go to jetpack io this is the website from jetpack and here you can actually search for a git repo url so if you search for your repository and it finds that you know that it's uh that you that you're able to include that or if it didn't work if the build file for jetpack you will also find your error logs here so you see we need to enter our git repository url here so in github we go to our actual repository here copy the url paste it here and click lookup and you can see it actually finds version 1.0 we can then say get it you can see the log is still still we're still waiting for the log because it's currently building but if we click and get it and we hope that the log actually succeeds you can see you get instructions how you can now include that library in another project so on the one hand we need to include this maven url jitpack in our builder gradle settings file and we need to include this dependency here in our build.gradle app file or any other module let's just hope nothing bad will happen in this log and we just go back to android studio here in our android library project and now i will remove this project implementation because we want to test if it actually works to to get the dependency from jetpack and not from our local project so let's just paste what we copied from a jetpack of course that will now look a little different fully different for you that was one step the other one was that we need to include the jetpack url as a maven repository so we go to let's actually switch back to android and then we go to settings.gradle and you can see here we can specify repositories right now this maven jitpack isn't in there so we want to include that maven url https jetpack io let's quickly check in google chrome if it um finally compiled i don't know still in the log maybe we need to click on that it's starting and yeah you can basically see your log here it's it's still working on that you can see it's still here's an unfinished task so i'll just wait here and when that is done you can see you hopefully get such a green file icon if something went wrong it will be red um you can also click on that to see that but if it's green everything went well we can then jump back to android studio here in our android library project and in the end just click synchronize now so it will now use that repository and it will then include the dependency from jitpack when that was synchronized we can go to main activity and just try out if we can access our image preview and there it is that now doesn't come from our local module so even if we would remove that then we could still include this image preview and you can also test this if you can include that dependency at home in your project then that will also work so that is it for this video i hope you now fully understand how you can build an android library in android studio and actually also deploy that to jetpack so other people can use it if this was helpful for you then you will for sure also find my email newsletter helpful which is fully free you will get regular kotlin android architectural advice right into your inbox so make sure to click the link down below and actually subscribe to that newsletter for free apart from that i wish you an excellent day and i hope i see you in the next video again bye
Info
Channel: Philipp Lackner
Views: 635
Rating: 4.9642859 out of 5
Keywords: android, tutorial, philip, philipp, filipp, filip, fillip, fillipp, phillipp, phillip, lackener, leckener, leckner, lackner, kotlin, mobile
Id: EzC-FXeZiIk
Channel Id: undefined
Length: 20min 8sec (1208 seconds)
Published: Tue Oct 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.