Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How do I add a library project (such as Sherlock ABS) to the Android Studio?

(Not to the old ADT Eclipse-based bundle, but to the new Android Studio.)

share|improve this question
12  
yeah figuring this out was way more confusing than it should have been... not like it was any better in eclipse. –  Daniel Smith May 21 '13 at 10:52
    
The YouTube video How to Add Libraries to Android Studio explains the process. –  Mohammed Jan 19 '14 at 19:43
    
If you're using Gradle, see this StackOverflow answer. –  craned Oct 16 '14 at 19:55
    
Here is a video which I found to be really useful: youtube.com/watch?v=1MyBO9z7ojk –  Simon Dec 20 '14 at 14:30

19 Answers 19

up vote 269 down vote accepted

Update for Android Studio 1.0

Since Android Studio 1.0 was released (and a lot of versions between v1.0 and one of the firsts from the time of my previous answer) some things has changed.

My description is focused on adding external library project by hand via gradle files (for better understanding the process). If you want to add library via Android Studio creator just check the answer below with visual guide (there are some differents between Android Studio 1.0 and those from screenshots, but the process is very similar).

Before you start adding library to your project by hand consider adding external dependency. It won’t mess in your project structure. Almost every well known Android library is available in maven repository and its installation takes only one line of code in app/build.gradle file:

dependencies {
     compile 'com.jakewharton:butterknife:6.0.0'
}

Adding the library

Here is the full process of adding external Android library to our project:

  1. Create new project via Android Studio creator. I named it HelloWorld
  2. Here is the original project structure created by Android Studio:
HelloWorld/
      app/
           - build.gradle  // local gradle config (for app only)
           ...
      - build.gradle // global gradle config (for whole project)
      - settings.gradle 
      - gradle.properties
      ...
  1. In root directory (HelloWorld/) create new folder: /libs in which we’ll place our external libraries (this step is not required - only for keeping cleaner project structure).
  2. Paste your library in newly created /libs folder. In this example I used PagerSlidingTabStrip library (just download ZIP from Github, rename library directory to „PagerSlidingTabStrip" and copy it). Here is the new structure of our project:
HelloWorld/
      app/
           - build.gradle  // local gradle config (for app only)
           ...
      libs/
           PagerSlidingTabStrip/
                - build.gradle // local gradle config (for library only)
      - build.gradle // global gradle config (for whole project)
      - settings.gradle 
      - gradle.properties
      ...
  1. Edit settings.gradle by adding your library to include. If you use custom path like I did, you have also define project directory for our library. Whole settings.gradle should look like below:

    include ':app', ':PagerSlidingTabStrip'
    project(':PagerSlidingTabStrip').projectDir = new File('libs/PagerSlidingTabStrip')
    
  2. In app/build.gradle add our library project as an dependency:

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:21.0.3'
        compile project(":PagerSlidingTabStrip")
    }
    
  3. If your library project doesn’t have build.gradle file you have to create it manually. Here is example of that file:

    apply plugin: 'com.android.library'
    
    dependencies {
        compile 'com.android.support:support-v4:21.0.3'
    }
    
    android {
        compileSdkVersion 21
        buildToolsVersion "21.1.2"
    
        defaultConfig {
            minSdkVersion 14
            targetSdkVersion 21
        }
    
        sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                java.srcDirs = ['src']
                res.srcDirs = ['res']
            }
        }
    }
    
  4. Additionaly you can create global config for your project which will contain SDK versions and build tools version for every module to keep consistency. Just edit gradle.properties file and add lines:

    ANDROID_BUILD_MIN_SDK_VERSION=14
    ANDROID_BUILD_TARGET_SDK_VERSION=21
    ANDROID_BUILD_TOOLS_VERSION=21.1.3
    ANDROID_BUILD_SDK_VERSION=21
    

    Now you can use it in your build.gradle files (in app and libraries modules) like below:

    //...
    android {
        compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
        buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
    
        defaultConfig {
            minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
            targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
        }
    }
    //...
    
  5. That’s all. Just click ‚Sync project with gradle’ icon sync with gradle. Your library should be available in your project.

...

(Removed outdated description here)

...

Here you have great presentation about building Android apps with Gradle Build System - http://www.youtube.com/watch?v=LCJAgPkpmR0. As Xavier Ducrohet said

Android Studio is all about editing, and debugging and profiling. It's not about building anymore.

At the beginning it may be little bit confusing (especially for those, who works with Eclipse and have never seen the ant - like me ;) ) but at the end Gradle gives us some great opportunities and it worth to learn this build system.

share|improve this answer
5  
hey there, you perfectly explained the real issue. voting up your answer so other people can see it. one minor thing that didn't work on my side is referencing the support library in the dependency's build.gradle. as it turns out ':Project/libs...' did not work for some reason. i had to do away with the project identification. so 'libs/...'. and it compiled fine. one note is that these two ways of referencing paths or projects is confusing. i hope a simple and unified scheme comes up in the future. there is no reason why the original solution wouldn't work. –  victor n. May 24 '13 at 1:56
8  
+1. Was struggling with this for a long time. As victor n said, referencing the main project's support jar from actionbarsherlock module doesn't work. I had to do the following to get it working... (1) Remove compile files('libs/android-support-v4.jar') from the project's build.gradle. The main module should only have compile project(":libraries:actionbarsherlock"). (2) Add compile files('libs/android-support-v4.jar') instead of compile files(':HelloWorld/libs/android-support-v4.jar') in actionbarsherlock's build.gradle. –  Akbar ibrahim May 24 '13 at 12:39
6  
Why dont i have "Import Module" section, at the option where you direct us ? I'm using Android Studio 0.3.1 –  alicanbatur Nov 7 '13 at 12:00
5  
Import Module option is missing!! –  amalBit Feb 8 '14 at 12:15
11  
Why is this so god damn confusing?! –  Kyle Gobel Oct 19 '14 at 19:15

Here is the visual guide for lazy guys like me:

Update for Android Studio 0.8.2:

In Android Studio 0.8.2, go to Project Structure > under Modules just hit the plus button and select Import Existing Project and import actionbarsherlock. Then sync your Gradle files.

If you face the following error:

Error: The SDK Build Tools revision (xx.x.x) is too low. Minimum required is yy.y.y

Just open the build.gradle file in actionbarsherlock directory and update the buildToolsVersion to the suggested one.

android {
  compileSdkVersion 19
  buildToolsVersion 'yy.y.y'

Android Studio 0.8.2


File > Project Structure

First

Module > Import Module

Second

After importing the library module, select your project module and add dependency:

Third

And then select the imported module:

Forth

share|improve this answer
58  
Thanks! But if you're a lazy guy, why did you take the time to take all those screenshots? :) –  Ruud Lenders Aug 30 '13 at 22:41
17  
Import module option not available in Android Studio4.3 –  amalBit Feb 8 '14 at 12:17
6  
@RuudLenders i think he is a lazy and nice guy :D –  Blaze Tama Jul 21 '14 at 6:52
2  
The key for me to make this work and resolve the package not found errors was the step Select your project module and add dependency dependencies { // ... compile project(':library') } –  toobsco42 Aug 25 '14 at 21:11
3  
This will copy the library module in your project, which is not what you want if you want a common code base shared between several projects –  Rémy DAVID Oct 6 '14 at 14:17
File -> Project  Structure -> Modules

I started using it today. It is a bit different.

For Sherlock, maybe you want to delete their test directory, or add the junit.jar file to the classpath.

Edit:

AndroidStudio is changing.

I find out that exist a section named "Open module settings" if you right-click on a module folder in the project section of android studio (I'm using the version 0.2.10)

thanks @gipi for the comment below

share|improve this answer
1  
Hmm it still says android-apt-compiler: styles.xml:5: error: Error retrieving parent for item: No resource found that matches the given name 'Theme.Sherlock.Light.DarkActionBar'. –  Alex May 16 '13 at 13:00
    
Under the Sherlock Module -> Dependecies I have android 4.2.2 and the support Library v4 –  Blackbelt May 16 '13 at 13:06
    
@ρяσѕρєяK next to the run button, there is a label with Android. If you ckick there you will find Edit Configuration. Push and under Target Device click on "Show Chooser Dialg" –  Blackbelt May 16 '13 at 13:07
7  
Hi, I've the same problem like Alex ("Gradle: Error retrieving parent for item: No resource found that matches the given name 'Theme.Sherlock.Light'.") - the Java dependency seems to be solved correctly, cause the IDE doesn't blame my SherlockActivity, but for some reason Gradle doesn't seem to look into the ressources of ABS. Any ideas? –  fish May 16 '13 at 15:51
3  
On Android Studio 0.2.8 there is no Modules section in the Project Structure window. –  Konrad Morawski Sep 15 '13 at 21:34

I would consider Dependencies, Android Libraries and Multi-project setup necessary reading. Please take a few minutes to do so.

Particularly, in the case of a non-jar library project, read the following snippet from above source:

Gradle projects can also depend on other gradle projects by using a multi-project setup. A multi-project setup usually works by having all the projects as sub folders of a given root project.

For instance, given to following structure:

MyProject/
 + app/
 + libraries/
    + lib1/
    + lib2/

We can identify 3 projects. Gradle will reference them with the following name:

:app
:libraries:lib1
:libraries:lib2

Each projects will have its own build.gradle declaring how it gets built. Additionally, there will be a file called settings.gradle at the root declaring the projects. This gives the following structure:

MyProject/
 | settings.gradle
 + app/
    | build.gradle
 + libraries/
    + lib1/
       | build.gradle
    + lib2/
       | build.gradle

The content of settings.gradle is very simple:

include ':app', ':libraries:lib1', ':libraries:lib2'

This defines which folder is actually a Gradle project.

The :app project is likely to depend on the libraries, and this is done by declaring the following dependencies:

dependencies {
    compile project(':libraries:lib1')
}

Kindly note that there was little or no use of Android Studio GUI to make this happen.

I am currently using git submodules to link the nested library to the actual library git repo to avoid a dependency mess.

share|improve this answer
    
Kindly upvote if you found this helpful, so that others can avoid the pain and move along faster. –  Jonathan Lin Oct 14 '13 at 8:13
    
Very helpful and, more importantly, not play to the gui whims of the day. –  RichieHH Mar 24 '14 at 23:09
1  
thank you so much for including the only answer that makes sense. every other answer is outdated and just doesn't work. i didn't even try in CS at Virginia Tech because the whole first two years was learning to fckn click around in eclipse. it didn't make any fckn sense, but seeing the gradle result of random clicks in an IDE does. –  dcunited001 Apr 20 '14 at 8:12

For anyone who may just be finding this. If you need access to the resources of a library project (as you do with ABS) ensure that you add the library project/module as a "Module Dependency" instead of a "Library".

share|improve this answer
1  
Yes! You sir win the Internet! Thanks! I have just wasted a couple of hours of my life because I didn't know that. –  cosimo Apr 1 '14 at 18:51

Simple way to add JAR file as library to your Android Studio project:

a) Copy your *.jar files

b) Paste into libs directory under your projects

enter image description here

c) Add to build.gradle

dependencies {
    ...
    compile files('libs/ScanAPIAndroid.jar', 'libs/ScanAPIFactoryAndroid.jar', .., ..)
}

b) IF Your project from example com.example.MYProject and libraries com.example.ScanAPI has the same namespace com.example: Android studio will check your build and create all necessary changes in your project. After than you can review these settings in File->Project Structure

c) IF Your project and libraries has different namespace you have to RigthClick on library and select OPTION "Add as Library" and select type what you need.

Remember "Project structure" option is not doing any auto changes in "build.gradle" in current version Android studio (0.2.3) maybe this feature will be available in next versions.

share|improve this answer
    
Thank you, this was the easiest way to do it! –  javaProgrammer Apr 23 '14 at 18:33

To add to the answer : If the IDE doesn't show any error but when you try to compile, you get something like :

No resource found that matches the given name 'Theme.Sherlock.Light'

Your library project is probably compiled as an application project. To change this, go to :

File > Project structure > Facets > [Library name] > Check "Library module".

share|improve this answer
  1. Press F4 show Project Structure, click libraries or Global libraries, click + add the jar
  2. Click Modules what you wannt add jar, Select Dependencies Tab, click +, add Library
share|improve this answer

After importing the ABS Module (from File > Project Structure) and making sure it has Android 2.2 and Support Library v4 as dependencies, I was still getting the following error as you @Alex

Error retrieving parent for item: No resource found that matches the given name 'Theme.Sherlock.Light.DarkActionBar'

I added the newly imported module as a dependency to my main app module and that fixed the problem.

share|improve this answer
    
I tried all steps inclusing adding the module as a dependency but I still have Gradle: Error retrieving parent for item: No resource found that matches the given name 'Theme.Sherlock.Light'.. It seems like my main project does not see the resources of the library project (ABS). The IDE however does recognize the references to classes and resources. Screenshot –  christiaanderidder May 19 '13 at 8:25
    
That worked for me. Thanks! –  marbdq May 21 '13 at 14:43
    
Same here. Who can solved this? –  Calvin Chan Jun 4 '13 at 14:45
    
I'm migrate to IDEA 13 from 12 and have this error too. Only re-import for all my dependencies will help. Manually remove all modules, remove related *.iml files and re-import your libs –  Dmitriy Tarasov Jun 6 '13 at 13:37

Editing library dependencies through GUI is not advisable as that doesn't writes those changes to your build.gradle file. So your project will not build from command-line. We should edit build.gradle file directly as follows.

For instance, given to following structure:

MyProject/

  • app/
  • libraries/
    • lib1/
    • lib2/

We can identify 3 projects. Gradle will reference them with the following name:

  1. :app
  2. :libraries:lib1
  3. :libraries:lib2

The :app project is likely to depend on the libraries, and this is done by declaring the following dependencies:

dependencies { compile project(':libraries:lib1') }

share|improve this answer
    
Really? Strange, they have the whole GUI and yet it's not advisable. How so? –  Alex May 23 '13 at 12:08
    
As editing through GUI doesn't writes those changes to your build.gradle file. GUI editing only saves changes in IntelliJ's own project data. This is a bug at the moment which will be fixed in future releases. You can refer to this answer from Google Android-Studio Team Lead stackoverflow.com/questions/16678447/… –  Shakti Malik May 23 '13 at 12:10

To resolve this problem, you just need to add the abs resource path to your project build file, just like below:

sourceSets {
    main {
        res.srcDirs = ['src/main/res','../../ActionBarSherlock/actionbarsherlock/res']
    }
}

So, I again compile without any errors.

share|improve this answer

The easiest way I found to include external library project is (for example to include facebook lib which stored one directory up in dependencies folder)

1. in settings.gradle add

include ':facebook'

project(':facebook').projectDir = new File(settingsDir, '../dependencies/FacebookSDK')

2. in build.gradle dependencies section add

compile project ('facebook')

all left to do is sync project with gradle files

share|improve this answer

If you have Android Studio .0.4.0, you can create a new folder in your build path, YourApp/libraries. Copy the JAR file. There in, right click on it and "Add As Library". Now you have a popup. Just select your directory and press OK, and that's it.

share|improve this answer

This is the dropbox link of how to Add jar File and Library Project in latest Version of Android Studio 1.0.1.

Please see the doc with screenshots its very easy for new user.

https://www.dropbox.com/s/1e3eteu3h0pmkf7/Android%20studio%20_doc.doc?dl=0

share|improve this answer

Hi, lazy guys like me ;)

Here is solution for

Android Studio 1.0

How To Import Material Design Library To Android Studio

share|improve this answer

I found the solution. It's so simple. Follow froger_mcs instructions.

Make sure that you make the src folder a Source folder in Project Structure -> Modules (Sources).

Enter image description here

share|improve this answer

very old question but I had a different cause of the problem so for peaple:

repositories {
    mavenCentral()
}

change mavenCentral() to jcenter() and add

allprojects {
repositories {
    jcenter()
}
}
share|improve this answer

An example of succesfully adding another library (PullToRefresh). Also works for ABS libproject.

This SO Question

This post

share|improve this answer

open build gradle module app file and add your dependency if you download library just import and build as gradle

other wise add repositories in side gradle module app

repositories {
        maven { url 'http://clinker.47deg.com/nexus/content/groups/public' }
}

first repositories will download library for you

add compile the downloaded library

 compile ('com.fortysevendeg.swipelistview:swipelistview:1.0-SNAPSHOT@aar') {
        transitive = true
    }

If you are created a library you just need to import the project as import new module

share|improve this answer

protected by Community May 23 '13 at 21:24

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.