I am making an application for blackberries and wanted to make use of touch capabilities on touch capable phones. Is there a way to do this without making two separate projects (like I would in c with preprocessors that check the compiler)?

An idea of what I'm trying to do would be like(if it were in C):

#ifdef RIM_JRE_5.0
//some touch sensitive code
#endif
#ifdef RIM_JRE_4.3
//some plain old buttons code
#endif

I was able to find something like this here:http://www.slashdev.ca/javapp/ but I was wondering if there was a way to do it natively
and I'm not sure if that project is able to detect the jre

Dani AI

Generated

Good catch, — rapc does include a small file‑level preprocessor (introduced around the JDE/rapc 4.x line), so you can keep a single source tree and selectively compile touch-only code. The preprocessor is deliberately simple: add //#preprocess as the very first line of any file you want processed, then use //#ifdef / //#ifndef / //#else / //#endif (there’s also //#implicit for whole-file inclusion). Be aware the implementation is limited (no nested preprocessed blocks, no macros). (exchangetuts.com)

Practical workflow that keeps one project:

  • Put //#preprocess at the top of any Java file that may contain touch-only APIs.
  • Wrap imports and touch-specific code with a tag, e.g. //#ifdef TOUCH//#endif.
  • Set the tag for touch builds in the BlackBerry Eclipse plugin (Project → BlackBerry Project → Build → Preprocessor Directives) or when using Ant/rapc supply the defines value (semicolon/colon-delimited or nested <define> elements) so rapc sees the tag at compile time.

Example (file header and guarded import):

//#preprocess
//#ifdef TOUCH
import net.rim.device.api.ui.TouchEvent;
// touch-only code...
//#endif

Use the plugin UI for interactive builds and the rapc/Ant defines when automating CI. (pt.scribd.com)

If you prefer shipping one binary that runs on both touch and non‑touch devices, detect touch at runtime (for example, use a temporary Canvas and call hasPointerEvents()), and branch behavior accordingly. For APIs added in newer OS versions, avoid direct imports in code paths that must compile for older SDKs: either remove those imports with the preprocessor or call them reflectively (Class.forName / Method.invoke) so missing classes aren’t referenced at load/compile time. (stackoverflow.com)

Common gotchas: //#preprocess must be the first line, some editors/IDEs will flag the directive until you enable preprocessing, and Ant tasks must pass the same defines to rapc or preprocessing will be ignored. For maintainability prefer small, well‑scoped guarded blocks or separate helper classes selected at runtime rather than deep conditional nesting. (stackoverflow.com)

Seems that since version 4.0 rapc natively supports, weird that it didn't come up on any searches, I only found it through the bb-ant-tools docs (). Maybe because I was searching for a preprocessor for a jre and not specifically rapc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.