{"id":15183,"date":"2021-09-13T11:33:10","date_gmt":"2021-09-13T11:33:10","guid":{"rendered":"https:\/\/flobiz.in\/blog\/?p=15183"},"modified":"2021-09-19T06:49:12","modified_gmt":"2021-09-19T06:49:12","slug":"create-your-own-android-library-publish-it","status":"publish","type":"post","link":"http:\/\/35.200.248.73\/blog\/create-your-own-android-library-publish-it\/","title":{"rendered":"Create your own Android Library &#038; Publish it"},"content":{"rendered":"\n<p id=\"0b1c\">I always wonder how android dependency works.<\/p>\n\n\n\n<p id=\"19f7\">An&nbsp;<strong>Android library<\/strong>&nbsp;is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest. However, instead of compiling into an APK that runs on a device, an Android library compiles into an&nbsp;<strong>Android Archive<\/strong>&nbsp;(AAR) file that you can use as a dependency for an Android app module.<\/p>\n\n\n\n<p id=\"ba5c\">In this tutorial, we are going to create an&nbsp;<strong>Android Library<\/strong>, and we also see how we can distribute it to&nbsp;<a href=\"https:\/\/jitpack.io\/\">JitPack<\/a>&nbsp;so we can use it as a dependency Gradle in other Android projects.<\/p>\n\n\n\n<p id=\"3965\">So let\u2019s start<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"cbf5\">Steps which we are following<\/h3>\n\n\n\n<p id=\"a5a4\">We are creating a simple&nbsp;<strong>toast<\/strong>&nbsp;<strong>library<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Step 1: Create a new Android app that showcases the library<\/li><li>Step 2: Create a new library module within the Android app<\/li><li>Step 3: Write the code for your custom library<\/li><li>Step 4: Add the library as a dependency to the project<\/li><li>Step 5: Publish your library to&nbsp;<a href=\"https:\/\/github.com\/\">Github<\/a><\/li><li>Step 6: Setup to share your library with any other android project using&nbsp;<code>Jitpack<\/code><\/li><\/ul>\n\n\n\n<p id=\"5a08\"><strong>Step 1: Create a new Android app that showcases the library<\/strong><\/p>\n\n\n\n<p id=\"f0da\">Open Android Studio and create a new project and name it as&nbsp;<strong>ToasterExample<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/2396\/1*UvIBPgsOE8_ZKlRvHEE4Cg.png\" alt=\"\" width=\"300\" height=\"288\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/4492\/1*DEK147fviyeZNhmECs1JSw.png\" alt=\"\" width=\"562\" height=\"449\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/4492\/1*HNnWgRnLsLEwtkkIZdDNQw.png\" alt=\"\" width=\"562\" height=\"448\"\/><figcaption>Create a new Android Project name as \u201cToasterExample\u201d<\/figcaption><\/figure>\n\n\n\n<p id=\"f071\">Click on Finish and your project will be ready.<\/p>\n\n\n\n<p id=\"37f1\"><strong>Step 2: Create a new library module within the Android app<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1400\/1*vRfvSdmh83bx3jnSly67nA.png\" alt=\"\" width=\"280\" height=\"454\"\/><\/figure>\n\n\n\n<p id=\"8788\">Select Android Library from the options and click on Next and Finish it<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/4500\/1*rbdPvno9UN-p_dWRg5u8VQ.png\" alt=\"\" width=\"563\" height=\"418\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1228\/1*fSuuKGaUO6oTusGRWhj7Mg.png\" alt=\"\" width=\"307\" height=\"426\"\/><figcaption>Your module toaster library has been added.<\/figcaption><\/figure>\n\n\n\n<p id=\"b71c\"><strong>Step 3: Write the code for your custom library<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Now we need to add code in our library. Create a new class in your module and name that class&nbsp;<strong>ToasterMessage<\/strong><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/7168\/1*3kUUZoRGrXgAKvFo7Hz7cg.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/2636\/1*LgMvvbqZxStmoqOH7j94Tg.png\" alt=\"\" width=\"659\" height=\"512\"\/><\/figure>\n\n\n\n<p id=\"707a\">Modify&nbsp;<strong>ToasterMessage.kt&nbsp;<\/strong>as follows<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class ToasterMessage {<br>    companion object {<br>        fun toastMessage(context: Context, message: String) {<br>            Toast.makeText(context, message, Toast.<em>LENGTH_SHORT<\/em>).show()<br>        }<br>    }<br>}<\/pre>\n\n\n\n<p id=\"8773\"><strong>Step 4: Add the library as a dependency to the project<\/strong><\/p>\n\n\n\n<p id=\"205b\">Open your app\u2019s build.gradle file and add the following inside&nbsp;<code>dependencies<\/code>&nbsp;after&nbsp;<code>\/\/ Testing<\/code>&nbsp;dependencies<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ added validatetor Android library module as a dependency<br>implementation project(':toasterlibrary')<\/pre>\n\n\n\n<p id=\"6088\">Now sync up your project Gradle files. That is it. You have just added the&nbsp;<strong>toasterlibrary<\/strong>&nbsp;Android library module to your app.<\/p>\n\n\n\n<p id=\"6b31\">This is one of the ways of adding an Android library to an app project. There are more ways which will be discussed later on in the tutorial.<\/p>\n\n\n\n<p id=\"34d6\">Now that you have the&nbsp;<strong>toasterlibrary&nbsp;<\/strong>library added as a dependency, you can reference the library code in your app.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em>Note:<\/em><\/strong><em>&nbsp;you will be referencing the Java-based Android library inside the Kotlin&nbsp;<\/em><strong><em>MainActivity<\/em><\/strong><em>&nbsp;class. There is no difference in usage except for following the Kotlin syntax<\/em><\/p><\/blockquote>\n\n\n\n<p id=\"9138\">So our Library is completed and now its time to publish the library. To publish the library all you need is a GitHub repository.<\/p>\n\n\n\n<p id=\"9624\"><strong>Step 5: Publish your library to&nbsp;<\/strong><a href=\"https:\/\/github.com\/\"><strong>Github<\/strong><\/a><\/p>\n\n\n\n<p id=\"0792\">There are several ways available to publish a project to Github<br>We will share our project to GitHub via Android Studio only<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/7168\/1*snJLSeIrxhu1eb3Z62i0FA.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1756\/1*1uNBLG1z1nRmRMpCXll8kw.png\" alt=\"\" width=\"439\" height=\"265\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/2672\/1*veVPWpWLtShUO6Qo3tik0w.png\" alt=\"\" width=\"668\" height=\"682\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/7168\/1*vnvjlFY0pSJrPidQsrLPiQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"85d5\">Now in GitHub repo click on\u00a0<strong>Release\u00a0<\/strong>and create a\u00a0<strong>New Release<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/712\/1*7GCetuwQljG5dlRGs2-gwQ.png\" alt=\"\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/3936\/1*OeUzafDBKMgBZHAniwCL2g.png\" alt=\"\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/2980\/1*JC0D74Rnl7PHEXDZvfh03Q.png\" alt=\"\"\/><figcaption>By clicking on `Create a new release`<\/figcaption><\/figure><\/div>\n\n\n\n<p id=\"32ea\">Publish the release.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ba16\"><strong>Step 5: Publishing your Android library<\/strong><\/h2>\n\n\n\n<p id=\"d570\">You have 3 options here<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/bintray.com\/\">JCenter<\/a><\/li><li><a href=\"https:\/\/search.maven.org\/\">MavenCentral<\/a><\/li><li><a href=\"https:\/\/www.jitpack.io\/\">Jitpack<\/a><\/li><\/ol>\n\n\n\n<p id=\"dad7\">For this tutorial, we are going to use&nbsp;<strong>Jitpack<\/strong>&nbsp;to publish our Android Library.<\/p>\n\n\n\n<p id=\"2291\">Insert your repository address and click on Lookup. Your releases will be listed. Now click on Get it.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/7168\/1*Arb55pagL6rjaam_mDHfEA.png\" alt=\"\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/7168\/1*El3uE1sq8oPo7yOwuV_AqQ.png\" alt=\"\"\/><\/figure><\/div>\n\n\n\n<p id=\"29fc\"><strong>Kudos, your Android Library is published and it is ready to use<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Note: Above example is for the public repo, if you have private rapo than visit this link&nbsp;<a href=\"https:\/\/jitpack.io\/private#auth\">https:\/\/jitpack.io\/private#auth<\/a><\/p><\/blockquote>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"d58f\">Using the Android Library<\/h1>\n\n\n\n<p id=\"c37f\">Now you can use this android library in any of your projects.<\/p>\n\n\n\n<p id=\"93eb\">In your project\u2019s build.gradle add the following line<br><strong><em>maven { url \u2018https:\/\/jitpack.io\u2019 }<\/em><\/strong><\/p>\n\n\n\n<p id=\"b943\">and in your app\u2019s build.gradle add the dependency<br><strong><em>compile \u2018com.github.aj019:Toaster-Library:0.1.0\u2019<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/7168\/1*RdFgpRi87KcuthAosjpWyQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/7168\/1*CJLMkEgX01Ci7tJcHV8TVQ.png\" alt=\"\"\/><\/figure><\/div>\n\n\n\n<p id=\"af92\"><strong>Thanks for reading this article<\/strong><\/p>\n\n\n\n<p id=\"bc1a\">Hopefully, after finishing this tutorial you have a solid understanding of building and publishing better Android libraries.<\/p>\n\n\n\n<p id=\"e390\">If you want to learn more about Android library development, check out&nbsp;<a href=\"https:\/\/developer.android.com\/studio\/projects\/android-library.html\">Google\u2019s official documentation<\/a>.<\/p>\n\n\n\n<p id=\"6628\">If you have any questions or comments, please join the forum discussion below!<\/p>\n\n\n\n<p id=\"68b1\">Source Code is available on GitHub at:<br><a href=\"https:\/\/github.com\/drjansari1992\/ToasterExample\">https:\/\/github.com\/drjansari1992\/ToasterExample<\/a><\/p>\n\n\n\n<p id=\"94ba\">Stay connected on\u00a0<a href=\"https:\/\/www.linkedin.com\/in\/dipen-jansari-55503122\/\">LinkedIn<\/a>\u00a0<a href=\"https:\/\/twitter.com\/Dipenjansari\">Twitter<\/a>\u00a0and\u00a0<a href=\"https:\/\/stackoverflow.com\/users\/3546909\/drjansari\">StackOverFlow<\/a>.<\/p>\n\n\n\n<p id=\"2c5f\"><strong>Be sure to clap\/recommend as much as you can and also share it with your friends.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Read this tutorial to get a solid understanding building and publishing better Android libraries.<\/p>\n","protected":false},"author":8,"featured_media":15283,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[149],"tags":[],"class_list":["post-15183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-engineering-product"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.1.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Create your own Android Library &amp; Publish it - Flobiz<\/title>\n<meta name=\"description\" content=\"Wondering how to create your own Android Library. Here&#039;s a step by step guide to creating your own android library and you can publish it.\" \/>\n<link rel=\"canonical\" href=\"http:\/\/73.248.200.35.bc.googleusercontent.com\/blog\/create-your-own-android-library-publish-it\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create your own Android Library &amp; Publish it - Flobiz\" \/>\n<meta property=\"og:description\" content=\"Wondering how to create your own Android Library. Here&#039;s a step by step guide to creating your own android library and you can publish it.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/73.248.200.35.bc.googleusercontent.com\/blog\/create-your-own-android-library-publish-it\/\" \/>\n<meta property=\"og:site_name\" content=\"FloBiz\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-13T11:33:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-19T06:49:12+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/35.200.248.73\/blog\/wp-content\/uploads\/2021\/09\/Untitled-design-4.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"7 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/35.200.248.73:80\/blog\/#website\",\"url\":\"http:\/\/35.200.248.73:80\/blog\/\",\"name\":\"FloBiz\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"http:\/\/35.200.248.73:80\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/73.248.200.35.bc.googleusercontent.com\/blog\/create-your-own-android-library-publish-it\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/35.200.248.73\/blog\/wp-content\/uploads\/2021\/09\/Untitled-design-4.png\",\"contentUrl\":\"http:\/\/35.200.248.73\/blog\/wp-content\/uploads\/2021\/09\/Untitled-design-4.png\",\"width\":1200,\"height\":675,\"caption\":\"Android Library\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/73.248.200.35.bc.googleusercontent.com\/blog\/create-your-own-android-library-publish-it\/#webpage\",\"url\":\"http:\/\/73.248.200.35.bc.googleusercontent.com\/blog\/create-your-own-android-library-publish-it\/\",\"name\":\"Create your own Android Library & Publish it - Flobiz\",\"isPartOf\":{\"@id\":\"http:\/\/35.200.248.73:80\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/73.248.200.35.bc.googleusercontent.com\/blog\/create-your-own-android-library-publish-it\/#primaryimage\"},\"datePublished\":\"2021-09-13T11:33:10+00:00\",\"dateModified\":\"2021-09-19T06:49:12+00:00\",\"author\":{\"@id\":\"http:\/\/35.200.248.73:80\/blog\/#\/schema\/person\/acf0255a56a8dacf88e9a27998430248\"},\"description\":\"Wondering how to create your own Android Library. Here's a step by step guide to creating your own android library and you can publish it.\",\"breadcrumb\":{\"@id\":\"http:\/\/73.248.200.35.bc.googleusercontent.com\/blog\/create-your-own-android-library-publish-it\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/73.248.200.35.bc.googleusercontent.com\/blog\/create-your-own-android-library-publish-it\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/73.248.200.35.bc.googleusercontent.com\/blog\/create-your-own-android-library-publish-it\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"http:\/\/35.200.248.73:80\/blog\/\",\"url\":\"http:\/\/35.200.248.73:80\/blog\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"http:\/\/73.248.200.35.bc.googleusercontent.com\/blog\/create-your-own-android-library-publish-it\/\",\"url\":\"http:\/\/73.248.200.35.bc.googleusercontent.com\/blog\/create-your-own-android-library-publish-it\/\",\"name\":\"Create your own Android Library &#038; Publish it\"}}]},{\"@type\":\"Person\",\"@id\":\"http:\/\/35.200.248.73:80\/blog\/#\/schema\/person\/acf0255a56a8dacf88e9a27998430248\",\"name\":\"Dipen Jansari\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/35.200.248.73:80\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d92393e3a8f1d1045848cb246f4a770d7e44bdcc74f38ac6e0e3bb67d4d10d13?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d92393e3a8f1d1045848cb246f4a770d7e44bdcc74f38ac6e0e3bb67d4d10d13?s=96&d=mm&r=g\",\"caption\":\"Dipen Jansari\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/posts\/15183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/comments?post=15183"}],"version-history":[{"count":5,"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/posts\/15183\/revisions"}],"predecessor-version":[{"id":15356,"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/posts\/15183\/revisions\/15356"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/media\/15283"}],"wp:attachment":[{"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/media?parent=15183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/categories?post=15183"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/tags?post=15183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}