Open In App

How to Add a Library Project to Android Studio?

Last Updated : 28 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads