Android Studio – Using a library Project

In the last post, we created our own Android library. The purpose of this was to create reusable code that could be used in any of our projects. If you haven’t yet read that, click here to read it first.

In this post we will now take that library and use it.

A quick note: The purpose of this app is to show you how to use the library  you created and, as such we don’t take care of ensuring it’s ‘safe’, i.e. we won’t worry about ensuring the text boxes only contain numbers etc…

1) As always, launch Android Studio, then create a new project. Choose ‘Empty Activity

2) Create three EditText views and three buttons. The first two of these views will be used to enter numbers and the third to display a result. The three buttons will be calling the maths functions that we created in our library to add, subtract and multiply. (Yes, that’s right.. there’s no divide function) 🙂

3) The library we created in the previous post (which you should have sensibly renamed to something like mathxlib.arr) should be added to the application\apps\libs folder.

4) In the appication’s build.gradle file, add the following:

            dirs 'libs'

5) In the dependencies section add your library (edit as appropriate):

compile(name:'mathxlib', ext:'aar')

6) Now, in your main activity, instantiate your library, i.e. in my case:

MathX mathX = new MathX();

7) Create code to handle the onClick event of your buttons. In each case call the appropriate method in your library.

int x = Integer.parseInt(editTextInputOne.getText().toString());
int y = Integer.parseInt(editTextInputTwo.getText().toString());

int result = mathX.addX(x, y);



8) Done. You’ve now create an application that uses your library.


Sample code for this blog post can be found on my GitHub:



Android Studio – Creating a library Project

Recently I have been developing some Android projects. Each application has had the requirement to retrieve data from an on-line web server and, since I have been using the Volley library, I’ve had to copy my wrapper code into each project. Additionally I’ve had to copy the same GSON code to parse this data. This is of course time consuming and not a good approach to programming.

Another more sensible approach is to create a library that contains the required methods.

So, in this post let’s look at how to do that:

1) Launch Android Studio and create a new project, give it a sensible name, such as ‘MathsLibrary’ and choose ‘No Activity’ when prompted.

2) We now have a standard Android application project. This is no good as that’s what we’d use to generate an Android Application. We however wish to generate an Android library or, in fact an Android Archive (AAR).

3) To change this to a library:

  • Open the app’s build.gradle file and change the line


  • apply plugin: ''


  • apply plugin: ''

4) Delete the applicationId line from the DefaultConfig section of build.gradle

5) Click ‘Sync Now

You’ve now created an empty library 🙂

6) Navigate to your src directory and then add a ‘Math’ class.

7) Add functions to add, subtract and multiply.

8) Click build and your library should build without any errors.

9) Now you’ve built a ‘debug’ library but it’s a ‘release’ library you should be building

10) Click on Build and then change the Build Variant to release.

11) Rebuild the project.

12) Now choose Build APK.

13) Since we aren’t building an app, Android Studio won’t generate an APK, it will instead generate a file entitled app-release.aar which can be found in the ‘app\build\outputs\aar‘ directory.

If you want to know how you can use this library in a project, click to read the next post.

Sample code for this blog post can be found on my GitHub: