329

I have created an Android project and added an external JAR (hessian-4.0.1.jar) to my project. I then added the JAR to the build path and checked it off in Order and Export.

Order and Export is ignored it seems, and all classes from the external JAR are missing at runtime.

Is there a trick to properly include the needed classes from an external JAR when building an Android application using the Eclipse plugin? I do not want to use ant or Maven.

AndroidSmoker74
  • 288
  • 4
  • 19
  • 1
    as you already found out: this is caused by dependencies in hessian-4.0.1.jar. If you want to use Hessian with Android, use Hessdroid (http://code.google.com/p/hessdroid/). Not very active, but works fine for me even on Android 4.0. – Ice09 Jan 12 '12 at 09:48
  • 1
    See also ["NoClassDefFoundError for code in an Java library on Android"](http://stackoverflow.com/questions/8678630/) for a common problem when using external jars. [Simple solution](http://stackoverflow.com/a/20497308/194894). – Flow Dec 10 '13 at 15:16

12 Answers12

444

For Eclipse

A good way to add external JARs to your Android project or any Java project is:

  1. Create a folder called libs in your project's root folder
  2. Copy your JAR files to the libs folder
  3. Now right click on the Jar file and then select Build Path > Add to Build Path, which will create a folder called 'Referenced Libraries' within your project

    By doing this, you will not lose your libraries that are being referenced on your hard drive whenever you transfer your project to another computer.

For Android Studio

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

Missing Image

  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 transitive dependencies where Gradle script automatically downloads the JAR and the dependency JAR when gradle script run.

Ex : compile 'com.google.android.gms:play-services-ads:9.4.0'

Read more about Gradle Dependency Mangement

Abhinav Saxena
  • 1,904
  • 2
  • 22
  • 50
Vinayak Bevinakatti
  • 38,839
  • 25
  • 103
  • 135
  • 49
    Note that when using recent Eclipse/ADT versions the external jars have to be in a folder called `libs` (instead of `lib`) – THelper May 14 '12 at 11:24
  • 4
    @THelper - Thanks! You are correct, the folder must be named "libs" and not "lib". I was banging my head over this problem. – Camille Sévigny Jul 06 '12 at 14:21
  • 7
    In recent versions of the SDK, step 3 (adding the JAR manually to the Build Path) is unnecessary. But if you have an IDE, like Eclipse open at the time you drop the file in, you will have to do a project refresh (F5 and/or context menu Refresh in the Package Explorer). – Jon Adams Nov 29 '12 at 18:26
  • Thanks Vinayak, was a big help. I realised all this time i've been using references from other projects. For example, for `httpmimi-4.1.2.jar` I've always been adding via `Right click new Project > Properties > Java Build Path > Add JARS ... `. I then navigate to a previous project which uses the jar. I now realise, only creates a _link_ to a previous project JARS and, as you say will potentially leave JARS behind if I move or backup the project. Thanks again – wired00 Dec 05 '12 at 03:39
  • 4
    How is it done from the command line? What configuration file must I edit, and how? – Kristian Spangsege Dec 06 '12 at 14:16
  • 1
    In order to make it works I had to "refresh" the libs folder in eclipse. – Alessandro Jacopson Feb 14 '13 at 20:57
  • 13
    This approach has been out of date since early 2012. Just add the JAR to `libs/`. Do not manually modify the build path. – CommonsWare May 19 '13 at 03:22
  • I had to do this because my jar file in the lib folder whas not added automatically to buildpath. So I made "right click > Build Path > Add to Build Path" and the jar was added in private libraries . I then deleted the extra jar imported in buildpath outside of private libraries. – djleop Sep 19 '13 at 15:35
  • After pasting it to libs please do not forget to refresh the current project in eclipse :) – Sagiruddin Mondal Jul 25 '14 at 10:18
127

Yes, you can use it. Here is how:

  1. Your Project -> right click -> Import -> File System -> yourjar.jar
  2. Your Project -> right click -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar

This video might be useful in case you are having some issues.

Nick Betcher
  • 1,871
  • 4
  • 18
  • 25
MannyNS
  • 4,663
  • 2
  • 19
  • 16
5

I'm currently using SDK 20.0.3 and none of the previous solutions worked for me.

The reason that hessdroid works where hess failed is because the two jar files contain java that is compiled for different virtual machines. The byte code created by the Java compiler is not guaranteed to run on the Dalvik virtual machine. The byte code created by the Android compiler is not guaranteed to run on the Java virtual machine.

In my case I had access to the source code and was able to create an Android jar file for it using the method that I described here: https://stackoverflow.com/a/13144382/545064

Community
  • 1
  • 1
Danny Remington - OMS
  • 3,924
  • 3
  • 28
  • 19
5

I know the OP ends his question with reference to the Eclipse plugin, but I arrived here with a search that didn't specify Eclipse. So here goes for Android Studio:

  1. Add jar file to libs directory (such as copy/paste)
  2. Right-Click on jar file and select "Add as Library..."
  3. click "Ok" on next dialog or renamed if you choose to.

That's it!

shanabus
  • 12,601
  • 6
  • 47
  • 75
4

Turns out I have not looked good enough at my stack trace, the problem is not that the external JAR is not included.

The problem is that Android platform is missing javax.naming.* and many other packages that the external JAR has dependencies too.

Adding external JAR files, and setting Order and Export in Eclipse works as expected with Android projects.

3

Goto Current Project

RightClick->Properties->Java Build Path->Add Jar Files into Libraries -> Click OK

Then it is added into the Referenced Libraries File in your Current Project .

Gapchoos
  • 1,412
  • 4
  • 18
  • 39
kalyan G
  • 31
  • 1
1

If using Android Studio, do the following (I've copied and modified @Vinayak Bs answer):

  1. Select the Project view in the Project sideview (instead of Packages or Android)
  2. Create a folder called libs in your project's root folder
  3. Copy your JAR files to the libs folder
  4. The sideview will be updated and the JAR files will show up in your project
  5. Now right click on each JAR file you want to import and then select "Add as Library...", which will include it in your project
  6. After that, all you need to do is reference the new classes in your code, eg. import javax.mail.*
Ramya Rao
  • 86
  • 7
kumaheiyama
  • 594
  • 1
  • 12
  • 27
1

in android studio if using gradle

add this to build.gradle

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

and add the jar file to libs folder

g_vk
  • 106
  • 7
1

If you are using gradle build system, follow these steps:

  1. put jar files inside respective libs folder of your android app. You will generally find it at Project > app > libs. If libs folder is missing, create one.

  2. add this to your build.gradle file your app. (Not to your Project's build.gradle)

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

    This will include all your jar files available in libs folder.

If don't want to include all jar files, then you can add it individually.

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

Rohit Arya
  • 6,458
  • 1
  • 21
  • 37
1

Android's Java API does not support javax.naming.* and many other javax.* stuff. You need to include the dependencies as separate jars.

Rodrigo
  • 11
  • 1
0

create a folder (like lib) inside your project, copy your jar to that folder. now go to configure build path from right click on project, there in build path select

'add jar' browse to the folder you created and pick the jar.

Dariusz
  • 19,750
  • 7
  • 65
  • 104
sharma_kunal
  • 2,018
  • 1
  • 26
  • 27
0

Copying the .jar file into the Android project's folder isn't always possible.
Especially if it's an output of another project in your workspace, and it keeps getting updated.

To solve this you'll have to add the jar as a linked file to your project, instead of importing it (which will copy it locally).

In the UI choose:

Project -> Import -> File System -> yourjar.jar -> (Options area) Advanced -> Create link in workspace.

The link is save in the .project file:

<linkedResources>
    <link>
        <name>yourjar.jar</name>
        <type>1</type>
        <locationURI>PARENT-5-PROJECT_LOC/bin/android_ndk10d_armeabi-v7a/yourjar.jar</locationURI>
    </link>
</linkedResources>

PARENT-5-PROJECT_LOC means relative to the project file, 5 directories up (../../../../../).

Then add it to the libraries:
Project -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar

In the same window choose the Order and Export tab and mark your jar so it will be added to the apk.

Yochai Timmer
  • 44,746
  • 21
  • 135
  • 179