By Ozzie

2013-05-17 11:41:59 8 Comments

I'm trying to use the new Android Studio but I can't seem to get it working correctly.

I'm using the Gson library to serialize/deserialize JSON-objects. But the library somehow isn't included in the build.

I had created a new project with just a MainActivity. Copied gson-2.2.3.jar in the /libs folder and added it as a library dependancy(right click->Add as library). This includes the jar in android studio so it can be referenced from the source files.

When I try to run the project it cannot compile so I added:

compile files('libs/gson-2.2.3.jar')

to the dependencies in de .gradle file. After that it compiles correctly but when running the application I get a ClassDefNotFoundException.

Does anyone know what I'm doing wrong?


@Fergara 2015-01-03 18:51:05

I made it work by just adding one line to build.gradle:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar']) ----> AS creates this
    implementation ''   ----------> I added this one

Do not forget to click Sync now in the top right corner.

I'm using Android Studio 1.0.1.

@Thufir 2017-07-08 18:03:00

would you elaborate on what the fileTree line does?

@huseyin 2015-01-20 12:51:12

You can do this with two options.

first simple way.

Copy the .jar file to clipboard then add it to libs folder. To see libs folder in the project, choose the project from combobox above the folders.

then right click on the .jar file and click add as a library then choose a module then ok. You can see the .jar file in build.gradle file within dependencies block.

 dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation ''
        implementation project(':okhttp-2.0.0')
        implementation ''

Second way is that: We can add a .jar file to a module by importing this .jar file as a .jar module then add this module to any module we want.

import module ---> choose your .jar file --> than import as a .jar -- enter image description here

Then CTRL+ALT+SHIFT+S --> project sturure -->choose the module you want ato add a jar -->Dependencendies --> Module Dependency. build.gradle of the module will updated automatically. enter image description here

@Aviv Ben Shabat 2015-01-26 11:28:06

Nice. one edit - It's "New module" now (Android Studio 1.0.1). "Import module" sends you to a different wizard..

@Pankaj kumar 2015-06-23 06:36:16

Unlike Eclipse we don't need to download jar and put it in /libs folder. Gradle handles these things we only need to add Gradle dependencies, Gradle downloads it and puts in gradle cache.

We need to add dependencies as:

dependencies {implementation ''}

thats it However we can also download jar & add that as library but the best practice is to add Gradle dependencies.

@Aby Mathew 2015-07-23 05:31:36

Put the .jar files in libs folder of the Android project.

Then add this line of code in the app's gradle file:

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

For Android gradle plugin 3.0 and later, it is better to use this instead:

    implementation fileTree(dir: 'libs', include: ['*.jar'])

@NarenderNishad 2015-03-23 06:11:12

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

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation ''

Do not forget to click "Sync now"

@user2779311 2015-02-01 15:54:03

For newer Android 1.0.2 the following is already there in your build.gradle file

implementation fileTree(include: ['*.jar'], dir: 'libs')

Add the library jar to your libs folder -> right click the library -> click add as a library -> it asks you for the project to add it for -> select your project-> click ok The following line is automatically added to build.gradle

implementation files('libs/android-query.jar')

That did it for me. nothing more was required. i have shown this for android aquery another third party library for android.

@Ranjith Kumar 2016-05-11 11:54:40

In Android Studio 2.1 I follow the this way,

Goto app -> src-> main -> assets folder (If not available create it) -> put your JAR files

enter image description here

In your build.gradle add dependency like this,

implementation files('src/main/assets/jsoup.jar')
implementation files('src/main/assets/org-apache-xmlrpc.jar')
implementation files('src/main/assets/org.apache.commons.httpclient.jar')
implementation files('src/main/assets/ws-commons-util-1.0.2.jar')

Sync now. Now your JAR files ready to use.

@Deni Erdyneev 2016-05-12 07:44:53

Why you not use libs directory?

@Ranjith Kumar 2016-05-12 09:09:11

@ErdeniErdyneev I give another approach for storing jar files. why I am not use assets? any issues in my approach? I am ready to correct mistakes if I am wrong

@parasrish 2015-06-08 10:38:02

  1. Added the libs folder at the level of app.
  2. Added all the jars in this project.
  3. Next, selected all the jars, in the libs folder,
  4. right click on the selected items, and say add library
  5. then you will find the jars expansion option, within the project explorer itself.

I observed CTRL + ALT + SHIFT + S --> project structure --> app-module -->Dependencies" already had an entry as (dir: 'libs', include: '*.jar') under compile-option, initially. And after adding the jar's as per the steps stated above, the build.gradle got the entries for the new added jar's, itself.

@live-love 2017-12-23 18:43:00

With Android Studio 3+:

You should just be able to simply copy the jar file to the libs folder right under the app folder.

... myproject\app\libs\myfile.jar

Then select Project Files from the drop-down on the Projects window, right click on the project, select Synchronize to see the file in Project Files. It will automatically add the dependencies in the gradle file (Module:app).

dependencies {
    implementation files('libs/myfile.jar')

Here is another solution:

Go to the Project Files view (select Project Files from the dropdown).

enter image description here

Select New... Directory, create a folder named libs right under app.

enter image description here

Open up File Explorer, copy and paste your jar file into the libs folder.

In Android Studio, right click on the jar file, and select Add as a Library... from the popup menu.

enter image description here

You should see the file listed in the dependencies list in the gradle file:

dependencies {
    implementation files('libs/myfile.jar')

Open up your java file, and add the import statement there:


@lepoetemaudit 2013-05-18 20:08:54

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]

@Jonah H. 2013-05-18 23:46:04

You are my hero! Ran gradlew.bat clean on both directories in my project with a build.gradle file, and it worked!

@lepoetemaudit 2013-05-19 15:26:13

Glad it sorted it. I just tried adding another jar and had the same issue - definitely need the compile files in build.gradle in there and need to clean with gradlew. Rebuild Project from Android Studio doesn't seem to do the clean properly.

@Collin Klopfenstein 2013-05-20 08:03:59

Solved my problem 100% as well! Thanks David!

@lordmegamax 2013-05-21 14:07:28

I tried this way. In terminal on Windows 7 project builds perfectly but when try Run in Android Studio - have same problem...

@lepoetemaudit 2013-05-22 12:07:44

@lordmegamax Try restarting Android Studio. Certainly when using external Jars in Maven it doesn't seem to pick stuff up properly until restarted. Luckily it loads pretty fast compared with Eclipse!

@František Žiačik 2013-05-25 14:15:14

gradlew clean helped me too. Thanks a lot!

@daveomcd 2013-05-26 14:51:21

gradlew clean didn't solve the problem alone for me, I had to also do as @Ozzie suggested and add the .jar to my build.gradle located in MyProject/MyProject-MyProject/build.gradle. Inside this file mine now looks like dependencies { compile files('libs/android-support-v4.jar') compile files('libs/myextlib.jar')} Thanks!

@Ozzie 2013-05-26 22:01:44

Maybe @DavidJeffrey could edit the answer so it includes that step.

@Mattias Nordqvist 2013-05-26 22:17:20

deleting all the folder in the build folder manually aslo works.

@J c 2013-05-27 23:50:24

If you don't want to have to explicitly add a line for each jar file: compile fileTree(dir: 'libs', include: '*.jar')

@Joe 2013-07-17 16:12:10

I would recommend taking advantage of gradle's transient dependency management and download gson from mavenCentral() (if requiring an internet connection is acceptable. Simply add repositories { mavenCentral() } and replace your compile files(...) line with compile '' and gradle will automatically download and manage the jar file for you. This allows SCM to track only source code files and not libraries.

@mSafdel 2013-12-03 15:13:44

@TheAppChaps i do all steps that you saied but i have a compile error: "Gradle: A problem occurred evaluating root project 'Test'. > Could not find method compile() for arguments [file collection] on root project 'Test'.". when i use "gradlew clean" i get an error: "FAILURE: Build failed with an exception. * What wen A problem occurred evaluating root project 'Test'. > Could not find method compile() for arguments [directory 'libs'] on root project 'Test'."

@mSafdel 2013-12-04 07:52:39

Solved: I should added dependencies to build.gradle in the App folder not build.gradle in the root!

@Yster 2014-08-10 15:42:07

I don't get the Add as Library option in step 2.

@Brian Marick 2015-01-16 17:54:45

In Android Studio 1.0.2, steps 2 and 3 are not needed because this line is already in the default build.gradle: compile fileTree(dir: 'libs', include: ['*.jar'])

@justinkoh 2015-02-25 10:40:18

Can't find libs folder, or where to add libs folder? Check this link. Why made it this way...

@Ajay Pandya 2015-03-14 06:33:56

@Sven 2015-06-16 10:57:41

As soon as I do the right-click "Add Library", the compile command is automatically added as a dependency to the app's build.gradle, not the root's build.gradle (where it actually says in a comment don't put lib dependencies here :-)). Build works without further ado.

@CodeToLife 2016-07-21 19:49:22

@Yster Yes, A little comment about the point 2.Right click it and hit 'Add as library' You can find it in Project window --> in Project subwindow (not ' Android' or 'project files') --> in the 'app' node. right click and look at last option 'Add as library'

@Nilesh Patel 2017-05-04 11:26:43

Add below line to add jar as library into project. Open build.gradle file of your application module. then paste this line... compile fileTree(dir: 'libs', include: '*.jar')

@Vinayak Bevinakatti 2016-08-26 18:48:41

Easy steps to add external library in Android Studio

  1. If you are in Android View in project explorer, change it to Project view as below

enter image description here

  1. Right click the desired module where you would like to add the external library, then select New > Directroy and name it as 'libs'
  2. Now copy the blah_blah.jar into the 'libs' folder
  3. Right click the blah_blah.jar, Then select 'Add as Library..'. This will automatically add and entry in build.gradle as compile files('libs/blah_blah.jar') and sync the gradle. And you are done

Please Note : If you are using 3rd party libraries then it is better to use dependencies where Gradle script automatically downloads the JAR and the dependency JAR when gradle script run.

Ex : compile ''

Read more about Gradle Dependency Mangement

@0xF 2017-01-15 18:50:18

I have read all the answers here and they all seem to cover old versions of Android Studio!

With a project created with Android Studio 2.2.3 I just needed to create a libs directory under app and place my jar there. I did that with my file manager, no need to click or edit anything in Android Studio.

Why it works? Open Build / Edit Libraries and Dependencies and you will see:

{include=[*.jar], dir=libs}

@Helzgate 2015-02-21 04:20:15

All these solutions are outdated. It's really easy now in Android Studio:

File > New Module...

The next screen looks weird, like you are selecting some widget or something but keep it on the first picture and below scroll and find "Import JAR or .AAR Package"

Then take Project Structure from File menu.Select app from the opened window then select dependencies ,then press green plus button ,select module dependency then select module you imported then press OK

@Gary Drocella 2015-05-04 02:31:24

I think using maven would still be the better solution especially for large scale projects, that is, provided by saneryee.

@Daniele Testa 2015-10-11 08:10:08

Even this seems to be outdated. Simply put your .jar file in your "libs" directory and it will auto-magically be compiled to your project. There is already a default rule setup in Android Studio 1.4 to include all *.jar files from the "libs" dir.

@A.B. 2016-07-23 13:32:25

I found Dependency Manager of Android Studio quite handy and powerful for managing 3rd party dependencies (like gson mentioned here). Providing step by step guide which worked for me (NOTE: These steps are tested for Android Studio 1.6 and onward versions on Windows platform).

Step-1: Goto "Build > Edit Libraries and Dependencies..." it would open up the dialog "Project Structure"

enter image description here

Step-2: Select "app" and then select "Dependencies" tab. Then select "Add > 1 Library dependency"

enter image description here

Step-3: "Choose Library Dependency" dialog would be shown, specify "gson" in search and press the "search button"

enter image description here

Step-4: The desired dependency would be shown in search list, select (this is the latest version at the time when I wrote the answer), press OK

enter image description here

Press OK on "Project Structure" dialog. Gradle would update your build scripts accordingly.

enter image description here

Hope this would help :)

@A.B. 2016-08-23 12:02:22

I have found some issue with this approach, have been working on app which uses commons-io-2.5. When uploaded the app to Google Developer Console, it shows 0 devices supported for the signed apk. After exhaustive investigation, I figured out that I have to place the jar file under app/libs/commons-io-2.5.jar and update dependencies in gralde like this 'compile files('libs/commons-io-2.5.jar')', not sure what I have been doing wrong here and why documentation doesn't cover this.

@imsrgadich 2017-08-10 05:19:46

This is the way it works. All other answers didn't work. Android Studio 2.3.3

@Elshan 2016-06-19 09:22:16

Ive done above 3 steps and its work charm for me.

(I am using Android Studio 2.1.2)

enter image description here

Step 1

  • Add your jar package name (as an example compile 'com.squareup.okhttp3:okhttp:3.3.1') into gradle build script under build.gradle(Module:app).

enter image description here

Step 2: Right click on app folder -> New -> Module

enter image description here

Step 3: Click Import JAR/.AAR Package Then browse your package. as an example: OkHttp.jar

enter image description here

@ZenBalance 2013-08-19 19:50:29

Here are the instructions for adding a local jar file as a library to a module:

  1. Create a 'libs' folder in the top level of the module directory (the same directory that contains the 'src' directory)

  2. In the build.gradle file add the following so that your dependencies closure has:

    dependencies {
        // ... other dependencies
        compile files('libs/<your jar's name here>')
  3. Android Studio should have already setup a gradlew wrapper. From the command line, navigate to the top level of your project (the directory that has a gradlew file).

    Run ./gradlew assemble. This should compile the project with the library. You may need to fix errors in your build.gradle file as necessary.

  4. In order to have Android Studio recognize the local jar files as libraries for support while coding in the IDE, you need to take a few more steps:

    4.1. Right click on the module in the left hand panel and choose Open Library Settings.

    4.2. On the left panel of the dialog, choose Libraries.

    4.3. Click the + sign above the panel second from the left -> Java


    4.4. Select your local jar and add it to the project.

  5. You may need to run the above ./gradlew command one more time

@Phil Freihofner 2015-05-01 18:45:28

This worked for me. I have Android Studio version 1.2 Build 141.1890965, on Ubuntu. Why do you say "as of Android Studio 2.5"? Is it a different version on Windows/Mac? (1a) Finding this was hard. Combo box is at top left of project structure area. Need to select "Project" as opposed to the "Android" and expand the "app". (1b) No "add" option came up, but "Paste" worked. (2) I had a line "compile fileTree(dir: 'libs', include: ['*.jar'])", but still had to add your explicit single line. (4, 5) not needed once (3) done successfully. Thanks!

@The incredible Jan 2017-09-19 07:18:50

"the left hand panel"? Do you mean the project panel? Could be anywhere... With Android Studio 2.3.3 I can't find Library Settings but adding the jar to Dependencies in Module Settings seems to be the same...

@Azkario Rizky 2017-09-23 14:38:39

for mac, if you haven't gradle: $ brew install gradle if you get "./gradlew: Permission denied" , you need to run $ chmod 755 gradlew Then you can run $ ./gradlew assemble

@IntelliJ Amiya 2015-12-02 09:10:36

Step 1 : Now under your app folder you should see libs, if you don't see it, then create it .

Step 2 : Drag & Drop the .jar file here, you may be get a prompt "This file does not belong to the project", just click OK Button .

Step 3 : Now you should see the jar file under libs folder, right click on the jar file and select "Add as library", Click OK for prompt "Create Library"

Step 4 : Now this jar has been added.

enter image description here

@Sandeep 2014-04-07 09:58:10

1. Put the jar (in my case, gson-2.2.4.jar) into the libs folder.

2. Ensure that compile files (libs/gson-2.2.4.jar) is in your build.gradle file.

3. Now Click on the "Sync Project with Gradle files"(Left to AVD manager Button on the topbar).

After I did the above three, it started working fine.

@Ganesh Pandey 2015-08-19 05:50:57

In the project right click

-> new -> module
-> import jar/AAR package
-> import select the jar file to import
-> click ok -> done

Follow the screenshots below:


Step 1


enter image description here


enter image description here

You will see this:

enter image description here

@Ganesh Pandey 2016-09-20 12:34:57

@CamConnor i also think so.. Other answers are outdated

@Alessandro 2016-11-23 12:50:05

This's THE answer

@Dave 2016-12-18 00:44:15

But it is not recognized as libraray. For example, andriod studio editor: try to import a package in the imported jar, not recognized.

@Dave 2016-12-18 00:47:24

Could not find a way to remove the imported module in this way.

@Eido95 2017-06-13 17:16:24

Why should I use this method of adding external jar and not the other answers of "copy-jar-to-libs" folder method?

@CGR 2017-06-14 15:41:39

Works like a charm!

@Jukurrpa 2013-05-17 13:13:50

IIRC, simply using "Add as library" isn't enough for it to compile with the project.

Check Intellij's help about adding libraries to a project

The part that should interest you the most is this:

(In File > Project Structure) Open the module settings and select the Dependencies tab.

On the Dependencies tab, click add and select Library.

In the Choose Libraries dialog, select one or more libraries and click Add Selected.

If the library doesn't show up in the dialog, add it in the Libraries settings, right below Modules.

You shouldn't need to add compile files() anymore, and the library should be properly added to your project.

@Ozzie 2013-05-17 14:35:45

Add as library already add's the jar in the same way. So i think the result is the same. I'll try it later.

@Jonah H. 2013-05-18 10:21:48

Add as library does do this successfully - I'm in the same boat as you Ozzie - EXACT same problem.

@Jukurrpa 2013-05-18 12:14:01

Alright, I thought Add as library only added it to the library list and did not add it as to module dependencies.

@Matt Holgate 2013-10-22 13:25:04

When I tried this, I found it did add the compile files() lines to my build.gradle file, but it seemed to use absolute paths to the libraries (/libs/foo.jar). I change these to relative paths (libs/foo.jar), which fixed the problem for me.

@sansalk 2014-10-02 09:06:58

  1. Download Library file from website
  2. Copy from windows explore
  3. Paste to lib folder from Project Explorer
  4. Ctrl+Alt+Shift+S open Project Structure
  5. Select Dependencies Tab, add the file by using +
  6. Tool bar Sync project with gradle file by using button

That solved my problem. Try, if anyone want more details let me know.

@mbo 2015-07-13 11:33:02

In android Studio 1.1.0 . I solved this question by following steps:

1: Put jar file into libs directory. (in Finder)

2: Open module settings , go to Dependencies ,at left-bottom corner there is a plus button. Click plus button then choose "File Dependency" .Here you can see you jar file. Select it and it's resolved.

@idris yıldız 2015-07-13 06:43:34

1) create an 'your_libs' folder inside the Project/app/src folder.

2) Copy your jar file into this 'your_libs' folder

3) In Android Studio, go to File -> Project Structure -> Dependencies -> Add -> File Dependency and navigate to your jar file, which should be under 'src/your_libs'

3) Select your jar file and click 'Ok'

and then you can see on your build.gradle like this : compile files('src/your_libs/your.jar')

@vuhung3990 2015-01-30 08:51:26

menu File -> project struct -> module select "app" -> dependencies tab -> + button 
-> File dependency -> PATH/myfile.jar

@Gowtham Chandrasekaran 2014-12-30 13:41:05

Create a folder libs. Add your .jar file. Right click on it and you will find add jar as dependency. Click on it. Its all you need to do. You can find the dependencies added to your build.gradle file.

@Christina 2014-12-27 19:22:16

Like many before pointed out you shall add

compile files('libs/gson-2.2.3.jar') 

to your build.gradle file.

However I have a project in Android Studio that was migrated from Eclipse and in this case the "libs" folder is named "lib" so for me removing the "s" solved the problem.

@olyv 2014-12-03 16:07:19

I don' know why but my Android Studio 0.8.14 goes crazy when I try to implement these solutions using Gradle. I admit my poor knowledge of this great build tool but what does Studio mutilate my project for? I manage to get it working this way: put android-support-v13.jar into 'libs' directory, then F4 on my project and add File dependency where I pointed android-support-v13.jar.

@Dirk 2014-11-03 00:51:25

In my case the added library missed some dependencies, but unfortunately Android Studio (0.8.14) has kept this as a secret.

There was no need to manually configure anything! I just added the missing libraries and used the default dependency configuration in the app build.gradle file, like this

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

@diegosasw 2014-09-25 04:00:14

My answer is basically gathering some of the right but incomplete answers provided above.

  1. Open build.gradle
  2. Add the following:

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

    This will allow support for two different ways of adding dependencies. The compile fileTree(dir: 'libs', include: ['*.jar']) (as @Binod mentioned) tells the compiler to look under the folder libs for ANY jar. It is a good practice to create such a folder 'libs' which will contain the jar packages that our application needs to use.

But this will also allow support for Maven dependency. The compile '' (as mentioned by @saneryee) it is another recommended way to add dependencies that are in a central remote repository and not in our /libs "local repository". It is basically telling gradle to look for that version of that package and it's telling the compiler to consider it when compiling the project (having it in the classpath)

PS: I use both

@saneryee 2014-09-15 23:25:02

In Android Stuido, I like use Gradle to manage Gson lib.

Add below dependency in your build.gradle file.

repositories {mavenCentral()}

dependencies {compile ''}

Everything is OK.

You can also see this post. The best way to integrate third party library in Android studio

@Gary Drocella 2015-05-04 02:29:48

This method is the best because it's using maven.

@Kitalda 2015-06-16 10:51:24

Just remember to update the version number to the latest :) As of now that is 2.3.1, but check link

Related Questions

Sponsored Content

105 Answered Questions

[SOLVED] Close/hide android soft keyboard

48 Answered Questions

[SOLVED] Automatically accept all SDK licences

20 Answered Questions

51 Answered Questions

[SOLVED] Is there a unique Android device ID?

52 Answered Questions

22 Answered Questions

[SOLVED] What is Gradle in Android Studio?

11 Answered Questions

[SOLVED] Proper use cases for Android UserManager.isUserAGoat()?

11 Answered Questions

[SOLVED] Dealing with "Xerces hell" in Java/Maven?

Sponsored Content