Add jar file android studio

How to Import External JAR Files in Android Studio?

A JAR (Java Archive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file to distribute application software or libraries on the Java platform. In simple words, a JAR file is a file that contains a compressed version of .class files, audio files, image files , or directories. We can imagine .jar files as a zipped file(.zip) that is created by using WinZip software. Even, WinZip software can be used to extract the contents of a .jar. So you can use them for tasks such as lossless data compression, archiving, decompression, and archive unpacking. Let us see how to create a .jar file:

To create a .jar file, use the jar cf command in the following way:

jar cf jarfilename inputfiles

Here, cf represents to create the file. For example, assuming our package pack is available in C:\directory, to convert it into a jar file into the pack.jar, we can give the command as:

Now, the pack.jar file is created. Let’s see the steps involved in importing or adding a JAR file in Android Studio:

Step 1: Download any jar file as your requirement. For example, download the following three mail dependency JAR files for Android Studio. Please refer to this link to download those JAR files. Download all the three JAR files somewhere in the system.

Step 2: Copy these three files and paste it into the libs folder as shown in the below image.

How to find the libs folder in Android Studio?

If you are unable to find the libs folder in Android studio then open your android project in “Project” mode If the project is already opened in the “Android” mode.

Then go to Your Project Name > app > libs and right-click on it and paste the downloaded JAR files. You may also refer to the below image.

Step 3: Once the JAR files are successfully copied to the libs folder we will add them as dependency files. To do so first go to File > Project Structure as shown in the below image.

Step 4: A pop-up screen will arise like in the following image. Now click on the Dependencies then click on the ‘+‘ icon and select Jar Dependency as shown in the below image.

Step 5: After clicking on the Jar Dependency another small pop-up screen will arise like the following image and select the JAR file one by one and click on OK.

Step 6: Now select all jar files and click on the OK button and the Gradle will Start building.

Источник

How To Import Or Add External JAR Files In Android Studio

Basically, JAR is a Java Archives Package file, which comprises several Java files to achieve a specific task. The meta data and Resources(text,images etc.) are also comprised with in a file to distribute it as Library files.

All JAR files are archived file which are built on Zip format. JAR files have the .JAR extension. You can add these JAR files to your libs folder under app folder to provide your project some additional functionality.

Lets see the steps involve in importing or adding a JAR file in Android Studio:

Читайте также:  Android что такое фреймы

How to import or Add External JAR files in Android Studio:

Step 1: Download any JAR file for your Project. For Example: Download three Mail dependency JAR files for Android Studio. Get it from Here

Step 2: Download all the three JAR files in somewhere in your system.

Step 3: Copy these three Files from that folder where you downloaded them, and Paste it in libs folder under app folder of your project.

Important Note: If you are not able to find libs then you must be viewing your project in “Android” view. On the right side of Android tab you will see symbol. Click on it and select Project. This option is available in Android Studio 2.1.

If you are using any older version where you can’t find this, then you simply need to click on Android tab and it will show you a drop down list from that list select project.

Step 5: Now paste jar files here in libs folder

Step 4: Once your JAR files are successfully copied to libs folder and we will add them as dependency files.

Step 5: Click on File > Project Structure >Select app > Dependencies Tab.

Step 6: Click on (+) plus button given on right side and select File Dependency.

Step 7: This will pop up a dialog box for selecting path. Under this open libs folder and add your Jar files one by one.

Steps 8: Once you select all three Jar files then click Ok button and your Gradle will Start building.

Источник

importing jar libraries into android-studio

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

First I tried doing the following:

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

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:

And got an error message: cannot resolve symbol dependencies

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

13 Answers 13

  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:

2.Add .jar to libs folder:

3.Edit app’s build.gradle dependency:

4.Sync project with Gradle files:

  • Finally add .jar files to your application.

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.

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

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

  1. Click plus icon then select File dependency

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

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

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

Module»>

Select Import .JAR/.AAR Package

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

Add the module as a dependency

Running Android Studio 0.4.0 Solved the problem of importing jar by

For those like manual editing Open app/build.gradle

In the project right click

You can follow the screenshots below:

You will see this:

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’

This is how you add jar files from external folders

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

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

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’

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.

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.

Источник

How to add a jar in External Libraries in Android Studio?

I am new to Android Studio.
How can I add a few jar files in the External Libraries below the folder?

16 Answers 16

A late answer, although I thought of giving an in-depth answer to this question. This method is suitable for Android Studio 1.0.0 and above.

STEPS

  1. First switch your folder structure from Android to Project.

  1. Now search for the libs folder inside the app folder.

  1. Once you have pasted the .jar file inside libs folder. Right click on the jar file and at end click on Add as library. This will take care of adding compile files(‘libs/library_name.jar’) in build.gradle [You don’t have to manually enter this in your build file].

Now you can start using the library in your project.

Add your jar file to the folder app/libs . Then right click the jar file and click «add as library».

If there is no libs folder you can create it. Click on the combo box that says «Android», and change it to «Project»

From here, you can right click on «apps» in the directory tree and go to «New» => «Directory»

Put your JAR in app/libs, and in app/build.gradle add in the dependencies section:

Create «libs» folder in app directory copy your jar file in libs folder right click on your jar file in Android Studio and Add As library. Then open build.gradle and add this:

Step 1: Download any JAR file for your Project.

Step 2: Copy .jar file and past in libs folder.

Step 3: Click on File > Project Structure >Select app > Dependencies

Step 4:

Step 5:

Step 6: After click Ok button then we can see the Dependencies add like this way:

In Android Studio version 3.0 or more I have used bellow like :

  1. Create libs to the app directory if not exist folder like
  • Set Project view in upper left corner
  • Go to project
  • Go to app
  • click on apps->New->Directory
  • name the folder libs
  • paste the jar to the libs folder
  • directory will look like bellow image
Читайте также:  Vsco с фильтрами android

In build.gradle add these lines

Example with Parse jar.

Add jars to libs folder from Project view … create lib folder if not exists

Copy all jars there.

Add libs to gradle. in build.gradle file :

For add all jars of lib folder. change Parse-*.jar to *.jar

This is how you can add .jar file in Android Studio 2.1.3.

Copy the .jar file

paste the file in Libs folder and then right click on .jar file and press Add as library

open build.gradle

add lines under dependencies as shown in screenshot

Now press play button and you are done adding .jar file

The GUI based approach would be to add an additional module in your project.

  1. From the File menu select Project Structure and click on the green plus icon on the top left.
  2. The new Module dialog pops
  3. From the phone and tablet application group select the «Import JAR or AAR package» option and click next.
  4. Follow the steps to create a new module that contains your JAR file.
  5. Click on the entry that corresponds to your main project and select the dependencies tab.
  6. Add a dependency to the module that you created in step 4.

One final piece of advice. Make sure that the JAR file you include is build with at most JDK 1.7. Many problems relating to error message «com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)» root straight to this :0.

A simple way to add Jar file Android Studio Steps:

  1. Copy and paste your jar file to libs folder of your project.
  2. Click File from File menu -> Project Structure (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X).
  3. Select Modules at the left panel -> Dependencies tab.
  4. Add. → Project Library → Attach Jar.

If anyone is looking for another solution without actually copying the jar file(s) into the project directory, e.g. when using a jar in multiple projects:

Open build.gradle and add

Note that of course you don’t have to use the myJarFolder variable, I find it useful though. The path can also be relative, e.g. ../../Path/To/My/Jars.
Tested with AndroidStudio 3.0

Update: For Gradle Plugin > 3.0 use implementation instead of compile:

  1. Create libs folder under app folder and copy jar file into it.
  2. Add following line to dependcies in app build.gradle file:

If you dont see option «Add as Library», make sure you extract (unzip) your file so that you have mail.jar and not mail.zip .

Then right click your file, and you can see the option «Add as library».

Please provide the jar file location in build.gradle

To add single jar file

Note: compile is deprecated in latest gradle, hence use implementation instead.

The «official way» to add a jar into your android project as an external library, is to add the jar in dependencies section in build.gradle.

If you’ve done all of the above, and none of the above works, then there are two other possibilities:

  1. If Android Studio or some other IDE doesn’t give red underlined errors in editor but runs in error in the Java Compiler, CHECK YOUR JAR. Your jar may not have a proper Manifest included therefore the compiler doesn’t know what the jar can provide/don’t know it’s package name
  2. Maybe it’s folder structure is messed up. The folder structure doesn’t match with its package name. package a.b.c; should be matching to folder a > folder b > folder c.
  3. The package name has some conflict. (Trust me, this happened to me and it took hours to figure it out.)

However, if you are going with cordova, here are some tips of adding external jars.

«build-extras.gradle» is the better way to manage your gradle file.

Here are the steps to manage extra settings in cordova-based android project:

Источник

Оцените статью