tags:

views:

797

answers:

1

I've read various answers to similar questions on StackOverflow, so I apologise if I'm repeating another question, but I can't seem to get this to work.

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, Eclispe 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?

Cheers,

Robin

UPDATE: I've tried the steps listed in http://stackoverflow.com/questions/1334802/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?

+4  A: 

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.

CommonsWare
Thanks. I've added the .jar to the libs/ directory and then selected it in the Build Path in Eclipse, but Eclipse still doesn't seem to recognise the BBCChannel class, and therefore won't let me compile and run my application. Any other ideas?
robintw
Are you sure your classes are public?
CommonsWare
Yes, my class is public, and it works fine from another simple command-line test program. I can't work out why it's not working! I can upload the files somewhere if you want...
robintw
Here is a project demonstrating incorporating a JAR into an Android application: http://github.com/commonsguy/cw-android/tree/master/Java/AndShell/
CommonsWare
Thanks for your help. Interestingly, when I load that example project into Eclipse it complains about not being able to find the bsh module or the Interpreter class. Obviously it works for you, so it must be something to do with my configuration. Any ideas?
robintw
I don't use Eclipse, so your difficulty is somehow tied to how you are attaching the JAR to your Eclipse project. Outside of Eclipse, it's automatic. Since it's been a couple of months since I last taught my course, I am rusty on the Eclipse steps -- sorry!
CommonsWare
That's ok - I'll keep investigating. Thanks for your help anyway.
robintw
Ah - I've just been playing around with it a bit more, and I've managed to get your sample code to work, by manually going into build properties and adding the JAR file. However, doing exactly the same thing for my project doesn't seem to work. Do you think it might be something to do with my .jar file? It works fine in a standalone command-line application.
robintw
Is your "standalone command-line application" part of the JAR itself? If so, try separating that out into its own JAR, and see if it still works.
CommonsWare
Nope - it's a completely separate project, just importing the jar file.
robintw
:: shrug :: If you visit http://github.com/commonsguy, all of the "cwac-" projects are designed to build reusable JARs for Android. Take a peek at those and see if anything leaps out at you as being the source of your difficulty.
CommonsWare