Adding External Library in Android Studio - Androhub

Adding External Library in Android Studio

Adding an external library in Android Studio is a very common thing but still, most beginners or freshers find it difficult to do. So let’s learn how to add any external library to our Android Studio project and the pros & cons associated with it.

VIDEO TUTORIAL DEMO

Let’s discuss three ways of adding an external library as follows:

  1. Adding Gradle dependency.
  2. Adding .jar or .aar dependency.
  3. Adding dependency as a module.

1. Adding Gradle Dependency

It is very easy to add Gradle dependency. Most of us are familiar with this method as it is a common way and recommended too. Suppose you want to add Picasso to your project then you can add Gradle dependency as mentioned below to your project in build.gradle at the app level inside the dependencies block.

In the above implementation, 2.71828 is the latest version of the library. It will be different for different libraries and you can get it from respective Github repositories or release note’s pages.

Pros: If we need the updated version of the library we can directly see the latest version over its Github repository and replace it with the existing one and our library will be updated.

Cons: If you want to change any kind of code in the existing library then it’s not possible as it is read-only.

2. Adding .jar/.aar Dependency

In some cases, we don’t get the Gradle dependency of the library. Instead, we get only the .jar/.aar file. In such case you can follow the below steps:

  1. Find the .jar/.aar file and download it.
  2. Then copy the file from the downloaded location and go to Android Studio.
  3. Go to the App module and paste the file inside the libs folder. (If you don’t see the libs folder, create a new directory inside the App module.)
  4. After adding the file right-click on it and select Add As Library.

    Add As Library
  5. Then select the module you want to add the library into.

    Add to App Module
  6. By doing that it will be added to your project and you can access the library.

Pros: I don’t see any pro for this. As we only need to add any .jar/.aar dependency if the library provider doesn’t have any other way to do.

Cons:

  1. Here updating the library is a bit lengthy task as we again need to download the latest jar and follow the same steps as mentioned above.
  2. You cannot modify the code if you want to as the files are read-only.

3. Adding dependency as a module

There can be a situation when we need to modify the library code according to our requirement then in that case we can follow this method.

  1. Go to the Github repo link of the library you are looking for.
  2. Click on the green color Code dropdown button.

    Download Github Repository
  3. Select Download Zip and save it in your system.
  4. Once downloaded extract the code.
  5. You will find the two main directory name sample and other <library name>.
  6. Go to Android Studio and navigate to File -> New -> Import Module -> Select the library path -> Finish.
    Import New Module

    Select Module to Import
  7. Then right-click on the App directory -> Open Module Settings -> Dependencies -> Click on + button inside Declared Dependencies -> Module Dependency -> Select your library -> Ok.
    Open Module Settings

    Select Module Dependency

Pros: You can modify the code according to your requirement.

Cons: Getting the updated version of the library can be difficult since we are making code changes here.

So these are the three ways of adding any external library to the Android Studio project. Hope this will help you guys.

Thanks. :)

 

Post comment

Your email address will not be published. Required fields are marked *