Monday, May 17, 2010

Ant Script for Blackberry Development in IntelliJ Idea

To say that developing Blackberry J2ME applications is a nightmare is an understatement, especially is you wish to setup a comprehensive build and test environment for the team. It is possible to do things in bits and pieces, but having a single integrated development environment handle it all gracefully looks like a distant dream, forcing us to move to Ant and use its build scripts to get a build environment that is not only independent of the IDE but can also be used in continuous builds. Many thanks to Andrew for helping us to get this far. I have compiled the steps needed to set up the Ant environment in IntelliJ Idea.

1. Install Sun's Java ME SDK

2. Install Ant such as its path does NOT have a blank space in its installation path name

3. Install the relevant version of RIM JDE and emulator

4. Create an ordinary Java project. Change SDK to ME in project settings (Project Settings->General, you can bring up the dialog from File->Settings)

5. Check the folder in which the project has been created. Create the following sub-folders inside it: application, bin, lib, src, test, test_build, test_results. Within "test_build" create a "classes" folder

6. Even though you could link jar files from anywhere in the system, it may be a good idea to put following jars for following projects at one place in the lib directory:

Blackberry Ant Tools
JMUnit and Hammock
JDOM
J2MEUnit
Microemulator

Additionally, to make your life easier while coding, copy the net_rim_api.jar from the lib folder of the installed RIM JDE

7. In project setting (Project Settings->Modules), set "src" as the source folder and "test" as the test folder. In dependencies, add jars for RIM, JMunit, Hammock and J2MEUnit.

8. Expand the "Ant Build" panel and open properties dialog. Under execution, use custom ant and point to the intallation that does not have any blank spaces in its path. Under Additional Classpath, add all the jars that we had put in step 6. Be sure to have the ant tasks for JMUnit and Blackberry Ant Tools.

9. You are all ready to start coding under your "src" folder and write tests under your "test" folder. Now we need a build file that can enable you to actually build and test your code. For that, make a "build.xml" file, which will be your ant script, and place under the root folder. Use this script provided for you. Don't forget to adjust the paths as per your installations. With some luck, this should bring up 8 tasks on your pane: build, build_and_deploy, build_deploy_and_launch_simulator, launch_simulator, test, test_compile. Note that the build task will not launch test task in the current script, but you could change it if you please.

These tasks will accrue you the following benefits:

1. You can write your blackberry development code in Idea
2. You have an ant script that can be used to make contunuous builds and tests. The test also generates reports that are stored under the test_results folder.

My team has still not found a satisfactory answer to debugging, but current set-up helps us to develop using a more effective tool and get our automated build systems to run effectively for the wretched J2ME. Moreover, this Ant script can be run from any IDE (with similar configuration for the ant used). It is still not 100% perfect, but we are getting there: constantly thinking about testability, logging and sustainability of build environment. I will keep updating the build file so as it gets more comprehensive.

PS: We were able to build and test using Ant and Netbeans, but the build/deploy part was tightly coupled with Netbeans, which was not encouraging for automated build tasks.

No comments: