I have 3 projects: desktop_starter, android_starter and platform. Both the starter projects references the platform project, and the platform project is supposed to be (99%) platform independent, and contains almost all code.
The reason is ofcourse that I want to be able to run the android app on the desktop without having to use the emulator.
However at least on one place I need to call platform-specific code in platform. Specifically I need to use javax.ImageIO on desktop or android.graphics.Bitmap to read an image pixel by pixel.
As there is no preprocessor or similar constructs in java AFAIK, what are my choices?
- I could send in the image-reader as an Object to
platformand use reflection I guess, but thats dirty. - I find a built-in image reader that works on both desktop java and android, any ideas?
- JNI and use libpng or something like that I guess, seems as there should be an easier way.
You could simply code the platform code on an interface defining the methods you need, and pass a platform-specific implementation of this interface from the platform-specific project.
For example, if what you need is an int array containing all the pixels of the image (just an example), the interface would be:
And you would have two implementations of the interface: one using ImageIO internally, in the desktop project, and the other using Bitmap internally, in the android project. An instance of one of those classes would be passed to the platform-independant code.