How to publish your own Android Library?

  • Posted by:

    Adhish L

    • April 28, 2017

    How to publish your own Android Library?

We often use various libraries to make our programming simpler, but making an Android Library is considered to be the next level of Android programming. With this, your pre-developed codes will be used by thousands of developers across the globe and not just the users of your app.

We make many helper Java classes which often make our programming simpler but now in this article you will learn how to convert those Java classes to library and publish it to JitPack so that anyone can sync your classes or, say, your library, into their project by just adding dependencies in their gradle file.

JitPack is a novel package repository for JVM and Android projects. It builds Git projects on demand and provides you with ready-to-use artifacts (jar, aar).

If you want your library to be available to the world, there is no need to go through project build and upload steps. All you need to do is push your project to GitHub and JitPack will take care of the rest.

So, here is a stepwise process to create your own library and publish it. We will be creating a simple debug logging library with the tag MyLibrary and a message of user’s choice.

Let’s get started:


1) Create an Android project or open an existing one in Android Studio

2) Create and add a new module and choose Android Library.

     Go to File>New>New Module>Android Library.

5.png

6.png

3) Implement your library code inside the library module you created in the last step.

4) Next, add the library module as a dependency to the app module.

  1. Goto File > Project Structure..

  2. Select app module in the sidebar

  3. Select the Dependencies tab

  4. At the bottom is a + icon, click that and select Module dependency and select your library module.

  5. Press ok.

As shown in the screenshots below:

10.png

11.png

12.png

13.png

5) Once the project is synced, add the android-maven-gradle-plugin to classpath in build.gradle file at root project level.

dependencies {

   classpath 'com.android.tools.build:gradle:2.1.2'

   ..

   ..

   // Setup the android-maven-gradle-plugin in the classpath

   classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'

   ..

6) Next, apply the android-maven-gradle-plugin to the build.gradle file at library module level. Also add the group variable.

apply plugin: 'com.android.library'

// Required plugin in library module

apply plugin: 'com.github.dcendents.android-maven'

// Replace adhishlal with <your_github_username>

group='com.github.adhishlal

7)  Once this is done, it’s now time to share your project on Git and make your library available for public use. Init the project with git using the following command in your Android Studio’s terminal

git init

8.png

8) Add your files using “git add .” command through the Android Studio’s terminal and then commit your code using the following command

git commit -m “”

Example: git commit -m “Initial commit”

9) Create the release TAG in git using the following command

git tag -a 1.0 -m “v1.0”

10) Finally, it’s time to publish your library!

Go to VCS>Import into version control>Share project on Github

Or if you already have a repository then just push it using the command-

git push origin 1.0

Hurray!

It’s done!

Just go to JitPack official site and check your library by just providing the repository URL or just follow this URL- https://www.jitpack.io/#username/reponame, where replace username with your github username and reponame with the reponame of the android project and start using it! Example: https://www.jitpack.io/#adhishlal/helloworldlibrary

19.png

20.png

Here is the code for the above article. If you want to use this simple debug logging class, sync the project by adding gradle dependencies as shown in screenshots above and then use as shown below:

HelloWorld helloWorld = new HelloWorld();

helloWorld.logMessage(“This is my first Android Library”);

Feel free to reach out to me and the other experts at Appiness, if you have any queries. :)

Read Next

Blockchain- Demystifying the Non-Financial applications

Posted by:

Pallavi

Read More Blogs

TOP

Next Blog Last Blog All Blogs