Tuesday, May 18, 2010

LWUIT BlackBerry ANT script refactored

My last article about using ANT script to build LWUIT blackberry application was intereted by some people, which is encouraging. I spent some time to review the ant script again and made some improvements. Here I posted my updated build script, it is cleaner and easier to use, you can just use it as template, copy and paste it in your NetBeans project by just configure some parameters.

<property name="app.title" value="helloLWUIT" />
    <property name="app.version" value="1" />
    <property name="app.icon"   value="/icon.png" />
    <property name="app.vendor" value="foo" />

    <target name="post-init">
          <available file="${platform.home}/bin/rapc.exe" property="do.rapc" />
          <condition property="jpda.port" value="8000">
              <isset property="do.rapc">

   <target name="post-jar" if="do.rapc">
     <antcall target="bbbuild" />

<target name="post-clean">
   <delete failonerror="false">
      <fileset dir="${platform.home}/simulator">
       <include name="**/${name}.*">

<typedef resource="bb-ant-defs.xml" classpath="bb-ant-tools.jar" />
  <target name="bbbuild" description="blackberry build" depends="init">
          <echo message="rapc build, dir=${dist.dir}"></echo>
          <mkdir dir="${dist.dir}/bbant" />
          <rapc verbose="true" output="${name}" jdehome="${platform.home}" import="${platform.bootclasspath}" destdir="${dist.dir}/bbant/" noconvert="true">
             <fileset file="${dist.dir}/${dist.jar}" />
          <jdp title="${app.title}" vendor="${app.vendor}" version="${app.version}" type="cldc" icon="${app.icon}" >

        <!-- sigtool jdehome="C:Program FilesResearch In MotionBlackBerry JDE 4.7.0" codfile="${dist.dir}/bbant/${name}.cod" password="" / -->
        <alx destdir="${dist.dir}/bbant" filename="${name}.alx">
            <application id="${app.title}">
                    <fileset dir="${dist.dir}/bbant" includes="*.cod">
       <mkdir dir="${dist.dir}/bbant-final" />
       <jadtool input="${dist.dir}/bbant/${dist.jad}" destdir="${dist.dir}/bbant-final">
        <fileset dir="${dist.dir}/bbant" includes="*.cod">

<target name="copyBBSimulator" depends="bbbuild" if="do.rapc">
    <copy todir="${platform.home}/simulator" verbose="true">
            <fileset dir="${dist.dir}/bbant">
                  <include name="*.cod"/>
                  <include name="*.jad" />

<target name="bb-run" depends="copyBBSimulator">
    <exec os="Windows NT Windows 95 Windows 98 Windows 2000 Windows XP"
        failonerror="true" resolveExecutable="true"/>

It has following improvements:
    1. Separate out the customized parameters in the build scriciptThen entire build script only needs following customized parameters: app.title, app.version, app.icon, app.vendor. You only need to configure them with your own application parameters.
    2. Simplify the build script.  In the old build script, you have to use target "copyBBTouchSources" and "copyBBSources" to copy and update the LWUIT library source code into specific folder, then call rapc compiler to build the entire application. But when you investigate a little bit deeper, since NetBeans IDE already generate jar file for you, which already included the preprocessed application and the LWUIT blackbery library class files, why not just pass this jar file to rapc and then generate the cod file? based on this idea, I modified the target "bbbuild" and it works.
    3. One build script supports both BlackBerry touch and non-touch build. since the build script is simplified and only use jar file to generate cod file,the touch or non-touch specific files are already embedded in the jar file, the build script itself becomes generic. To tested it, I installed JDE 4.5 and JDE 4.7 two different JDE in the NetBeans, and set up 4.5 and 4.7 different configurations in my project, and I found both works.
    4. Added the ant target "bb-run", which allow you run the application in the blackberry simulator. To make it work, you had better to check the nbproject\project.properties file, if the simualtor device name is configured properly, In JDE 4.7, the device name is 9500, like this:

      In 4.5, the device name is 8320, like this:

    Update: Sample project
    Please click here to download the sample project.


    1. i'm using netbeans to develop this kind of progect but unfortunately i didn't find the ant build.xml
      can you guide me

    2. mouhamad do you mean the bb-ant-defs.xml ? by any chance ? if so then you can find it inside the folder extracted from the bb-ant-tools.jar. copy both the bb-ant-tools.jar and the xml inside your ant classpath and things should start working thereafter.


      Ashok Srinivasan.

    3. i applied all configuration based on shai's article and added above code to my project. Finally, when i want to build my project, i get an error like that "Error preverifying class ca.slashdev.bb.tasks.BaseTask
      java/lang/NoClassDefFoundError: org/apache/tools/ant/Task
      C:\MB2\nbproject\build-impl.xml:471: Preverification failed with error code 1.

    4. Hi, Yakup,
      It might be possible that you did not install the bb-ant properly, or the NetBeans did not find bb-ant-tools.jar in its class path, for example in my environment, I put bb-ant-tools.jar at folder C:\Program Files\NetBeans 6.8\java3\ant\lib please try it.

    5. Hi, mouhamad,
      do you mean the build.xml in your NetBeans project, in NetBeans IDE, click the files tab, then you will find the build.xml
      please check this link to download the sample project source code: http://docs.google.com/leaf?id=0B3TGMRuLRen_NTJmNTYxOWUtYmJhYi00MDY4LWEyNTEtMjEzZGU3YzA2N2I3&hl=en&authkey=CLqUuuAH

    6. Hi.

      I followed Shay blog, created all the necessary configurations and also copied the bb-ant-tools.jar to my "c:\Program Files\NetBeans 6.9\java\ant\lib" folder.

      If I just build the project, I'm getting:

      C:\Dev\NetBeans\helloLWUIT\nbproject\build-impl.xml:354: The following error occurred while executing this line:
      C:\Dev\lwuit\BlackBerry\BlackberryPort\nbproject\build-impl.xml:57: Platform home (platform.home property) is not set. Value of this property should be BlackBerry 4_7 emulator home directory location.

      If I right click the build.xml and select "Run" I get:

      rapc build, dir=dist/BlackBerry
      C:\Dev\NetBeans\helloLWUIT\bb-build.xml:47: srcdir attribute or element required!

      Any idea what's going on?

      Thanks in advance!

    7. I'm trying to run your sample project, and getting same results.

      Any idea?

    8. I tried to move the following bit of Ant above rapc (as in your example above - it was different in your sample application):

      But got following error:
      rapc build, dir=dist/BlackBerry
      C:\Dev\NetBeans\helloLWUIT\bb-build.xml:47: Problem: failed to create task or type src
      Cause: The name is undefined.
      Action: Check the spelling.
      Action: Check that any custom tasks/types have been declared.
      Action: Check that any / declarations have taken place.


    9. Sorry, this code didn't post well here, so I'm reposting with <> stripped.

      fileset file="${dist.dir}/${dist.jar}" /

    10. Had some progress but stumbled this error:

      rapc build, dir=dist/BlackBerry
      Created dir: C:\Dev\NetBeans\helloLWUIT\dist\BlackBerry\bbant
      Compiling 1 source files to helloLWUIT.cod
      java.lang.NoClassDefFoundError: Files\Java\jdk1/6/0_20
      java.lang.NoClassDefFoundError: Files\Java\jdk1/6/0_20
      Caused by: java.lang.ClassNotFoundException: Files\Java\jdk1.6.0_20
      Caused by: java.lang.ClassNotFoundException: Files\Java\jdk1.6.0_20
      at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
      Could not find the main class: Files\Java\jdk1.6.0_20. Program will exit.
      Could not find the main class: Files\Java\jdk1.6.0_20. Program will exit.
      Exception in thread "main" Exception in thread "main"
      C:\Dev\NetBeans\helloLWUIT\bb-build.xml:12: The following error occurred while executing this line:
      C:\Dev\NetBeans\helloLWUIT\bb-build.xml:36: Java returned: 1

      Any idea?

    11. Answering myself, the trick was using older BB-tools - version 1.2.4 to be exact.

      Thanks for the build file!

    12. Hi, Syrenity,
      Thanks for you posting your issue, you mentioned that you have to use bb-ant-tools 1.2.4 only, but I checked my bb-ant version, it is 1.2.7. Maybe because my netbeans version is 6.8 while yours is 6.9?

    13. hi Steve,
      I am developing J2me project and using LWUIT in netbeans 6.9 while i build my MIDP project i got an error build.xml file not found.what's the problem.Plz help...