Adding a library/JAR to an Eclipse Android project
Now for the missing class problem.
I'm an Eclipse Java EE developer and have been in the habit for many years of adding third-party libraries via the "User Library" mechanism in Build Path. Of course, there are at least 3 ways to add a third-party library, the one I use is the most elegant, in my humble opinion.
This will not work, however, for Android, whose Dalvik "JVM" cannot handle an ordinary Java-compiled class, but must have it converted to a special format. This does not happen when you add a library in the way I'm wont to do it.
Instead, follow the (widely available) instructions for importing the third-party library, then adding it using Build Path (which makes it known to Eclipse for compilation purposes). Here is the step-by-step:
- Download the library to your host
development system. - Create a new folder, libs, in
your Eclipse/Android project. - Right-click libs and choose
Import -> General -> File System,
then Next, Browse in the filesystem
to find the library's parent
directory (i.e.: where you
downloaded it to). - Click OK, then click the
directory name (not the checkbox) in
the left pane, then check the
relevant JAR in the right pane. This
puts the library into your project
(physically). - Right-click on your project,
choose Build Path -> Configure Build
Path, then click the Libraries tab,
then Add JARs..., navigate to your
new JAR in the libs directory and
add it. (This, incidentally, is the moment at which your new JAR is converted for use on Android.)
NOTE
Step 5 may not be needed, if the lib is already included in your build path. Just ensure that its existence first before adding it.
What you've done here accomplishes two things:
- Includes a Dalvik-converted JAR
in your Android project. - Makes Java definitions available
to Eclipse in order to find the
third-party classes when developing (that is, compiling)
your project's source code.
how to include jar files in my eclipse project
add the jar to your libs folder in your project.
open eclipse go to package explorer
right click on your project and select project properties
click on java Build path in the bar on the left hand side of the popup
select the libraries tab
select the "Add External Jar File" and navigate to your JAR file
best of luck =)
Create jar for android project in eclipse
In Eclipse, select project, right click on the project, Select Export, From that select Jar. Then follow the simple wizard and give the destination where you want to save your jar, and finish. You can find your jar there.
Eclipse IDE- Add jar? Add External jar? Add Library?
What is an External JAR?
- External as the name suggest is a Jar file that is not built by the currently developing code. It is a utility Jar file that is added externally for extra features. It could be an API Jar file provided from the external site, just as in your case the 3 Jar files will be going to call External Jar Files.
To add an External Jar file in Eclipse, follow these steps: Right Click on Project -> Select Build Path -> Select Configure Build Path -> Library Tab in the following Dialog box.
What is a Library?
A Library is a in-built Jar file from the JDK Directory or any other software e.g. J2ME-Polish as you can see in the Image above. Normally these Library files are developed by a manufacturer.
What's the difference between adding JAR, Adding External JAR, and Adding Library?
- Adding Jar File: When you have more than one project in Eclipse and you add another project's Jar file, that is called adding Jar File. There is a first Button in the Photo above, it only allows you to add Jar file from the other Project.
- Adding External Jar: When you add any API Jar file provided by the site or another manufacturer. In your case the 3 Jar files are External Jar Files. You can add then by using second Button "Add External JARs".
Adding A Library: Clicking on the 4th Button will open a dialog box as below.
You can add different libraries installed on your system. You can also add your own Library, by selecting "User Library".
Is it possible to embed (combine) an external jar file to my library project in eclipse?
So you want to combine the library and the external jar into one library that your project can link to? That is not possible. You cannot just combine/link two binaries (JARs) and expect them to work as one single library. Your libraries are dynamically linked and not statically, and each library is loaded in a different address space. If you want them to be as one, you need to have the source of the two libraries (your existing library and external jar) and compile them into one (that is, static linking).
Update:
Thanks Sergiu for the insight, that is quite true. The above answer was based on the precinct that a library is a binary loaded by the JRE at runtime at some memory location, and that it is formatted binary with functions/variables loaded at locations calculated by the environment - A General CS theory.
Based on your comments, I googled how to combine two JARs, and your method seems right.
Here's another SO answer: Combine two JARs
Check out the 2nd answer, it also clarifies about the Manifest.mf file.
Adding a JAR to an Eclipse Java library
In Eclipse Ganymede (3.4.0):
- Select the library and click "Edit" (left side of the window)
- Click "User Libraries"
- Select the library again and click "Add JARs"
Related Topics
How to Send an Object from One Android Activity to Another Using Intents
Android List View Inside a Scroll View
How to Determine When Fragment Becomes Visible in Viewpager
Saving and Reading Bitmaps/Images from Internal Memory in Android
Java Finished With Non-Zero Exit Value 2 - Android Gradle
Android: Difference Between Parcelable and Serializable
"Debug Certificate Expired" Error in Eclipse Android Plugins
Is the Way the Firebase Database Quickstart Handles Counts Secure
Different Values Folders in Android
How to Check Visibility of Software Keyboard in Android
How to Get the Source Code from an Apk File
How to Implement Onbackpressed() in Fragments
How to Pick an Image from Gallery (Sd Card) For My App
Difference Between "@Id/" and "@+Id/" in Android
How to Hide the Title Bar For an Activity in Xml With Existing Custom Theme
Ioexception: Read Failed, Socket Might Closed - Bluetooth on Android 4.3