Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Using preprocessing or dynamic class loading to support multiple OS versions

I spent some time browsing through threads about this topic and finally figured it out, just wanted to share this knowledge and ask more experienced users to confirm if i got it all correct.

I use eclipse to code and the jde to compile (no rim plugin)

to enable preprocessing for a project do the following:
edit the jdp file and add a line Options=-quiet -define=VER_4_2_1;PREPROCESSOR
the VER_4_2_1 is freely defined, it is the name of the tag you use.
To add another version for the storm you create a copy of both jdw and jdp.
Rename the copied project to, e.g., projectname47.jdp and projectname47.jdw.
Edit the 47 jdw to reference the new jdp file
Edit the jdp file to replace the 4.2.1 tag with a new one for 4.7, for example VER_4_7_0_AND_ABOVE

In every class that should use preprocessing put //#preprocess in the first line.
There are statements that allow the control flow: //#ifdef, //#ifndef and //#else. Each statement has to be followed by a tag as defined in the jdp.
I had to change some comment formatting in eclipse to avoid the auto formatter indenting the comments, the jde only wants the statements if they are on the beginning of the line.
With //#endif the dependent code is ended.

Example of a version depending import:

//#ifdef VER_4_7_0_AND_ABOVE
import net.rim.device.api.ui.decor.BackgroundFactory;//

To make things easier to control it is also benefical to change the output name of the cod, for example to appname47.

If you want to add more versions you have to copy workspace and project again, define a new tag in the project.

Other options:
- use Ant to build multiple versions at once (and more), see Ant preprocessing
- Maven2 provides even more powerful tools.

Dynamic class loading:

richard_puckett wrote:

Let me just post how I create my User-Agent string, which also contains how I get the current OS.


public static String getOSVersion() {
String swVersion = null;

ApplicationManager appMan = ApplicationManager.getApplicationManager();

ApplicationDescriptor[] appDes = appMan.getVisibleApplications();

int size = appDes.length;

for (int i = 0; i < size; i++){
if ((appDes[i].getModuleName()).equals("net_rim_bb_ribbon_app") {
swVersion = stripBuildId(appDes[i].getVersion());

return swVersion;

public static String getUserAgent() {
String version = getOSVersion();
return "BlackBerry" + DeviceInfo.getDeviceName() + "/" + version + " "
+ "Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/" + Branding.getVendorId();

private static String stripBuildId(String version) {
int index = version.lastIndexOf('.');
return version.substring(0, index);


Source: Forum post by Richard Puckett

You can load classes dynamically using Class.forName() together with the version of the blackberry OS at runtime.( Also see AOP, AOP (wikipedia))
Dynamic class loading can not be integrated into existing projects easily, but if you encapsulate your code you get some side benefits, like being able to test networking code with JUnit and similar tools.
Using factory method pattern allows code to use bb-specific APIs, in your test classes you only have to overwrite the factory provider.
See Factory pattern for example. Without this i would never have managed to integrate the SAP webservices using ksoap2.

I hope this post provides some helpful information, feel free to amend anything you think appropriate or tell about your own best practices.

feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-, Z10 (STL100-3)-, Z30 (STA100-5)-, Passport (SQW100-1)-, PlayBook (16GB)-
My Carrier: Verizon

Re: Using preprocessing or dynamic class loading to support multiple OS versions

This should be helpful for the people who keep asking how to build for multiple BlackBerrys.
---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Using preprocessing or dynamic class loading to support multiple OS versions

I would just reiterate that the c++ preprocessor is an option and you can use all the features you

may be used to.

g++ -x c++ $3 -DRIM_OS_TGT=500000 -I some_include_thing etc