How to Add a Jar in External Libraries in Android Studio

Adding external library (.jar) in Android Studio

I ran the gradlew.bat file, which gave me the following Exception:

Exception in thread "main" java.lang.RuntimeException: Could not determine wrapper version.at
org.gradle.wrapper.GradleWrapperMain.wrapperVersion(GradleWrapperMain.java:111)at
org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)Caused by:
java.lang.RuntimeException: No build receipt resource found.at
org.gradle.wrapper.GradleWrapperMain.wrapperVersion(GradleWrapperMain.java:97)... 1 more

which brought me to this answer and indeed i had used an ! in my project name. Removing it somehow solved the problem.

How to add external dependencies (jar file) to android studio?

Step wise

  1. Copy JAR File to libs Folder
  2. Register module in build.gradle file, instructions for which are given in steps 3 through 9
  3. Open file menu and click on project structure
  4. Now in Project Structure dialog box select app under module
  5. Now Click on Dependencies tab in project structure dialog
  6. Click on + sign in right side corner
  7. Select File Dependency from list
  8. Select jar file from libs folder
  9. Click apply and Ok
  10. Finally click on sync gradle button

One more thing, check for proxy connection if you are using it.

Android Studio: Add jar as library?

I've been struggling with the same thing for many hours, trying to get the Gson jar to work no less. I finally cracked it – here are the steps I took:

  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 (or compile fileTree(dir: 'libs', include: '*.jar') if you are using many jar files)

    Edit : Use implementation files('libs/gson-2.2.4.jar') (or implementation fileTree(dir: 'libs', include: '*.jar')) in Android Studio 3.0+

  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

After I did the above four, it started working fine. I think the 'Add as library' step was the one I'd previously missed, and it didn't work until I cleaned it either.

[Edit - added the build.gradle step which is also necessary as others have pointed out]

Adding external library in Android studio

Try this:

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

Where the module dependency is the project library Android folder.

importing jar libraries into android-studio

Updated answer for Android Studio 2

The easy and correct way to import a jar/aar into your project is to import it as a module.

New -> Module

Right click on the main project and New -> Module

Select Import .JAR/.AAR Package

Import .JAR/.AAR Package

Select the .JAR/.AAR file and put a module name

Select the .JAR/.AAR file

Add the module as a dependency

Add the module as a dependency

Add external library and jar in Android Studio

For adding external library or dependency, you can either edit gradle file for app manually, or you can do it by Project Structure.

  • Edit Gradle File of app

Open gradle file of app, you can add/edit in the dependencies tag,

Sample Image

the first line compile fileTree(dir: 'libs', include: ['*.jar']) indicates that it will include the jar files in this project;

  • in Project Structure

File -> Project Structure, and in the pop-up window, click app in the left list, then click dependencies, in the top-right corner, click + to add new dependency.

Sample Image

Android Studio - Importing external Library/Jar

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.


Related Topics



Leave a reply



Submit