2

I'm writing a library that requires some of the functionality provided by the javax.imageio.ImageIO class. Android does not provide these classes, but instead exposes similar functionality through android.graphics.Bitmap and related classes. It should be pretty easy to abstract away this functionality behind an interface, but I'm having trouble figuring out a good way to automatically load the correct implementation for a platform.

I initially looked at java.util.ServiceLoader, which looked like it would do the job perfectly, but it appears that it is broken in android. I've also looked at some heavier solutions like OSGi, but they really seem like overkill.

Is there an easy way to write two implementations for an interface and then load the correct one based on whether the code is running on Android vs the Desktop?

Community
  • 1
  • 1
TwentyMiles
  • 4,063
  • 3
  • 30
  • 37
  • Unless you have an actual JVM on the Android device (possible but highly unusual) you will ultimately need different builds anyway. And it will similarly be tricky to get the Android toolchain to build code which references unavailable classes in javax. So my suspicion is that you should be trying to solve this at build time ( perhaps by using projects which include different target-specific folders as well as a common core) rather than trying to do runtime detection. Not that runtime detection is tricky itself, just that making that the only problem will be. – Chris Stratton Feb 18 '14 at 19:24

0 Answers0