[TIP] Device fragmentation - configuration and abilities

Device fragmentation
Everybody creating game, app for more than one device has to face this problem. How to use all features of the particular device and still keep the jar file as small as possible? J2ME really isn't "write once and run everywhere", unfortunately.
What are the main reasons for need of device fragmentation?
  • custom/optional API's are incompatible each other
  • every device has a little bit different features that you will use only on particular device and not on any other
  • you want to use as much as possible from your display but not a bit more => different sizes of pictures for different screen sizes of devices need to be packaged for different distributions
  • definitelly exist more reasons...
To achieve all these goals you'll have either create a branch with a different code for every device (or device set) or you will use a preprocessor. NetBeans Mobility pack decided to go the way of preprocessor. It enable to have all the code in one place (one document) and just pick the right lines and throw out the others. The mobility pack provides even nice integration with Editor and CVS. The active lines of code are marked by different color than the inactive ones. The inactive lines are commented out and the code is compilable outside NetBeans - you will still have fully compatible java source code.

'Configuration' and 'Ability' in Mobility pack.
You can create configurations for your project in Project Properties. Please don't get confused by difference between Project Configuration and Device Configuration (CLDC).

For each configuration you can set up different project properties. E.g. what emulator will used for this configuration, what files will be excluded from build etc... When you have a configuration created you can use it in Editor. Select a block and invoke contextual(popup) menu by right click then select 'Preprocessor Block' and you can specify to which configuration this block belongs. You can of course specify that the block belongs to more configurations. There are allowed NOT conditions too. Else/If blocks can be created from the contextual menu.

'Abilities' were introduced in NB4.1. Even if the configurations are usefull it's still not enought and there are some cases that the configurations didn't cover. E.g. you have preprocessor blocks for Nokia 50s, you will add new configuration for Nokia 60s and you will have to add manually the Nokia 60s' configuration for all the blocks of Nokia50. It's boring and disturbing.
And here comes the 'Ability' approach. Lets define new 'Ability' e.g. NokiaAPI - again in Project Properties

And now you can define preprocessor block for this NokiaAPI. It seems be same as configuration, don't you think? But now comes the trick and amazing feature... For every configuration with a Nokia's device you'll add the NokiaAPI ability and you don't have to change nothing in your code. The NokiaAPI block of code is used when you change to a configuration with NokiaAPI ability.

Switch Configuration, building etc.
You can change active configuration at two places in the IDE
  • in Project Properties - see the 'Project Configuration' combobox on pictures above
  • there is combobox with configurations of main project in middle of toolbar
When you switch configuration the right code is commented out/uncommented in editor. When you want to create distribution build for all existing configurations you don't have to do it one by one. There is 'Build All Project Configurations' action in menu. The builded jars can be found in dist directory in your project location. JAR/JAD for every configuration is in directory with the name of configuration and JAR/JAD of DefaultConfiguration is directly in 'dist'.
Look at the directory structure of compiled project with Nokia50, SiemensCX65 and of course the default DefaultConfiguration configurations

Restrictions and coming improvements in next release
  • Once the ability is created it cannot be removed from abilities list (the list of all abilities available in all opened projects). Abilities are shared among projects. Unused ability is removed after restart of IDE.
  • You cannot combine configurations and abilities with AND and create boolean expresions. For example if you want have a block used only when both abilities NokiaAPI AND NokiaSound are active you cannot do this. There is solution but it's rather workaround - define new ability NokiaAPIandSound for this block and associate it with the configuration
  • The syntax of preprocessor block is hidden - you can see it when you select the code in editor. Something like /\*#NokiaAPI#\*/// should appear.
For next release there will be improvements
  • Antenna like syntax visible in editor. Only part of antenna directives, definitelly not the #include.
  • boolean expressions
  • nested blocks
  • stay tuned for more, i'll announce it in my blog when it's available in build.

Are you interested in a demo in flash? I'll try to prepare not-shorted version of demo that is provided on javalobby - you can check it on this link - NB41 Javalobby Demo by Roumen and check chapter J2ME.

Post a Comment:
  • HTML Syntax: NOT allowed

This is the main channel to share news, tips and other stuff related to my work. Sometimes you can find even personal stuff. Enjoy your reading...


« April 2014

No bookmarks in folder