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.
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.
Goto File > Project Structure..
Select app module in the sidebar
Select the Dependencies tab
At the bottom is a + icon, click that and select Module dependency and select your library module.
5) Once the project is synced, add the android-maven-gradle-plugin to classpath in build.gradle file at root project level.
// Setup the android-maven-gradle-plugin in the classpath
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>
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
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 “<your commit message>”
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
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
Here is the code for the above demonstrated 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 me if you have any doubts.