[android] importing jar libraries into android-studio

android-studio 0.2.7
Fedora 18

Hello,

I am trying to add the jtwitter jar to my project.

First I tried doing the following:

1) Drag the jtwitter.jar into the root directory of my project explorer, see picture
2) File | project structure
3) Modules | yamba-yamba | dependencies
4) Click the plus sign | jars or directories | and navigate to jtwitter jar | click ok

project explorer

When I import the jar file I get the following error:

import winterwell.jtwitter.Twitter;

Cannot resolve symbol winterwell
Gradle: error: package winterwell.jtwitter does not exist

I researched and found that android-studio has some issues and that you have to edit the build.gradle file yourself.

So I tried adding this to my build.gradle file:

dependencies {
    compile files('libs/jtwitter.jar')

build.gradle

And got an error message: cannot resolve symbol dependencies

Another question, where would the libs folder be. Does it mean the External Libraries?

Any help will be most helpfull,

This question is related to android android-studio

The answer is


This is how you add jar files from external folders

1) Click on File and there you click on New and New Module

2) New Window appears ,,There you have to choose the Import .JAR/.AAR package .

3) Click on the path option at the top right corner of the window ...And give the whole path of the JAR file .

4)click on finish.

Now you have added the Jar file and You need to add it in the dependency for your application project

1)Right click on app folder and there you have to choose Open Module Settings or F4

2)Click on dependency at the top right corner of the current window .

3)Click on '+' symbol and choose 'Module Dependency' and It will show you the existed JAR files which you have included in your project ...

Choose the one you want and click 'OK/Finish'

Thank you


There are three standard approaches for importing a JAR file into Android studio. The first one is traditional way, the second one is standard way, and the last one is remote library. I explained these approaches step by step with screenshots in this link:

https://stackoverflow.com/a/35369267/5475941.

I hope it helps.


In the project right click

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

You can follow the screenshots below:

1:

Step 1

2:

enter image description here

3:

enter image description here

You will see this:

enter image description here


If the code for your jar library is on GitHub then importing into Android Studio is easy with JitPack.

Your will just need to add the repository to build.gradle:

allprojects{
 repositories {
    jcenter()
    maven { url "https://jitpack.io" }
 }
}

and then the library's GitHub repository as a dependency:

dependencies {
    // ...
    compile 'com.github.YourUsername:LibraryRepo:ReleaseTag'
}

JitPack acts as a maven repository and can be used like Maven Central. The nice thing is that you don't have to upload the jar manually. Behind the scenes JitPack will check out the code from GitHub and compile it. Therefore it works only if the repo has a build file in it (build.gradle).

There is also a guide on how to prepare an Android project.


I also faced same obstacle but not able to find out solution from given answers. Might be it's happening due to project path which is having special characters & space etc... So please try to add this line in your build.gradle.

compile files('../app/libs/jtwitter.jar')// pass your .jar file name

".." (Double dot) will find your root directory of your project.


Avoid redundancy. If you have your jars under /libs in your app build.gradle by default you will have

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

that is enough to add all the jars you have under /libs

this is not necessary

//    compile files('libs/activation.jar')
//    compile files('libs/additional.jar')
//    compile files('libs/mail.jar')

Android Studio 1.0 makes it easier to add a .jar file library to a project. Go to File>Project Structure and then Click on Dependencies. Over there you can add .jar files from your computer to the project. You can also search for libraries from maven.


Try this...

  1. Create libs folder under the application folder.
  2. Add .jar files to libs folder.
  3. Then add .jar files to app's build.gradle dependency.
  4. Finally Sync project with Gradle files.

1.Create libs folder:

enter image description here

2.Add .jar to libs folder:

enter image description here

3.Edit app's build.gradle dependency:

  • Open app/build.gradle

enter image description here

4.Sync project with Gradle files:

  • Finally add .jar files to your application.

enter image description here

UPDATE:

Here I'm going to import org.eclipse.paho.client.mqttv3.jar file to our app module.

  1. Copy your jar file and paste it in directory called libs.

open project structure

  1. Press Ctrl + Alt + Shift + s or just click project structure icon on the toolbar.

project structure

  1. Then select your module to import .jar file, then select dependencies tab.

add jar

  1. Click plus icon then select File dependency

File dependency

  1. Select .jar file path, click OK to build gradle.

jar path

  1. Finally we're imported .jar file to our module.

imported jar file

Happy coding...


Running Android Studio 0.4.0 Solved the problem of importing jar by

Project Structure > Modules > Dependencies > Add Files
Browse to the location of jar file and select it

For those like manual editing Open app/build.gradle

dependencies {
    compile files('src/main/libs/xxx.jar')
}

Android Studio 1.0.1 doesn't make it any clearer, but it does make it somehow easier. Here's what worked for me:

1) Using explorer, create an 'external_libs' folder (any other name is fine) inside the Project/app/src folder, where 'Project' is the name of your project

2) Copy your jar file into this 'external_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/external_libs'

3) Select your jar file and click 'Ok'

Now, check your build.gradle (Module.app) script, where you'll see the jar already added under 'dependencies'


I see so many complicated answer.

All this confused me while I was adding my Aquery jar file in the new version of Android Studio.

This is what I did :

Copy pasted the jar file in the libs folder which is visible under Project view.

And in the build.gradle file just added this line : compile files('libs/android-query.jar')

PS : Once downloading the jar file please change its name. I changed the name to android-query.jar


This is the way I just did on Android Studio version 1.0.2

  • I have created a folder libs in [your project dir]\app\src
  • I have copied the jtwitter.jar (or the yambaclientlib.jar) into the [your project dir]\app\src\libs directory
  • The following the menu path: File -> Project Structure -> Dependencies -> Add -> File Dependency, Android Studio opens a dialog box where you can drag&drop the jar library. Then I clicked the OK button.

At this point Gradle will rebuild the project importing the library and resolving the dependencies.