views:

149

answers:

1

I'm trying to develop an external library (not sure if that's the right term) to provide prepackaged functionality in Android projects. Working in Eclipse, I've added the appropriate android.jar file to the build path, and everything is happy both while editing and upon compilation.

However, when I use Android's Handler and Message classes (android.os.Handler, android.os.Message) for inter-thread communication, I get exceptions unless I'm running within an Android app, on the emulator or a device. I can no longer test my library in a "standalone" way without having to go through a running Android target.

Is there any way for me to include these two Android classes and still be able to test my library standalone? Do I need to have the Android source available? Or would it require some sort of conditional compilation hand-waving?

+2  A: 

Is there any way for me to include these two Android classes and still be able to test my library standalone?

Not readily, by any means I can think of.

Do I need to have the Android source available?

I don't know where else you would get the implementation from. But, more importantly, those things are not designed to work in isolation outside of the OS, any more than you could just grab a Cocoa class or two and pull them into your Objective-C library and expect them to run on a Windows box.

Off the cuff, knowing nothing about what you're building, I would make whatever dependency you are introducing on Handler and Message be more pluggable. Test outside of Android using a pure-Java implementation, perhaps even just some mocks. Test inside of Android using the real implementation.

CommonsWare
I figured this would probably be the case - though I'm not sure how I would go about making the dependent portions of my library "pluggable". At this point I'm thinking about inheriting the class in question with a thin wrapper class, then overriding the portions that require the use of Handler and Message. The test code would instantiate the (pure Java) parent class directly, while Android apps would instantiate the wrapper. Does this sound reasonable?
medicdave
Yes, off the cuff, that seems reasonable.
CommonsWare
Thanks - I tried this out and it works great! A link to how I did it is below; you can see where the handler and message code were removed from the DeaconService class and moved to the Deacon wrapper class. Thanks again for your help!!http://github.com/davidrea/Deacon/commit/117f7586b37d3d994afc7f948aefa4e5a5000856
medicdave