Sometimes being a lazy developer is a good thing, especially when you want to reduce repetitive tasks and find efficient ways to accomplish more with less.
Many times I have to re-write code to solve the same problem again and again, which is painful. As developers, all of us have copied code between projects. But this is not the best way to reuse code. It’s better to convert that code into a library so that it can be reused easily to save time.
Here, I will try to guide you on how to package your code into a library and share it with the world in just 3 easy steps.
In our first step, we will create a library module in our Android Studio that we want to publish. After that we will create a space on Bintray and make a repository there so that we can upload our library module in our second step. In the final step we will upload our library module on Bintray. Once module gets successfully uploaded we will push it to JCenter so that it becomes available to everyone.
So let’s start-
1. Make library module in Android Studio
Let’s build a library with some time manipulation utils that we might need to use in various projects. For the sake of this example, the library will contain a single method that will extract time as a String.
So let’s create a new project and name it as TimeUtilsExample in Android Studio. This will keep both Android Library & app that will use that library project.
Your new project is ready now, let’s create a new module inside this project.
Make sure to select Android Library from the dialog.
Let’s add some utility method in the module. The basic idea of this library module is to show the time picker dialog and select the time and get back into the app.
Bingo! You have created your first Library Project.
2. Create Space on Bintray
Before uploading the library on JCenter you need to upload it on Bintray first and then you can push it to JCenter.
I am assuming that you already have an account on Bintray and it’s simple too. Once you login into the account there is an option “Add New Repository”. Click on it.
Fill the details as shown in the screenshot and click on “Create”. Make sure to select “Maven” in type section.
You will get a success message and will be redirected to the screenshot below. Click on “Add New Package” to create a package in the repository.
Fill in the required information as shown below with your exact module name and your GitHub repo links and click on “Create Package”.
Congrats! You have successfully completed the second step.
3. Upload your library on Bintray and JCenter
Now you need to configure your library so that you can upload it to Bintray followed by JCenter.
Modify the project build.gradle file by adding these below dependencies:
Add your Bintray authentication details in the local.properties file. That information should not be pushed to the version control system.
bintray.user= “YOUR USERNAME”
bintray.apikey= “YOUR API KEY”
You can find both credentials in your account by following these steps-
Open your Bintray account, click on your name and then click edit under your name and then the API Key tab.
Now we need to modify our module build.gradle file. Open the file and modify according to the below-mentioned steps.
Sync the project. Once everything is configured, run the command below in your terminal, in root of the project.
If there is nothing wrong, it will show something like this:
Shout Hurray! if it shows BUILD SUCCESSFUL.
Check your package in the Bintray web interface. You will see a change in Versions area. Now it needs to be pushed to JCenter. Now once your project is up on Bintray, simply hit “Add to JCenter” button to sync with JCenter.
It may take a few hours before your request has been accepted. When it happens, you should get a notification via email and also see the jCenter badge in your package details under the “Linked to” section.
Congratulations! Your library is now on jCenter!!! You can give the gradle path into your project now. For example my dependency looks like this:
Tip: Update your repository with an example that indicates the use of your library so that users get to know about your library and how they can use it in their projects. Also, make sure that your library actually works before you share it with the world.
I hope this blog was useful. If you have any questions, please drop in your comments. Thank you for reading.