How to create and publish an Android Library using Android Studio
Today i’d be teaching you how to create a Library using Android Studio and how to publish it. After creating it you can import/implement it in your Android projects, and other people too can use it.
So what exactly is a library…
In programming, a library is a collection of precompiled routines that a program can use. The routines, sometimes called modules, are stored in object format. Libraries are particularly useful for storing frequently used routines because you do not need to explicitly link them to every program that uses them.
In very simple terms a library is a set of classes and methods that can be re-used in different projects, they help reduce boilerplate codes. You’ve probably used one or two libraries created by others, well today i’d be teaching you how you can create your own awesome library that other people around the world can use too.
In this article you’d learn:
- How to create an Android library inside Android studio
- How to publish it
- And how to import it into your project and see it work!, sounds great, yea?, lets get to it
Tools you’d need
- Android studio
- Internet connection
- GitHub Account
So lets get started,
First create a new Android studio project, and give it a name, I’ll call mine LibraryDemo, click next and select your API level, click on next, select Empty activity and click on finish, and allow the project to build
After the project is done building, from the Menubar, select File → New → New Module → Android Library → Next → Give the library a name, i’ll call mine “DoModulus” because this is gonna be a really simple basic library for tutorial purpose, it will only find the modulus of two integer values, you can change the module name or leave as it is, then click on Finish and allow to build/sync
After build/sync, your project structure should look like this, You should now have your module name appear thus in your project structure.
Now its time to define what our library would do exactly, so we’d create classes and define functions to do something, well for this demo we’d be building a very simple library, so we’d create just one class and one method/function.
Head over to your newly created module (domodulus in this case) and expand it, there you’d see a java folder, expand the java folder and right click on the package name (com.example.domodulus in this case) and select New → Java Class, then give your class a name, i’ll call mine DoModulus
Inside your DoModulus class, put the following codes
Now we can import our library and test it in our app, go to File → Project structure , under the Modules select app, then click on dependencies, you’d see a green + icon at the right, click that and select Module dependency, you’d see your module name, select it and click OK, click OK again and allow project to build/sync
Now our library have been added to the gradle file, if you open the gradle(Module:app) you’d see our library as below
Now to test if the library is really working, in the MainActivity.java file, inside the oncreate method
So if we create an integer variable for example and call the calculateModulus method we just created and give it some parameters, it will perform a modulus operation on the two parameters, we can then display the result in a TextView or via a Toast message
Publishing your Library
We’d go ahead and publish the library so we can import it into not only this particular project but other projects as well and every other person can use it.
We’d first need to create a Github repository, we can do this from Android studio directly. While still on the project you created earlier, From your Android Studio Menu bar click on VCS → Import into version control → Share project on GitHub, You need to have linked/connected your Android studio to Github for this to work, to link/connect your Android Studio to Github go to File → Settings → Version control → Github , click on Auth type and select Password, then enter your username and password and click on Test, if you have entered everything correctly, you should get a success message. ( You can also login to your github account on your browser and create a repo, then push your files).
Now that your project is on github, go to your repository page in your browser and click on releases
Then click on Create new release and input the version number , since this is our first release for this particular library we’d use 1.0
Then scroll down and click on Publish.
Copy the link from the releases page, copy only the part highlighted in blue as below
Head over to jitpack.io and paste the link from the releases page and click on the Look up button
Then click on the get it button and scroll down, you should see your library as below
Congratulations you have successfully created and published your Library :) , Now go ahead and create another Android Studio project and test your new library
After creating your new project, copy this
and paste it in your root build.gradle
Open the build.gradle(Module:app) and paste this…
…under the dependencies, then sync.
And that’s it! Your Library is now ready to be used on any Android project, Now go ahead and create great solutions with your Library
You can find the codes used in this tutorial and the project itself here .
Cheers.