Adding external library in Android studio

AndroidAndroid StudioAndroid Library

Android Problem Overview


I want to add external library https://github.com/foursquare/foursquare-android-oauth to my Android application (I use Android Studio, the instructions provided by lib author for Eclipse didn't work for Android Studio).

I've tried to do it with maven, so in File->Project Structure->Dependencies I've added com.foursquare:foursquare-android-nativeoauth-lib:1.0.0 but Gradle Sync fails:

Error:Failed to find: com.foursquare:foursquare-android-nativeoauth-lib:1.0.0

When I try to build my app (without fixing above error becaus I don't know how) I get:

Error:A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugCompile'.
> Could not resolve com.foursquare:foursquare-android-nativeoauth-lib:1.0.0.
 Required by:
    ForSquaresOnly:app:unspecified
   > Could not parse POM http://jcenter.bintray.com/com/foursquare/foursquare-android-nativeoauth-lib/1.0.0/foursquare-android-nativeoauth-lib-1.0.0.pom
     > Could not find any version that matches com.foursquare:parent:1.0.0.

Any other way to import this lib? I can simply copy-paste source code into my source or create JAR out of it?

BTW: if you run into problems see this question (I had this issue after importing): https://stackoverflow.com/questions/24438170/manifest-merger-failed-uses-sdkminsdkversion-14

Android Solutions


Solution 1 - Android

Try one of these approaches:

Approach 1)

1- Choose project view

enter image description here

2- Copy your JAR file in app -> lib folder

enter image description here

3- Right click on your JAR file and choose add as library

enter image description here enter image description here

4- Check it in build.gradle

enter image description here


Approach 2)

1- File -> New -> New Module

enter image description here

2- Import .JAR/.AAR Package

enter image description here

3- Browse your JAR File

enter image description here

4- Finish

5- File -> Project Structure -> Dependencies

enter image description here

6- You should click on + button and then click on Module Dependency

enter image description here

7- You will see your library here

enter image description here

8- choose your library and click ok

enter image description here

9- Then, you will see that your library is added.

enter image description here


For first two approaches, you need a JAR file. You can search http://search.maven.org/ to find JAR files that are related to Android. For example, this is the search result for jdom in this link

Search result for jdom


Approach 3) Android is using http://jcenter.bintray.com/ as remote library. For example, this is the search result for jdom in the link.

jcenter jdom

To add a library in this approach, please follow these steps:

1- File -> Project Structure -> Dependencies

enter image description here

2- Click on + button and choose library dependency

enter image description here enter image description here

3- find your library and select it, then click OK.


I hope it helps.

Solution 2 - Android

Try this:

File > Project Structure > Dependencies Tab > Add module dependency (scope = compile)

Where the module dependency is the project library Android folder.

Solution 3 - Android

To reference an external lib project without copy, just do this:

  • Insert this 2 lines on setting.gradle:

    include ':your-lib-name'
    project(':your-lib-name').projectDir = new File('/path-to-your-lib/your-lib-name)
    

Insert this line on on dependencies part of build.gradle file:

compile project(':your-lib-name')

Sync project

Solution 4 - Android

There are two simplest ways if one does not work please try the other one.

  1. Add dependency of the library inside dependency inside build.gradle file of the library you are using, and paste your library in External Libraries.

OR

  1. Just Go to your libs folder inside app folder and paste all your .jar e.g Library files there, Now the trick here is that now go inside settings.gradle file now add this line include ':app:libs' after include ':app' it will definitely work.

Solution 5 - Android

> Any other way to import this lib? I can simply copy-paste source code > into my source or create JAR out of it?

Complete Steps for importing a library in Android Studio 1.1

  1. Goto File -> Import Module.

  2. Source Directory -> Browse the project path.

  3. Specify the Module Name

  4. Open build.gradle (Module:app) file

  5. Add the following line with your module name

    compile project(':internal_project_name')

Taken from: how to add library in Android Studio

Solution 6 - Android

I had also faced this problem. Those time I followed some steps like:

  1. File > New > Import module > select your library_project. Then include 'library_project' will be added in settings.gradle file.

  2. File > Project Structure > App > Dependencies Tab > select library_project. If library_project not displaying then, Click on + button then select your library_project.

  3. Clean and build your project. The following lines will be added in your app module build.gradle (hint: this is not the one where classpath is defined).

     dependencies {
         compile fileTree(dir: 'libs', include: ['*.jar'])
         compile project(':library_project')
     }
    

4. If these lines are not present, you must add them manually and clean and rebuild your project again (<kbd>Ctrl + F9</kbd>).

5. A folder named `library_project` will be created in your app folder.

6. If any icon or task merging error is created, go to `AndroidManifest` file and add `<application tools:replace="icon,label,theme">`

Solution 7 - Android

A late answer, although I thought of giving an in-depth answer to this question. This method is suitable for Android Studio 1.0.0 and above.

STEPS

  1. First switch your folder structure from Android to Project.

enter image description here

  1. Now search for the libs folder inside app - build folder.

enter image description here

  1. Once you have pasted the .jar file inside libs folder. Right click on the jar file and at end click on Add as library. This will take care of adding compile files('libs/library_name.jar') in build.gradle [You don't have to manually enter this in your build file].

enter image description here

Now you can start using the library in your project.

Solution 8 - Android

For the simplest way just follow these steps

  1. Go to File -> New -> Import Module -> choose library or project folder

  2. Add library to include section in settings.gradle file and sync the project (After that you can see new folder with library name is added in project structure)

    include ':mylibraryName'

  3. Go to File -> Project Structure -> app -> dependency tab -> click on plus button

  4. Select module dependency -> select library (your library name should appear there) and put scope (compile or implementation)

  5. Add this line in build.gradle in app level module in dependency section

    implementation project(':mylibraryName')

Solution 9 - Android

Three ways in android studio for adding a external library.

  1. if you want to add libarary project dependency in your project :

A. In file menu click new and choose import module choose your library project path and click ok, library project automatically add in your android studio project .

B. Now open your main module(like app) gradle file and add project dependency in dependency section dependencies {

compile project(':library project name')

  1. if you want to add jar file : A. add jar file in libs folder. B. And Add dependency

compile fileTree(dir: 'libs', include: '*.jar') // add all jar file from libs folder, if you want to add particular jar from libs add below dependency.

compile files('libs/abc.jar')

  1. Add Dependency from url (recommended). like

compile 'com.mcxiaoke.volley:library-aar:1.0.0'

Solution 10 - Android

If the library you need is on GitHub then adding it to Android Studio is easy with JitPack.

Step 1. Add the jitpack repository to build.gradle:

allprojects { 
  repositories {
    jcenter()
    maven { url "https://jitpack.io" }
  }
}

Step 2. Add the GitHub repository as a dependency:

dependencies {
    // ...
    compile 'com.github.Username:LibraryRepo:ReleaseTag'
}

JitPack acts as a maven repository and can be used much like Maven Central. The nice thing is that the maintainers don't have to upload the library. Behind the scenes JitPack will check out the code from GitHub and compile it. Therefore for this to work there needs to be a working build file in the git repository.

There is also a guide on how to publish an Android library.

Solution 11 - Android

Android studio doesn't use Maven, it use Gradle, you can open your build.gradle the route should be: /app/build.gradle and add the Gradle dependency that shows on the repo:

The build.gradle has a dependencies section:

dependencies {
    //here add your dependency
}

The repo says that you need to add this dependency:

compile 'com.foursquare:foursquare-android-oauth:1.0.3'

only add that line to your dependencies on buil.gradle, save the file, and android will rebuild the project, that's all

Solution 12 - Android

Turn any github project into a single line gradle implementation with this website

https://jitpack.io/

Example, I needed this project: https://github.com/mik3y/usb-serial-for-android

All I did was paste this into my gradle file:

implementation 'com.github.mik3y:usb-serial-for-android:master-SNAPSHOT'

Solution 13 - Android

There are some changes in new gradle 4.1

instead of compile we should use implementation

implementation 'com.android.support:appcompat-v7:26.0.0'

Solution 14 - Android

1.Goto File -> New -> Import Module
   2.Source Directory -> Browse the project path.
   3.Specify the Module Name – it is used for internal project reference.

Open build.gradle (Module:app) file.

 implementation project(':library')

Solution 15 - Android

I had the same problem. This happened because of core library dependency. I was using javax.* . This is what i did to fix

In File->Project Structure->Dependencies I added this as as provided file, not a compile. Then re build the project.

This problem started after upgrade of android studio. But I think it happens when you try to edit you build files manually.

Solution 16 - Android

Adding library in Android studio 2.1

Just Go to project -> then it has some android,package ,test ,project view

Just change it to Project View

under the app->lib folder you can directly copy paste the lib and do android synchronize it. That's it

Solution 17 - Android

1)just get your lib from here http://search.maven.org/

2)create a libs folder in app directory

3)paste ur library there

4)right click on ur library and click "Add as Library"

5)thats all u need to do!

I hope this will definitely gonna help you!!!!

Solution 18 - Android

> Could not find any version that matches com.foursquare:parent:1.0.0.

Means that library developer made a mistake in .pom file and specified a wrong one.

I would recommend use the standard approach. You can find .pom file in Maven Repository and use that version or ask developer to take a loot at this problem(via github issue tracker)

compile 'com.foursquare:foursquare-android-oauth:1.1.1'

Solution 19 - Android

SIMPLE STEPS TO ADD ANY LIBRARY IN ANDROID STUDIO:

  1. Copy the file in question from any folder in your computer (eg. C:/documents/xyz.jar"), then go to Android Studio and right-click the Project Library folder and select paste.

  2. Now, right click on the newly added Library file and select the option "add as Library"

D.O.N.E

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarian PaździochView Question on Stackoverflow
Solution 1 - AndroidMohammadView Answer on Stackoverflow
Solution 2 - AndroidmalcubierreView Answer on Stackoverflow
Solution 3 - AndroidPedro SantosView Answer on Stackoverflow
Solution 4 - AndroidSudhanshu GaurView Answer on Stackoverflow
Solution 5 - AndroidKnowITView Answer on Stackoverflow
Solution 6 - AndroidRatna HalderView Answer on Stackoverflow
Solution 7 - Androidcapt.swagView Answer on Stackoverflow
Solution 8 - Androidkunal khedkarView Answer on Stackoverflow
Solution 9 - AndroidvinodView Answer on Stackoverflow
Solution 10 - AndroidAndrejsView Answer on Stackoverflow
Solution 11 - AndroidCarlos Herrera PlataView Answer on Stackoverflow
Solution 12 - AndroidcagneyView Answer on Stackoverflow
Solution 13 - AndroidVahe GharibyanView Answer on Stackoverflow
Solution 14 - AndroidSoumen DasView Answer on Stackoverflow
Solution 15 - AndroidsudheerView Answer on Stackoverflow
Solution 16 - AndroidVinayak ShedgeriView Answer on Stackoverflow
Solution 17 - AndroidAditya KumarView Answer on Stackoverflow
Solution 18 - AndroidyoAlex5View Answer on Stackoverflow
Solution 19 - AndroidJohn yepthomiView Answer on Stackoverflow