Saturday, May 1, 2010

ANT build script for LWUIT blackberry application

Update:
  This article is more than a year old, now LWUIT project uses a new tool to build BlackBerry application - NetBeans BlackBerry Plugin, please refer my two latest post:

  Developing Blackberry application using NetBeans Plugin - part 1, first impression
  Developing Blackberry application using NetBeans Plugin - part 2, fixing debug issue


Shai's article about build script for LWUIT in BlackBerry is great, but it has so many errors,I spend two days trying to fix the errors, and finally make it work.

Here I posted my modified ant buil.xml and application source code. The application is very simple, just display a "Hello World" text. Since this article is based on Shai's article, for details such as project set up, configuration and tools installation etc, please check his article.

application source code
HelloLWUITMidlet.java

import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.layouts.BorderLayout;


public class HelloLWUITMidlet extends
//#ifdef RIM
     net.rim.device.api.ui.UiApplication
//#else
//#      javax.microedition.midlet.MIDlet
//#endif
 {
    public void startApp() {

        Display.init(this);
        Form f = new Form();
        f.setTitle("Hello World");
        f.setLayout(new BorderLayout());
        f.addComponent("Center",
        new Label("I am a Label"));
        f.show();

    }
//#ifdef BlackBerry
   public static void main(String[] argv)
   {
       new HelloLWUITMidlet().startApp();
   }

   public void notifyDestroyed()
   {
       System.exit(0);
   }

   public void platformRequest(String s) {
      net.rim.blackberry.api.browser.Browser.getDefaultSession().displayPage(s);
     }
//#endif


    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

ANT build script
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<project name="helloLWUIT" default="jar" basedir=".">
    <description>Builds, tests, and runs the project .</description>
    <import file="nbproject/build-impl.xml"/>
   

   
     <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>
     </target>


        
        <target name="post-preprocess" depends="copyBBSources,copyBBTouchSources">
        </target>
        

       <typedef resource="bb-ant-defs.xml" classpath="bb-ant-tools.jar" />
       <target name="bbbuild" description="blackberry build" depends="init,copyBBSources,copyBBTouchSources">
          <echo message="Compiling ${preprocessed.dir}"></echo>
          <mkdir dir="${dist.dir}/bbant" />
          <rapc verbose="true" output="${name}" jdehome="${platform.home}" import="${platform.bootclasspath}" destdir="${dist.dir}/bbant/" noconvert="true">
          <src location="${basedir}/${preprocessed.dir}">
          </src>
          <jdp title="helloLWUIT" version="1" type="cldc" icon="/icon.png" >
          </jdp>
          </rapc>
          
        <!-- sigtool jdehome="C:\Program Files\Research In Motion\BlackBerry JDE 4.7.0" codfile="${dist.dir}/bbant/${name}.cod" password="" / -->
        <alx destdir="${dist.dir}/bbant" filename="${name}.alx">
        <application id="helloLWUIT">
       <codset>
        <fileset dir="${dist.dir}/bbant" includes="*.cod">
       </fileset>
       </codset>
       </application>
       </alx>

      
       <mkdir dir="${dist.dir}/bbant-final" />
       <jadtool description="desc" id="appId" input="${dist.dir}/bbant/${dist.jad}" destdir="${dist.dir}/bbant-final">
        <fileset dir="${dist.dir}/bbant" includes="*.cod">
        </fileset>
       </jadtool>
       
      </target>

 <target name="copyBBTouchSources" if="bbtouch">
 <echo message="Copying blackberry touch sources"></echo>
 <copydir forceoverwrite="true" src="${project.BlackberryPort}/build/touch/preprocessed" dest="${basedir}/${preprocessed.dir}" />
 <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/M3G.java" />
 <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/SVGImage.java" />
 <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/animations/Transition3D.java" />
 <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/impl/midp/SVGImplementation.java" />
 
 </target>

 <target name="copyBBSources" if="do.rapc">
   <echo message="Copying blackberry sources ${preprocessed.dir}"></echo>
   <copydir src="${project.LWUIT}/src" dest="${basedir}/${preprocessed.dir}" />
   <copydir forceoverwrite="true" src="${project.BlackberryPort}/build/preprocessed" dest="${basedir}/${preprocessed.dir}" />
   <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/M3G.java"/>
   <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/SVGImage.java"/>
   <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/animations/Transition3D.java"/>
   <touch file="${basedir}/${preprocessed.dir}/com/sun/lwuit/impl/midp/SVGImplementation.java"/>
 </target>

 <target name="post-jar" if="do.rapc">
   <rapc verbose="true" output="${name}" jdehome="${platform.home}" import="${platform.bootclasspath}" destdir="${platform.home}/simulator/" noconvert="true">
     <src location="${basedir}/${preprocessed.dir}">
     </src>
     
     <jdp title="helloLWUIT" version="1" type="cldc" icon="/icon.png" >
     </jdp>
   </rapc>
 </target>
 
 <target name="post-clean">
    <delete failonerror="false">
       <fileset dir="${platform.home}/simulator">
        <include name="**/${name}.*">
        </include>
       </fileset>
    </delete>
 </target>

</project>


Note:
This build script is for touch screen build, for non touch sreen build you need to
1.Comment out "bbtouch" property in target "post-init" :
<!-- <available file="${platform.home}/simulator/9500.bat" property="bbtouch">  --> </available>
2.Remove the "copyBBTouchSources" dependency in target  "bbbuild".

11 comments:

  1. I have not yet checked this. But it is really good and great of you to have taken your precious time to atleast give a consideration to fix this and posting the same.

    Thanks a lot Brother.

    Regards,

    Ashok Srinivasan.

    ReplyDelete
  2. I have compiled both the version of BlackberryPort i.e., Blackberry and BlackberryTouch. Then included them in my LWUIT project inside Netbeans.

    Now since Shai Lang's XML has a good number of errors we are unable to use it. So tried using yours. Is it possible for you to highlight the places where we need to adjust in the XML for the port to work for our specific projects.

    Regards,
    Ashok Srinivasan.

    ReplyDelete
  3. Steve Zhang,

    Right from today you are also one who is going to be added to my list of Superhero's.

    1 - Steve Zhang the great.
    2 - Superman
    3 - Spiderman
    4 - Captain Marvel
    5 - Batman

    Thanks a lot bro for your ant script did wonders. I was able to render my full fledged Symbian App built with LWUIT with the same effect on a Blackberry Device.

    Thanks a lot brother.

    Regards,

    Ashok Srinivasan.

    ReplyDelete
  4. Hi, Ashok,
    Please check my latest article, I made some improvements for the ant script, http://code-dojo.blogspot.com/2010/05/lwuit-blackberry-ant-script-refactored.html

    ReplyDelete
  5. kewl brother,

    I am now going to see it. Thanks for the update though.

    Regards,

    AShok Srinivasan.

    ReplyDelete
  6. Thanks for this update. Me Also work with Blackberry Application Development and this information is useful to all.

    ReplyDelete
  7. thanks too but u have problem with netbeans 6.9

    Compiling 1 source files to HelloLWUITMidle.cod
    java.lang.NoClassDefFoundError: Files\glassfish-v2/1
    java.lang.NoClassDefFoundError: Files\glassfish-v2/1
    Caused by: java.lang.ClassNotFoundException: Files\glassfish-v2.1
    Caused by: java.lang.ClassNotFoundException: Files\glassfish-v2.1
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    Could not find the main class: Files\glassfish-v2.1. Program will exit.
    Could not find the main class: Files\glassfish-v2.1. Program will exit.
    Exception in thread "main" Exception in thread "main"

    please help me as fast as you can

    ReplyDelete
  8. I am a new blackberry developer. This is very nice blackberry application development tutorial... found much interesting...

    ReplyDelete
  9. hello I am a new blackberry developer and when i try to build as instructed here i keep getting:

    "Could not load definitions from resource bb-ant-defs.xml. It could not be found."

    any idea what might be causing this?
    Netbeans 7.0.1
    Blackberry 6.0.0 USING 9800 SIMULATOR


    THANKS

    ReplyDelete
  10. Hi, Funky-munky,
    It seems NetBeans could not find bb-ant jar files. it requires bb-ant

    ReplyDelete
  11. You can also check the new tool:

    http://code-dojo.blogspot.com/2011/08/developing-blackberry-application-using.html

    http://code-dojo.blogspot.com/2011/09/developing-blackberry-application-using.html

    ReplyDelete