was successfully added to your cart.


BlackBerry: Catering for multiple OS versions with a single code base.

By February 17, 2011BlackBerry, development

One of the challenges involved with BlackBerry development is the need to produce code that is able to cater for a variety of devices, running different OS versions and with differing screen sizes. In an ideal scenario, one would want a single code base that can be built against different BlackBerry OS versions, if needs be. A problem begins to emerge when one needs to access an API which doesn’t exist in some of the OS versions you are targetting… clearly, if it doesn’t exist to your compiler, it won’t build. Whilst BlackBerry has support for Java reflection – the mechanism to dynamically obtain a class at runtime by the use of the class name – the support is basically limited to only class loading…. one cannot actually call actions/methods on the loaded class dynamically.

I’ve just recently come across this issue where I needed to access a class introduced in 4.7, but my code base was building against 4.5. My specific problem was that I needed to restrict the orientation of my application, however, the class allowing me to do so was only introduced in 4.7. Instead of having to create two separate projects to build against different versions, I came across a solution here which allowed me to target different versions from the same code base. Essentially the solution relies on preprocessor compiler directives to dictate which pieces of code ultimately get compiled.

As a quick example of how something like this will look in code, I’ve included the piece of code that enabled me to restrict the orientation of my application (using a class from 4.7) within the same code base that is also built against 4.5.

private void setOrientations()
//#ifndef JDE_4_7_0
int directions = Display.DIRECTION_NORTH;
//#ifndef JDE_4_7_0

The directive JDE_4_7_0 will need to be defined in the project application descriptor when building against 4.7 – for full configuration instructions, see the previous link posted. What you will notice from the above code is that when the directive is not defined (in my case when building against 4.5), the piece of code is commented out and as a result the 4.5 compiler will never complain about the code that was only introduced in 4.7.

Obviously, this type of setup can become very messy if used excessively; however, I think it is a great solution for simple pieces of code when one needs to access APIs in different OS versions from the same code base 😉

One Comment

Leave a Reply