Add Jar as Library in Android Studio

I am creating an Application in Android using Android Studio today morning. This application requires Third Party jar file to use in project. So its very easy task to add "jar" in Eclipse by just pasting in "libs" directory and add it as library. But in Android Studio, it requires some more effort to add any library in project. Because if you don't add it nicely, it will give following errors.

Gradle: error: package com.xyz.ClassName does not exist
Gradle: error: cannot find symbol class ClassName


So whenever you want to import any library in your project, try following steps to fix above error.

  1. Put your library file (jar) in "libs" directory.
  2. Right click that jar file and select "Add as library"
  3. Now, open your build.gradle file as shown in below image

it will have following line inside.

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

Ensure that your jar has been listed in this tab. If not, just add it like below.

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

You can add as many library names in above tag you want. Use "," to differentiate it.

Now, build your project. This will let you now third party library import and will not display any type of error in import section.