Java – How to add .jar files in Android Studio

How to add .jar files in Android Studio… here is a solution to the problem.

How to add .jar files in Android Studio

I’ve moved from Eclipse to Android Studio for Android programming.
I’m having trouble importing .jar files in Android Studio.
I can’t seem to import any files from my jsoup package.

Error:(6, 17) error: package org.jsoup does not exist
Error:(7, 23) error: package org.jsoup.nodes does not exist

Solution

Add the following to the build gradle file in the dependencies section:

compile fileTree(dir: 'libs', include: '*.jar')

The above assumes that the library is located in the libs folder of the project.

Alternatively, you can add a dependency like this by telling Gradle a separate jar file:

compile files('libs/yourlibrary.jar')

After adding this line, you should sync your project again, and Android Studio should now allow you to import these classes from your library.

Update: The jsoup library is actually available in the mavenCentral repository. Therefore, a good way to include this dependency is:

 compile 'org.jsoup:jsoup:1.8.2'

Or just do it to always have the latest version:

 compile 'org.jsoup:jsoup:+'

Note: If you do, you need to add the mavenCentral repository to the build level:

 repositories {
     mavenCentral()
 }

Related Problems and Solutions