Create an Android Library using Android Studio

Photo by Adrien on Unsplash

Create an Android Library using Android Studio

In this article we are going to explore how to use Android Studio to create and Android Library and how use it in another app.

Create an Android Studio Project

Let's start by creating a sample android app which is going to be used to test the library you will create while you develop and enhance it. If you are an android developer you're already familiar with the process of creating a new project through android studio but if you are new to android you can follow the following steps otherwise you can skip to the next section.

  1. Open Android Studio and click on 'New Project' button. image.png

  2. Make sure you have selected the 'Phone and Tablet' template from the left side panel then select the 'Empty Activity' or 'Empty Compose Activity' template. image.png

  3. Let's call our new project 'CommonUtils' and you can customize the other options with what you want. After you choose all the options you want click on the 'Finish' button. The new project wizard will be closed and your project will open as shown in the next screenshots. image.png

image.png

Creating The Android Library

Now that you have created a new project let's proceed with creating our android library by following the next steps.

  1. From android studio's menu select File -> New -> New Module. After that you will get the 'Create New Module' wizard. image.png image.png

  2. Make sure you have selected the 'Android Library' template from the left side panel Then enter 'common-utils' in the Module name field and complete the other options with your preferences or you can leave them with defaults as we will do in this case then click on 'Finish'. image.png image.png

  3. Now you have created the android library let's make a utils file called 'Utils.kt' with a dummy method just for testing. Under the library package Right Click your mouse then select 'New' -> 'Kotlin class/ file' then enter the name of the file and hit Enter. image.png image.png

  4. After you created the 'Utils.kt' file replace its content with the following content (which is a simple function that concatenate two strings and return the result for testing the library later):


package com.example.common_utils

fun concatenateTwoStrings(first: String, second: String): String {
    return first + second
}

image.png image.png

Congrats!, You have successfully created your first android library with a simple utils function that you can use in any other app and that's what we are going to do next.

How to use the created Android Library in other apps

In order to be able to use the created android library we need to make the app depend on it using gradle. To do that proceed with the following steps:

  1. Make sure you have selected the 'Project' view not the 'Android' view in the Project side pane in android studio. image.png image.png

  2. Open the 'build.gradle' file in the app module and in the dependencies section add the 'common-utils' library as a gradle dependency

    dependencies {
     implementation project(path: ':common-utils')
    }
    
  3. After you add the dependency you will be shown an option that the gradle files have changed with a suggestion to sync the gradle files. Click on 'Sync Now' and wait for the gardle sync to finish. image.png image.png

  4. Now that you have added the dependency of the library as a dependency we can use it in the app we created previously. Open the MainActivity class in the app module and add the following code (which use the concatenateTwoStrings function from common-utils library):

    Greeting( concatenateTwoStrings ("Hello ", "Android"))
    

    image.png

  5. Now click on run the app button (which is a green button in top toolbar) to run and deploy the app to the connected device

image.png image.png

Now you can see the device running the simple app which we previously created and you have successfully manged to use the created library in another apps!

Wrapping it up

In this article we have covered all the steps you need to create an android library and we learned how to use it in other android apps.

References