Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing external .jar file to Android project [duplicate]

Tags:

java

android

jar

I have a standard Java project in which I've written code to parse some BBC Radio XML data from the internet. The project is called BBCSchedules and the class I'm interested in is called BBCChannel.

I'm now trying to use the BBCChannel class in an Android application. How should I go about doing this?

I've tried various things, following various bits of advice on the internet, and the place I've got to at the moment is compiling my BBCSchedules project to a .jar file, and importing that to the Android project using the Build Path/Library/Add External Jar option. However, Eclipse still doesn't recognise the BBCChannel class, and won't let me run the application because of this.

I guess I've done something silly wrong, but what is it?

UPDATE: I've tried the steps listed in How can I use external JARs in an Android project? and various other StackOverflow questions I can find that seem to be related to this, but absolutely nothing seems to work. Any other ideas?

UPDATE: The discussions I've had with the author of the first answer below suggest that it is something to do with how I am using Eclipse to attach the .jar file. The project he sent me with the .jar file already attached didn't work. Any other ideas anyone?

like image 429
robintw Avatar asked Apr 22 '10 20:04

robintw


People also ask

Can Android phones run jar files?

You cannot directly run jar files in android as android runs on Dalvik Virtual Machine , whereas to run jar files you will need Java Runtime Environment which has Java Virtual Machine .

Where is the JAR file in Android Studio?

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.


2 Answers

Put the JAR in your project's libs/ directory. Then Build Path -> Library -> Add JAR should allow you to pick the one out of your project. IIRC, this works with Eclipse.

If you decide someday to dump Eclipse, just having your JAR in libs/ is enough -- the command-line build tools will pick it up automatically.

UPDATE: If you have the R17 or newer version of the ADT Eclipse plugin, now you only need to add a libs/ directory and put your JAR in there. It will automatically be added to your build path, much like with command-line builds.

like image 164
CommonsWare Avatar answered Oct 20 '22 19:10

CommonsWare


Here is what I did, and it worked :

  • Right-click on your project
  • Build Path > Add External Archives
  • Select your archive (.jar)

The library.jar will be added to the 'Referenced Libraries' in your project, but not in the folder of your project. I can now run my project with that external library, and I don't get errors, but I don't know if it is the usual way to do it. My archive is not in the 'libs' folder, as everyone. You should try what I did.

like image 35
cleroo Avatar answered Oct 20 '22 19:10

cleroo