Tuesday, November 24, 2009

Building A Project On The BlackBerry

Update 2: The Netbeans link within the body died a while back. Updated instructions for 6.7 and newer are available here.

Updated: I forgot to add the main method and UiApplication section. It is now part 9 of the instructions.

While Thorsten & myself wrote build instructions for the BlackBerry in the forum/mailing list in the past they are somewhat outdated by now and aren't as easy to find using Google. So here is a step by step guide on porting to the BlackBerry devices with some explanations of the Caveats.

The standard LWUIT won't work on the Blackberry since it references API's such as 184/226, notice that LWUIT doesn't need or use these API's when they are not available! On the Blackberry device the very reference to these API's will cause the application to fail. Furthermore, as I explained in a previous post, Blackberry devices have some "issues" when using classes compiled without the RIM toolchain and LWUIT isn't compiled with the RIM toolchain.Last but not least, the performance and user experience of the MIDP based LWUIT on Blackberry isn't very good...

The solution for all these issues is to use the RIM API's, which unfortunately means you need to create two separate versions of your application (3 if you need blackberry Storm support) .

I won't go into too many details on the BlackBerry devices, but a couple of years ago they introduced the Storm device which was their first touch screen device (featuring a click screen). Supporting the Storm requires importing classes of the API that aren't supported in older versions of RIM's API, hence Storm compatibility requires using a relatively new BlackBerry OS that most users don't have.So you will need two builds and two blackberry Java Development Environments (JDE's) if you need to support Storm.

The instructions bellow are also geared towards the NetBeans IDE and Ant build.

LWUIT currently has two maintained BlackBerry ports, one created by Thorsten & another created by myself. Each has its advantages and you can easily go back and forth between them. Since I used NetBeans I will explain based on my port but you can easily replace the content of the src directory with Thorsten's port and it should be pretty seamless.

A standard LWUIT MIDlet should work fine on a RIM device using the ports, however RIM also offers support for a CLDC application. Using this approach tends to provide improved performance/security handling for RIM although its completely optional.

Fetch the LWUIT sources from SVN and open the BlackBerry project. Clean & Build the project (notice that you MUST clean & build, a plain build will often fail since the RIM port needs to replace some classes from LWUIT).

If using storm as well, change the configuration of the Blackberry LWUIT port to "touch" and again clean & build.

Download the BB ant tasks which are much easier to work with than the approach taken by the knowledge base article.

Add "configurations" to the NetBeans project for BlackBerry and BlackBerryTouch. Set them to use the appropriate JDE Platforms.

In the blackberry configurations add an "Ability" called RIM. This will allow you to use #ifdef RIMrather than #ifdef BlackBerryTouch && BlackBerry(yes, I can't stand #ifdef's but they are better than just copy and pasting the entire code and with RIM we don't have a choice).

In the libraries for the project add both LWUIT & the appropriate blackberry project. Make sure to add the touch JAR only to the touch (Storm) configuration and the standard jar to the other BlackBerry configuration.

If you are building a CLDC UiApplication add the following to your MIDlet class definition (instead of extends MIDlet):public MyMIDlet extends //#ifdef RIM net.rim.device.api.ui.UiApplication //#else //# javax.microedition.midlet.MIDlet //#endif{

Then add this to the body of the MIDlet: //#ifdef Blackberry public static void main(String[] argv) { new MyMIDlet().startApp(); } //#endif

Open the build.xml file of your NetBeans project and add the following, update the paths and names marked in bold (notice this is geared towards a CLDC UiApplication, you might need to modify this slightly for a MIDlet):<target name="post-init"><available file="${platform.home}/bin/rapc.exe" property="do.rapc"><available file="${platform.home}/simulator/9500.bat" property="bbtouch"><condition property="jpda.port" value="8000"> <isset property="do.rapc"></isset></condition>

Hi Shay.I got over the part where it complained about the xml not being well formed. Now it says:Compiling ${preprocessed.dir}Created dir: path_to_my_project\${dist.dir}\bbant(AND YES, IT ACTUALLY CREATES A FOLDER CALLED ${${dist.dir}} LITERALLY!!)Compiling 1 source files to ${name}.codI/O Error: D:\NetBeansProjects\golden-phone_cb_g_2\${preprocessed.dir} (The system cannot find the file specified)D:\NetBeansProjects\golden-phone_cb_g_2\build.xml:24: Java returned: -1

So it obviously fails to interpret ${name},${preprocessed.dir} and so on, it uses them literally. Why and what can I do to fix this? Thanks.

Property "platform.home" has not been setUnable to find ${platform.home}\bin\rapc.exe to set property do.rapcProperty "platform.home" has not been setUnable to find ${platform.home}\simulator\9500.bat to set property bbtouchparsing buildfile jar:file:/D:/NetBeansProjects/myproject/bb-ant-tools.jar!/bb-ant-defs.xml with URI = jar:file:/D:/NetBeansProjects/myproject/bb-ant-tools.jar!/bb-ant-defs.xml from a zip fileProperty "preprocessed.dir" has not been setCompiling ${preprocessed.dir}Property "dist.dir" has not been setSkipping D:\NetBeansProjects\myproject\${dist.dir}\bbant because it already exists.Property "name" has not been setProperty "platform.home" has not been setD:\NetBeansProjects\myproject\build.xml:99: jde home must be a directory at ca.slashdev.bb.tasks.BaseTask.setJdeHome(BaseTask.java:56) at ca.slashdev.bb.tasks.RapcTask.setJdeHome(RapcTask.java:134) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.tools.ant.IntrospectionHelper$7.set(IntrospectionHelper.java:1101) at org.apache.tools.ant.IntrospectionHelper$AttributeSetter.setObject(IntrospectionHelper.java:1502) at org.apache.tools.ant.IntrospectionHelper.setAttribute(IntrospectionHelper.java:405) at org.apache.tools.ant.RuntimeConfigurable.maybeConfigure(RuntimeConfigurable.java:393) at org.apache.tools.ant.RuntimeConfigurable.maybeConfigure(RuntimeConfigurable.java:350) at org.apache.tools.ant.Task.maybeConfigure(Task.java:202) at org.apache.tools.ant.UnknownElement.configure(UnknownElement.java:196) at org.apache.tools.ant.UnknownElement.maybeConfigure(UnknownElement.java:163) at org.apache.tools.ant.Task.perform(Task.java:347) at org.apache.tools.ant.Target.execute(Target.java:390) at org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:179) at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:224) at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:497) at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:151)

sorry to keep coming back with this issue... what do you mean by "you might need to modify this slightly for a MIDlet"? could you give me a hint, be a little bit more specific? thanks and sorry for the trouble...

This is a really good news for blackberry development, today I tried to follow the step by step using your build script, but I found there are so many errors in the build.xml, could you help us update a workable build script?

First thanks for your blog Shai it is basically a unique resource most of us couldnt live without! Now onto my issue:

Ive got a midlet which I have managed to build and run on Blackberry using RIM_UI blackberry lwuit port. It works pretty nice apart from:- "back" commands dont appear in the menu when I hit the blackberry menu key, I can use setBackCommand instead to make it work with the blackberry back key which is fine actually - just wondering why they dont appear, the rest do..- if there is no commands on that screen (ie only a back command which wont get added) then the blackberry menu key brigns up an empty menu, which is misleading, ugly and pointless, is there a way to stop the menu appearing if it has nothing on it? I dont mind if the user has to hit the blackberry back key but I want the empty menu to simply not appear when they hit the blackberry menu key..Thanks in advance,Gaz.

Hi, I have built my midlet for Blackberry Storm, and it runs on the emulator - but I cant touch things to select them, on the emulator im still having to use the cursor keys to move around and enter to select things - how do I get it so I can simply touch something and it will select? Do I need to turn on touch somehow? Thanks!

OK I fixed my issue, I had to define "touch" ability, now when i click on an item it gets highlighted, but it still doesnt do the "click" to actually go into it? How do I enable it so when I click on a menu item it actually goes in, right now I still have to click on it then hit enter!Thanks.

The solution is to use the NewOS or NewOSSigned configuration for touch support since RIM broke compatibility for touch in their 4.7 OS.About the menu issue I have no idea, did you change the command behavior settings?

Article is quite good. Pegasi Media is a b2b marketing firm that has worked with many top organizations. Availing its email list is fast, simple, convenient and efficient. Appending services adds the new record as well as fills up the fields that are missing. Pegasi Media Group also perform Data Refinement, Data building, Data Enchancement, and Data De-Duplication. Database marketing is a form of direct market in which the customers are contacted through their email addresses with the help of the database. There is a scope for email marketing to generate personalized communication with the clients in order to promote your sales. Blackberry Users