Android Studio - Importing external Library/Jar

130,014

Solution 1

So,

Steps to follow in order to import a JAR sucesfully to your project using Android Studio 0.1.1:

  • Download the library.jar file and copy it to your /libs/ folder inside your application project.
  • Open the build.gradle file and edit your dependencies to include the new .jar file:

compile files('libs/android-support-v4.jar', 'libs/GoogleAdMobAdsSdk-6.4.1.jar')

  • File -> Close Project
  • Open a command prompt on your project's root location, i.e 'C:\Users\Username\AndroidStudioProjects\MyApplicationProject\'
  • On the command prompt, type gradlew clean, wait till it's done.
  • Reopen your application project in Android Studio.
  • Test run your application and it should work succesfully.

Solution 2

You don't need to close the project and go to command line to invoke grade:clean. Go to Build-> Rebuild Project

Solution 3

Easy way and it works for me. Using Android Studio 0.8.2.

  1. Drag jar file under libs.
  2. Press "Sync Project with Gradle Files" button.

enter image description here

Solution 4

"simple solution is here"

1 .Create a folder named libs under the app directory for that matter any directory within the project..

2 .Copy Paste your Library to libs folder

3.You simply copy the JAR to your libs/ directory and then from inside Android Studio, right click the Jar that shows up under libs/ > Add As Library..

Peace!

Solution 5

Here is how I got it going specifically for the admob sdk jar file:

  1. Drag your jar file into the libs folder.
  2. Right click on the jar file and select Add Library now the jar file is a library lets add it to the compile path
  3. Open the build.gradle file (note there are two build.gradle files at least, don't use the root one use the one in your project scope).
  4. Find the dependencies section (for me i was trying to the admob -GoogleAdMobAdsSdk jar file) e.g.

    dependencies {
       compile files('libs/android-support-v4.jar','libs/GoogleAdMobAdsSdk-6.3.1.jar')
    }
    
  5. Last go into settings.gradle and ensure it looks something like this:

    include ':yourproject', ':yourproject:libs:GoogleAdMobAdsSdk-6.3.1'
    
  6. Finally, Go to Build -> Rebuild Project
Share:
130,014
daniel_c05
Author by

daniel_c05

Updated on August 03, 2020

Comments

  • daniel_c05
    daniel_c05 almost 4 years

    I recently downloaded Android Studio to develop Android applications (I'm coming from Eclipse), and I am having issues using external libraries and/or external JAR files alongside my own project. NOTE: the following tests were conducted on a new application project created from scratch in Android Studio.

    Example 1: JAR Import.

    1. Download a fresh copy of the Admobs SDK from Google.

    2. Copy the library jar GoogleAdMobAdsSdk-6.4.1.jar to the project's /libs/ folder.

    3. In the project explorer, right click on the newly added library.jar and click on 'Add as Library'.

    Technically at this point everything works, imports work just fine, the layout editor shows a preview of the AdView widget and all that. The only problem is that it doesn't compile successfully.

    Log from console:

    Gradle: 
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':APITests:compilePaidDebug'.
    > Compilation failed; see the compiler error output for details.
    
    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
    
    Could not execute build using Gradle distribution 'http://services.gradle.org/distributions/gradle-1.6-bin.zip'.
    

    I tried running gradlew compileDebug --stacktrace, and the problem seems to be that the despite being able to import the classes successfully in both the code & design editor, at compile time, it cannot resolve the imports. Here's the relevant part of the log: (full stacktrace here)

    java:6: error: package com.google.ads does not exist
    import com.google.ads.AdRequest;    
    java:7: error: package com.google.ads does not exist
    import com.google.ads.AdView;
    java:11: error: cannot find symbol
    AdView mAdView;
    symbol:   class AdView
    location: class MainActivity
    java:22: error: cannot find symbol
    mAdView = (AdView)this.findViewById(R.id.adView);
    symbol:   class AdView
    location: class MainActivity
    java:23: error: cannot find symbol
    mAdView.loadAd(new AdRequest());
    symbol:   class AdRequest
    location: class MainActivity
    5 errors
    :Test:compileDebug FAILED
    

    But again, the imports work well in the editor, and the dependency is there:

    enter image description here

    It's also worth noting that without attempting to add a library/JAR, the projects compiles just fine.

    I then tried editing the build.gradle file to include the new lib like this:

    dependencies {
    compile files('libs/android-support-v4.jar', 'libs/GoogleAdMobAdsSdk-6.4.1.jar')
    }
    

    This time, it did compile successfully, but the app now forces closes, as apparently, it cannot find a specific class from the lib in the application package.

    Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.ads.AdView" on path: /data/app/com.foo.test-1.apk
    

    Any ideas?

  • daniel_c05
    daniel_c05 almost 11 years
    The two projects I'm testing with were not exported from Eclipse, they were both created from scratch with Android Studio.
  • abbood
    abbood almost 11 years
    i know eh.. my command line on mac didn't even recognize gradlew, and issuing brew install gradle is making me wait an hour for it to install.. kudos to you big man
  • bompf
    bompf over 10 years
    gradlew is an executable located in the project root, you probably would have had to write ./gradlew instead of simply gradlew.
  • Oh Danny Boy
    Oh Danny Boy over 10 years
    There is no Add Library menu item in Android Studio 0.2.5
  • IgorGanapolsky
    IgorGanapolsky over 10 years
    Why is settings.gradle necessary here? I don't think one needs to specify jars in settings.gradle!
  • David M
    David M over 10 years
    FYI, fileTree can be used to import all JARs in a directory. For example: compile fileTree(dir: 'libs', include: '*.jar')
  • Chris623
    Chris623 over 10 years
    instead of editing the build.gradle file manually, you can just right-click on the jars in the project explorer and select "Add as Library"
  • Y2i
    Y2i over 10 years
    The advice on closing AS, cleaning from command line and re-opening AS solved all my compilation problems!
  • George Baker
    George Baker over 9 years
    Closing AS and running gradlew clean did it for me to. You would think that sync gradle from within project would have.
  • Kitalda
    Kitalda almost 9 years
    This works for me :) By far the simplest way of doing it.
  • Daniel Adenew
    Daniel Adenew over 8 years
    This is an awsome solution :)
  • Mijo
    Mijo about 5 years
    It was added automatically as a dependency in the project in intellij idea