The best way to integrate third party library in Android studio

63,160

Solution 1

I prefer to use central repository for dependencies management. So for gson 2.3 dependency you should add to build.gradle file:

  • Specify that you want to use maven central repository for your dependency

    repositories {jcenter()}

  • Add compile dependency to gson 2.6.2

    dependencies {compile 'com.google.code.gson:gson:2.6.2'}

Android Studio as well as your CI server should easily build your project now. And you can continue app development.

I prefer to use central repository for dependencies management because:

  • easier scope management - some libraries are only for testing, some should be included to apk and some are part of running environment (like android.jar itself)
  • easier transitive dependencies management - it is quite hard to collect libraries dependencies and if you use "jar-with-dependencies" you could get error "class already added" during dexing
  • lighter repository and easier dependency upgrade

Examples:

  • Robolectric jar should be used for unit testing only and shouldn't be part of apk itself
  • Repository is clean from different folders with jars, checkout takes much less. No needs to download and replace old jars with new jars

I should notice:

  • Not many libraries are in maven central and you should make some effort to use them such way in your project
  • You could much easier get to "class already added" error during dexing with central repository approach
  • You can mix usage of dependencies from central repository and from lib folder, but I prefer to use only one way for simplicity

Solution 2

  1. Put the Gson jar (in my case, gson-2.2.4.jar) into the libs folder
  2. Right click it and hit 'Add as library'
  3. Ensure that compile files('libs/gson-2.2.4.jar') is in your build.gradle file
  4. Do a clean build (you can probably do this fine in Android Studio, but to make sure I navigated in a terminal to the root folder of my app and typed gradlew clean. I'm on Mac OS X, the command might be different on your system

This series of steps was taken from Android Studio: Add jar as library? and is not my original answer. I am posting them here, again, because your question was the third in search results on Google when looking up this same topic. Hence, copying.

All credits to the one who wrote the steps.

Solution 3

Download & Copy Your .jar file in libs folder then adding one line to build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar']) ----> AS creates this
    compile 'com.google.code.gson:gson:2.3.4'   ----------> I added this one
}

Do not forget to click "Sync now"

I´m using Android Studio 1.1.0

Solution 4

Download and copy your jar to libs folder then add the following to your app.gradle file and SYNC.

dependencies {
     compile 'com.google.code.gson:gson:{version_you_need}'
}

repositories{
  flatDir{
      dirs 'libs'
  }
}
Share:
63,160

Related videos on Youtube

Bagusflyer
Author by

Bagusflyer

Updated on July 09, 2022

Comments

  • Bagusflyer
    Bagusflyer almost 2 years

    We can find some very good open source libraries for android. I want to know what is the best way to integrate them to our own projects in Android studio. Here are some basic methods:

    • Copy the source code and resource files into our own project. We need to change a lot of codes (the package name, and the name in xml,etc)
    • If jar files is provided, I just create libs folder for my project and copy the jar files inside. And add the jar file in Module setting's dependencies. But unfortunately I got a lot of error messages like "Gradle: Package com.google.gson doesn't exist".

    Is there a general rule to add third party source or jar files into an existing android studio project? Thanks

    • Bagusflyer
      Bagusflyer almost 11 years
      I found one post is good for jar file integration. stackoverflow.com/questions/16608135/… But for some projects with some resources file doesn't provide a jar file.
    • An SO User
      An SO User almost 11 years
      Yes, what bagusflyer said ..
  • Bagusflyer
    Bagusflyer almost 11 years
    That's so nice. But how can I know which library is in center repository?
  • William T. Mallard
    William T. Mallard over 10 years
    Apparently you can now drag and drop the jar from wherever to your project's libs folder and it gets added in to the build path automagically. I worked fine for me, but YMMV.
  • tonga
    tonga over 9 years
    @EugenMartynov: There are two build.gradle files in my project. One in the project root and the other in app directory. Which file to put the repositories and dependencies section?
  • Pratik Butani
    Pratik Butani over 9 years
    Now new version available compile 'com.google.code.gson:gson:2.3'
  • Timo
    Timo about 9 years
    The line about maven repository should not be needed, the default Android Studio repository jcenter() should be a superset of the mavenCentral() repository so it should already contain everything there is and more. For more information see this SO question: stackoverflow.com/questions/25137263/…
  • aditya_medhe
    aditya_medhe almost 8 years
    Thanks. And what import statement will go into the .java file in which we are using the library?
  • Hitesh Sahu
    Hitesh Sahu over 7 years
    The answer is 2 year older and jar has been update to 2.6.2 ,see updated answer
  • NeoWang
    NeoWang almost 6 years
    What if I want to change some code in the third-party library? What is the best practice? Build jar file and add as library?
  • Eugen Martynov
    Eugen Martynov almost 6 years
    I would say there are two possible solutions - use start use library from sources and then you modify and compile it; or you modify sources, compile to binary and upload it to some repository from where you can use it.