Easiest ways to add an external library in Android Studio

Congratulations! If you were searching for how to add an external library in the Android Studio, your search is now over. This blog will shed some light on various methods to add an external library into your Android Studio project with ease. We will first be looking at some informal definitions of commonly used terms in the process and then head on to the methods to do the same. So let’s get started without further ado.

What is the Android Studio?

Android Studio is the official Integrated Development Environment (IDE) for Android app development, based on IntelliJ IDEA. On top of IntelliJ’s powerful code editor and developer tools, Android Studio offers even more features that enhance your productivity when building Android apps.

Android Studio uses a Gradle-based build system, emulator, code templates, and Github integration to support application development within the Android operating system. Every project in Android Studio has one or more modalities with source code and resource files. These modalities include Android app modules, Library modules, and Google App Engine modules. Hence, libraries make up an integral part of an Android Studio project.

What is an external library file in Android Studio?

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest. But, instead of compiling into an APK that runs on a device, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module.

We can use two types of libraries in our Android Studio project:

  • Android Library Module: Android Library Module compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module. It allows you to add Android-specific components like resources and manifest files, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods.
  • Java Library: Java library builds a Java ARchive file (JAR file). A JAR file is useful for many projects especially when you want to share code with other platforms. But it does not allow you to include Android resources or manifest files, which is very useful for code reuse in Android projects. So when you do not need any android specific resources in a library you should create a java library.

Benefits of using external libraries in your Android Studio project

Using external libraries in an Android Studio project makes things easier. The following are some of the advantages of doing so:

  • When you’re building multiple apps that use some of the same components, such as activities, services, or UI layouts, you can simply copy-paste the same libraries in your multiple apps.
  • When you are using a service whose code has already been written, you can simply add its library as opposed to writing the entire code by yourself.
  • When you’re building an app that exists in multiple APK variations, such as a free and paid version and you need the same core components in both, you can simply add the same libraries for both.

Now that we are in terms with the commonly used definitions and have knowledge of the benefits of using an external library, let’s look at how to add an external library in our Android Studio project.

How To Add External Library In Android Studio

There are generally three ways of adding an external library in an Android Studio project. We will be starting with the most commonly used methods and go down the same. So let’s check these methods out.

Adding an external library by copying it to the libs folder

In this method, we will be adding an external library by copying and pasting the JAR file into the libs folder. It’s the easiest way to add a library and works almost every time without any issues.

How to:

  • The first step is to of course open the Android Studio project that you are working on. If you want to create a new project, you can do so by selecting an Empty Activity and naming your project.
  • Once you are in your project, change the project view from Android to Project Files.

    Change project view to Project Files

  • Copy the JAR file of the library that you want to add. For example, here, we will be adding the Commons Lang Java library. You can download its JAR file from here.

    Sample JAR file

  • Once you have copied the JAR file, navigate to the libs folder by going to app->libs in the Project Files view.

    Navigate to libs folder

  • Right-click on the libs folder and hit Paste.

    Paste the JAR file

  • Right-click on the pasted JAR file and select Add As Library… from the drop-down menu.

    Select Add As Library by right-clicking

  • You have now successfully added an external library to your Android Studio project.
  • You can change back the project view to Android and navigate to your build.gradle file to see that your external library is now included in it.

    The library dependency should now be added

  • It’s that simple.

Adding an external library by using the module library

Android Studio also allows users to create modules into their Android Studio project. These modules can then be used to add an external library to your project as a module library.

How to:

  • The first step is to of course open the Android Studio project that you are working on. If you want to create a new project, you can do so by selecting an Empty Activity and naming your project.
  • Next, click on File->New->New Module…

    Navigate to New Module

  • Select Import .JAR/.AAR Package and hit Next.

    Choose Import .JAR/.AAR Package

  • In the filename, choose the location of the JAR file that is to be added as a library.
  • Your library is now added as a module, although you need to declare it as a dependency now.
  • To add it as a dependency, go to File->Project Structure and navigate to Dependencies.
  • Click on the + icon to add a new dependency and choose Module Dependency from the drop-down menu.

    Choose Module Dependency

  • The Add Module Dependency window will open. Choose the previously created module of the library in Step1 and in Step2, choose “implementation” and hit OK.

    Type "implementation"

  • You have now successfully added an external library to your project.

In conclusion, adding an external library to our project can save us a lot of time of writing unnecessary code and now we have seen two of the most used methods of doing so. Hope you try them out.