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:



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s