How to Add a Library Project to Android Studio?
Last Updated :
28 Apr, 2022
Adding an external library in Android Studio is a very common thing but still, most beginners or freshers find it difficult to do. Some of you must have gone through this. Whenever you want to add some external library and you try to do that with maven(File > Project Structure > Dependencies) and try to add your dependencies there but when you hit Gradle sync, it fails. So now, without fixing this, you want to go for building your app then you will get the error: Error: A problem occurred configuring project ':app'. There are many external libraries available that make many specific tasks simple and easy with the APIs and Classes they provide. So including all those libraries in our project is really going to help you a lot. Many developers are often confused about how to add these libraries to their projects. This article is helpful for all such people.
In this article, we will see different ways to add library projects to our project. But before jumping to that let's have a quick look at this: We can use Gradle, to add library projects with a single line code. Android Studio Project has a modular structure and we have a main module called “app”. There may be multiple modules in a Project. We can connect them together through Gradle, this connection is known as Dependency. Now Let's get back to our main agenda.
Different Ways To Add Library Project to Android Studio
Method 1
Almost every well-known Android library is available in a Maven repository and its installation takes only one line of code in the app/build.gradle file:
dependencies {
compile 'com.jakewharton:butterknife:6.0.0'
}
Let's add the external library to our project:
Step 1: Create a new project using Android Studio and name it anything you want (GFG in this example) and hit the finish button.

Step 2: The original project structure created by Android Studio looks like this:

Step 3: In the root directory (GFG/), create a new folder: /libs in which we’ll place our external libraries (this step is not required - only for keeping a cleaner project structure).

Step 4: Paste your library in the newly created /libs folder. For this example, we are using the PagerSlidingTabStrip library (you just need to download ZIP from GitHub, rename the library directory to "PagerSlidingTabStrip" and copy it). The new structure of our project should look like this:

Step 5: Edit settings.gradle by adding your library to include. If you use a custom path like I did, you have also to define the project directory for our library. A whole settings.gradle should look like below:
include ':app', ':PagerSlidingTabStrip'
project(':PagerSlidingTabStrip').projectDir = new File('libs/PagerSlidingTabStrip')

Step 5.1: If you face "Default Configuration" error, then try this instead of step 5,
include ':app'
include ':libs:PagerSlidingTabStrip'
Step 6: In app/build.gradle add our library project as a dependency:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile project(":PagerSlidingTabStrip")
}
Step 6.1: If you followed step 5.1, then follow this instead of 6,
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile project(":libs:PagerSlidingTabStrip")
}
Step 7: If your library project doesn’t have build.gradle file you have to create manually.
Step 8: That’s all. Just click‚ sync the project with Gradle. Your library should be available for your project.
Method 2
Step 1: Follow to File > New Module

Click on "Import Existing Project".

Step 2: Select the desired library and the desired module. Then click finish. Android Studio will import the library into your project and will sync Gradle files.
Step 3: In the next step you need to add the imported module to your project's dependencies. Right-click on the app folder > Open Module settings

Step 4: Navigate to the dependencies tab > Click on the '+' button -> click on Module Dependency. The library module will be then added to the project's dependencies.

Method 3
Step 1: Navigate to File > Project Structure

Step 2: Then click on Modules.

To import the library using Gradle, you can have to add it to the dependencies section of your build.gradle (the module's one).
Method 4
Just go to Project Structure > under Modules (see below images) just click the plus button and select Import Existing Project and import. Then sync your Gradle files. There are chances to face the error
Error: The SDK Build Tools revision (xx.x.x) is too low. Minimum required is yy.y.y
Then just open the build.gradle file in your library project directory and update the buildToolsVersion to the suggested one.
android {
compileSdkVersion 19
buildToolsVersion 'yy.y.y'
}
then try importing the module by clicking on File > Project Structure

Module > Import Module

After importing the library module, select your project module and add the dependency and then select the imported module.
Similar Reads
How to Add Support Library to Android Studio Project? An android support library is part of Android SDK tools. You can use the Android Support Library for backward-compatible versions of new Android features and additional UI elements and features not included in the standard Android framework. Each package in the support library has a version number i
3 min read
How to Include *.so Library in Android Studio? The SO file stands for Shared Library. You compile all C++ code into the.SO file when you write it in C or C++. The SO file is a shared object library that may be dynamically loaded during Android runtime. Library files are larger, often ranging from 2MB to 10MB in size. As a result, the app becomes
3 min read
How to Create/Start a New Project in Android Studio? After successfully installing the Android Studio and opening it for the first time. We need to start with some new projects to start our journey in Android.In this article, we will learn about How to Create a New Project in Android Studio.Steps to Create/Start a New Android Project in Android Studio
2 min read
How to Add OpenCV library into Android Application using Android Studio? OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in todayâs systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a
3 min read
How to Clone Android Project from GitHub in Android Studio? Android Studio is the official IDE for Android app development, based on IntelliJ IDEA. It supports building apps for Android phones, tablets, Wear OS, TV, and Auto using Java or Kotlin for backend and XML for UI design. Git is an open-source version control system that tracks changes in code, makin
2 min read