It is possible to compile an app for OS 6.0 and run it on a 5.0 device, for instance, and as long as you don't use newer APIs you are ok (I've just made a test 2 minutes ago just to make sure).
I'm using eclipse 3.6 with the last BB plugin to develop an app where we need to use barcode scanning only when available (API 6.0 and higher). The cleanest solution would have been not to load the conflictive classes when in lower versions. If this were Java SE I'd have encapsulated all uses of the barcode API in a class (lets call it OS6Impl
), also created a default OS5Impl
, and I'd have instantiated one or the other using reflection at runtime depending on the OS level of the device.
But this is BB and that is not enough, so we had to resort to preprocessor directives and manage 2 different versions of the app. In case the preprocessor flags disable the new API features, the resulting preprocessed OS6Impl.java
file is stripped of all content. I can see this by looking at the file inside the .preprocessed
folder, and also opening the generated jar file, where no OS6Impl.class
file is present. When flags enable the new features, the OS6Impl.java
file has code inside and the OS6Impl.class
file is included in the jar.
This is OK and should work on both OS5 and OS6 devices compiling both versions with JRE 6. But it doesn't. The preprocessing is OK, the class is not included in the jar (so it shouldn't be in the cod), but when we run it in a 5.0 device it shows an error message ("module not found"). The module is "net_rim_barcodescanner", which we intend to use to scan barcodes only in 6.0+ devices.
I had to compile for JRE 5.0 to get rid of the error. This is ridiculous, since code compiled for 6.0 that doesn't use newer APIs is able to run in lower versions as I said before. I've verified several times that no import of the newer APIs are used other than in the OS6Impl.java
class, that the preprocessor directives are set up correctly, and that a clean and build was done after each preprocessor directive change. Also rebooted the blackberry after installing each cod.
And now to compile a version or another, we have not only to modify the directives in the app descriptor, but also to modify the build path to switch the JRE to 5 or 6.
What kind of dependencies are added when using preprocessor directives to strip incompatible code? Why are those dependencies not added when compiling compatible code without preprocessor directives? Isn't the preprocessor something that runs before the compiler?