Sunday, June 27, 2010

Debug BlackBerry Application Inside NetBeans

This post continues the my last post  about blackberry application development.

I added the ANT target to launch the blackberry JDWP debugger from these two articles: 1) and 2). I can launch the BlackBerry JDWP and simulator, but unfortunately the app did not stopped at the break point.
I googled a lot, but it seems none of the post tells you that debugging blackberry successfully inside the NetBeans IDE, I did not find any tutorial about using JDWP in blackberry development document either.
So I have to figure out by myself by using reverse engineering again.

First I set up the BlackBerry project in the JDE environment, and it launched the debugger successfully, can stop at the break point, and can single step;
then I go to the simulator folder of the BlackBerry ( in my pc is C:\Program Files\Research In Motion\BlackBerry JDE 4.5.0\simulator), I found there not only .cod file and .jad file, but also found
 .debug, .jar, and .alx file. I realize this might be the reason: you need to copy .debug files in the simulator! In my old build script, I only copied jad and cod files, did not copy .debug files into the simulator.

So I modified my ant build script, make sure it will copy all the rapc generated files into the simulator folder, which includes:
  - .jad and .jar file, I don't know why JDWP needs jar file, but it works any way;
  - debug file, like .debug and xxx-1.debug, xxx-2.debug, etc;
  - .cod file
  - .alx file

The ant script snippet look like this:
1.  ANT target: copy files.  Since I used bb-ant, this target means copy all the file contains the project name under \bbant folder to the simulator folder.

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


2. ANT target: launch blackerry debugger, this target will launch the blackberry JDWP remote server and blackberry simulator.

<target name="bb-debug" depends="copyBBSimulator" if="do.rapc">
    <delete file="${preprocessed.dir}/.timestamp"/>
    <parallel>
          <java jar="${platform.home}/bin/JDWP.jar" fork="true" dir="${platform.home}/bin">
            <jvmarg value="-Xmx128M"/>
          </java>

          <sequential>
              <sleep seconds="5"/>
              <antcall target="nbdebug"/>
         </sequential>
   </parallel>
</target>


At this time I use the new ant script and press the debu button in NetBeans, this time it works!!! It takes a longer time to launch the debugger and simualtor, but finally the app stops at the break point, and it allows me to step in and step out.
Another thing I like to mention is for the system outputs, you can only see them from the BlackBerry JDWP output window, you can not see them from the NetBeans output window.


For your reference, I post the whole build script and source code:
1. bb-build.xml

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


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

   <target name="run" depends="init,jar,bb-run,cldc-run" />
   <target name="debug" depends="init,jar, bb-debug, cldc-debug" />


   <target name="cldc-run" unless="do.rapc" >
       <nb-run jadfile="${dist.dir}/${dist.jad}" jarfile="${dist.dir}/${dist.jar}" jadurl="${dist.jad.url}" device="${platform.device}" platformhome="${platform.home}" platformtype="${platform.type}" execmethod="${run.method}" securitydomain="${evaluated.run.security.domain}" commandline="${platform.runcommandline}" classpath="${platform.bootclasspath}:${dist.dir}/${dist.jar}" cmdoptions="${run.cmd.options}"/>
   </target>

   <target name="cldc-debug" unless="do.rapc">
       <delete file="${build.dir}/.timestamp"/>
                <parallel>
                    <nb-run debug="true" debugsuspend="true" debugserver="true" debuggeraddressproperty="jpda.port" platformtype="${platform.type}" platformhome="${platform.home}" device="${platform.device}" jadfile="${dist.dir}/${dist.jad}" jarfile="${dist.dir}/${dist.jar}" execmethod="${run.method}" securitydomain="${evaluated.run.security.domain}" commandline="${platform.debugcommandline}" classpath="${platform.bootclasspath}:${dist.dir}/${dist.jar}" cmdoptions="${run.cmd.options}"/>
                    <sequential>
                        <sleep seconds="5"/>
                        <antcall target="nbdebug"/>
                    </sequential>
                </parallel>
   </target>

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

<typedef resource="bb-ant-defs.xml" classpath="bb-ant-tools.jar" />
  <target name="bbbuild" description="blackberry build" depends="init" if="do.rapc">
          <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">
          <src>
             <fileset file="${dist.dir}/${dist.jar}" />
          </src>
          <jdp title="${app.title}" vendor="${app.vendor}" version="${app.version}" type="cldc" icon="${app.icon}" runonstartup="false" >
          </jdp>
          </rapc>

        <!-- 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}">
               <codset>
                    <fileset dir="${dist.dir}/bbant" includes="*.cod">
                   </fileset>
               </codset>
           </application>
       </alx>
       <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">
        </fileset>
      
       </jadtool>
 </target>

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

    </copy>

</target>

<target name="bb-run" depends="copyBBSimulator" if="do.rapc">
    <exec os="Windows NT Windows 95 Windows 98 Windows 2000 Windows XP"
             dir="${platform.home}/simulator"
            executable="${platform.home}/simulator/${platform.device}.bat"
        failonerror="true" resolveExecutable="true"/>
</target>

<target name="bb-debug" depends="copyBBSimulator" if="do.rapc">
    <delete file="${preprocessed.dir}/.timestamp"/>
    <parallel>
          <java jar="${platform.home}/bin/JDWP.jar" fork="true" dir="${platform.home}/bin">
            <jvmarg value="-Xmx128M"/>
          </java>

          <sequential>
              <sleep seconds="5"/>
              <antcall target="nbdebug"/>
         </sequential>
   </parallel>
</target>

2. build.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project [
       <!ENTITY bb-common SYSTEM "bb-build.xml">
]>
<project name="helloLWUIT" default="jar" basedir=".">
    <description>Builds, tests, and runs the project .</description>
    <import file="nbproject/build-impl.xml"/>
    <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" />

     &bb-common;
</project>



3. source code project: helloLWUIT:
  Here
  Environment requirements: please refer Shai's blog for blackberry to set up the project environment


Next Steps:
  1. Figure out how to debug blackberry application on the actual device inside NetBeans;
  2. Make sure how to read attribute in the jad file in the BlackBerry CLDC application;
  3. How to easier copy the customized attribute in the midlet jade file to the BlackBerry CLDC jad file.

References:
  1. http://www.xenglobaltech.com/blackberry/index.php?option=com_content&view=article&id=45&Itemid=40
  2. http://supportforums.blackberry.com/t5/Java-Development/Building-Blackberry-apps-with-Netbeans/td-p/431612


Monday, June 21, 2010

Professional Devlelopers require professional managers

I realize that there are similarities between software developers and software managers:
- Developers manage codes, managers manage people;
- Developers resolve bugs in the codes, managers resolve bugs among the people;
- A developer has to make decisions all the time, compromise his solution with different constraints; A manager has to make decision all the time based on people, schedule and budget;
- A professional developer always choose the right framework and right tool for the task, a professional manager choose the right person to do the  right thing;
- A professional developer improves his code by keep constantly refactoring and reflection, a professional manager keep improving his team;
- A professional developer always follows the software principles and design patterns to guide his work, deliberately improve his skills, keep finding new tools;  A professional manager will follow the agile patterns and other principles and philosophy (ex. The Art Of War) to guide his work, his has the clear picture of his vision, and keep finding the new process to improve the productivity of his team.

But in reality,  unfortunately in my career so far I haven't found a manager which I can regard as a professional manager.  Most of them I call them "event dispatcher". They  care more about passing the command down to developers, focus more on his boss instead of developers, they don't care how developer finish it nor be interested in it. I even could not find his strategy of management. I really want he can show me how he solved a difficult issue creatively and beautifully, just like I saw my coworker solve an issue by using a pattern or a tool, but so far I haven't seen once.

Right now I know the reason why software industry is still primitive, why there are so many unprofessional developers, because most of our managers are unprofessional.  If a manger dos not know the principles and rules of software, how can he manage well for his team?  If a manager does not care his developer, how can his developer has motivation to become better?

A good manager for a software team just as a good coach for a soccer team. Professional developers do require professional managers.

Thursday, May 27, 2010

LWUIT blackberry ANT script refactored 2

Last time I posted the refactored blackberry build script, today I made some other small improvements:

1. Add a "run" target, support running both blackberry and regular J2ME simulator.
<target name="run" depends="init,jar,bb-run,cldc-run">
     
    <target name="cldc-run" unless="do.rapc" >
       <nb-run jadfile="${dist.dir}/${dist.jad}" 
                  jarfile="${dist.dir}/${dist.jar}" jadurl="${dist.jad.url}" 
                  device="${platform.device}" platformhome="${platform.home}" 
                  platformtype="${platform.type}" 
                  execmethod="${run.method}" 
                  securitydomain="${evaluated.run.security.domain}" 
                  commandline="${platform.runcommandline}" 
                  classpath="${platform.bootclasspath}:${dist.dir}/${dist.jar}" 
                  cmdoptions="${run.cmd.options}"/>
    </target>
    
    <target name="bb-run" depends="copyBBSimulator" if="do.rapc">
     <exec os="Windows NT Windows 95 Windows 98 Windows 2000 Windows XP"
             dir="${platform.home}/simulator"
            executable="${platform.home}/simulator/${platform.device}.bat"
        failonerror="true" resolveExecutable="true"/>
   </target>
   The above code snipplet means, when user press "run" button in NetBeans IDE, if the current configuration is blackberry, then launch the blackberry simulator, else launch the regular J2ME simulator.

2. Separate the blackberry specific ant targets into aother build script file, make the build file generic and easier to use.
At the beginning I tried to put all the targets in a separate file: bb-build.xml, then import it in the build.xml, but I found the netbeans custom targets like post-init, post-jar are not called, it seems these targets needs to be explicit specified in the build.xml.
I found another solution - embedding the xml snippet in the build.xml using XML entity, based on this link:
 for exmaple, like this:
<!DOCTYPE project [
       <!ENTITY bb-common SYSTEM "bb-build.xml">
    ]>

   &bb-common;
 The above snippet is equivlent of copying the code from bb-build.xml into build.xml.  

Benefits:
The new refactored build script becomes generic, does not specific to lwuit project, can apply to any blackberry CLDC app.

I attach the full build script here, and you can use them as template for your blackberry build script in your NetBeans project.
1) build.xml
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE project [
       <!ENTITY bb-common SYSTEM "bb-build.xml">
]>

<project name="myApp" default="jar" basedir=".">
    <description>Builds, tests, and runs the project .</description>
    <import file="nbproject/build-impl.xml"/>
   
    <property name="app.title" value="myApp" />
    <property name="app.version" value="1" />
    <property name="app.icon"   value="/icon.png" />
    <property name="app.vendor" value="foo" />

    &bb-common;
 </project>

2) bb-build.xml, the blackberry build script snippet
<target name="post-init">
          <available file="${platform.home}/bin/rapc.exe" property="do.rapc" />
          <condition property="jpda.port" value="8000">
              <isset property="do.rapc">
              </isset>
         </condition>
         
  </target>


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

   <target name="run" depends="init,jar,bb-run,cldc-run">

</target>

   <target name="cldc-run" unless="do.rapc" >
       <nb-run jadfile="${dist.dir}/${dist.jad}" jarfile="${dist.dir}/${dist.jar}" jadurl="${dist.jad.url}" device="${platform.device}" platformhome="${platform.home}" platformtype="${platform.type}" execmethod="${run.method}" securitydomain="${evaluated.run.security.domain}" commandline="${platform.runcommandline}" classpath="${platform.bootclasspath}:${dist.dir}/${dist.jar}" cmdoptions="${run.cmd.options}"/>
   </target>

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

<typedef resource="bb-ant-defs.xml" classpath="bb-ant-tools.jar" />
  <target name="bbbuild" description="blackberry build" depends="init" if="do.rapc">
          <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">
          <src>
             <fileset file="${dist.dir}/${dist.jar}" />
          </src>
          <jdp title="${app.title}" vendor="${app.vendor}" version="${app.version}" type="cldc" icon="${app.icon}" >
          </jdp>
          </rapc>

        <!-- 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}">
               <codset>
                    <fileset dir="${dist.dir}/bbant" includes="*.cod">
                   </fileset>
               </codset>
           </application>
       </alx>
       <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">
        </fileset>
       </jadtool>
 </target>

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

</target>

<target name="bb-run" depends="copyBBSimulator" if="do.rapc">
    <exec os="Windows NT Windows 95 Windows 98 Windows 2000 Windows XP"
             dir="${platform.home}/simulator"
            executable="${platform.home}/simulator/${platform.device}.bat"
        failonerror="true" resolveExecutable="true"/>
</target>

Next Step:
Add a target support debugging blackberry Application inside the NetBeans IDE.

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">
              </isset>
         </condition>
    </target>

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

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

<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">
          <src>
             <fileset file="${dist.dir}/${dist.jar}" />
          </src>
          <jdp title="${app.title}" vendor="${app.vendor}" version="${app.version}" type="cldc" icon="${app.icon}" >
          </jdp>
          </rapc>

        <!-- 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}">
               <codset>
                    <fileset dir="${dist.dir}/bbant" includes="*.cod">
                   </fileset>
               </codset>
           </application>
       </alx>
       <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">
        </fileset>
       </jadtool>
 </target>

<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" />
            </fileset>
    </copy>
</target>

<target name="bb-run" depends="copyBBSimulator">
    <exec os="Windows NT Windows 95 Windows 98 Windows 2000 Windows XP"
             dir="${platform.home}/simulator"
            executable="${platform.home}/simulator/${platform.device}.bat"
        failonerror="true" resolveExecutable="true"/>
</target>

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:




      configs.BlackBerryTouch.platform.device=9500
      In 4.5, the device name is 8320, like this:




      configs.BlackBerry.platform.device=8320
    Update: Sample project
    Please click here to download the sample project.

    Tuesday, May 4, 2010

    Why should managers embrace agile?

    When you look at the articles about Agile/Scrum/Lean, they are all about self-organizing, decentralized, democracy, etc.Those are welcomed by the developers but conflict with managers benefit, because our conventional management is about hierarchy, command and control, chasing the power, the manager cares more about how his boss think instead of how his subordinates think. For managers, embracing agile means loosing power, weaken their benefit, that is why manager don't like agile.
    On the other hand, agile is about changing, changing means risky, but the managers care more about the stability of their position, their power, they don't have much enthusiasm to make changes.

    So we have a conclusion that developer and manager like two different classes, developers like agile because it brings them benefits; managers dislike agile because it will lose managers' benefit. The progress of agile movement is quite similar with the progress of human's democracy movement.

    This sounds discouraging, but this is the fact, that will explain why agile has not become mainstream after 10 years of its movement.Then what can we do? Try to educate as many developers as possible, gradually if agile principles are accepted by most people, then it is quite possible to force the managers make changes. This will take time, but please remember that the manager won't truely embrace agile,just like no ruler will truely embrace democracy.

    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".

    Wednesday, April 21, 2010

    Build LWUIT BlackBerry CLDC application

    Last week finally I have time to review the Shai's blog  about building LWUIT project on Blackberry platform. At first I tried to follow the step by step in his article, but I am  disappointed because of so many errors of his build.xml. After a couple hours fix, I still could not make the build, so I decided to give up Shai's script, and choose an easier way.
    My solution is just using NetBeans and BlackBerry JDE, it is much easier to build a demo.
    Here is the step by step description:

    Build LWUIT BlackBerry library
    1. Checkout the LWUIT source code from SVN, (https://lwuit.dev.java.net/svn/lwuit/trunk), the LWUIT source includes the NetBeans BlackBerry port project.
    2. Install JDE 4.7 and Configure BlackBerry support in NetBeans, you can follow this link to configure;
    3. Start NetBeans, open the "BlackBerryPort" poject under the "BlackBerry" sub folder of lwuit project source, there are two configuration for this project, the default is for non touch screen build, the "touch" configuration is for touch screen device like STORM.for non touch device, just choose DefaultConfiguration, build the project, you will see BlackberryPort.jad and BlackberryPort.jar in \dist folder, they are the LWUIT library for BlackBerry devices;

    Build LWUIT Demo into BlackBerry CLDC application
    1. Open Blackberry JDE, create a new workspace, called "lwuitdemo.jdw";
    2. add a new project under the workspace, name it "lwuit",  choose its Application type as "Library", import the BlackberryPort.jad and BlackBerryPort.jar file into the project. then build it, the JDE will generate lwuit.cod and update BlackBerryPort.jad file.  
    3. download LWUIT 1.3 from sun website, it comes with the famous LWUITDemo source code, open the LWUITDemo project using NetBeans, build it;
    4. add another new project in the JDE workspace, name it "lwuitdemo", choose application type as "CLDC Application";
       set up the correct value with title, version and Vendor information;
       next you need to add project dependency to "lwuit" project, right click project, choose "Project Dependencies..", choose "lwuit project";
       copy all the java source code from LWUITDemo src folder, add them into the project, ignore those png files and .res files;
       go to  build\preverified folder, copy all the png files and .res files, add them into your JDE project;
       right click icon.png, choose "use as application icon";
    5.Modify the UIDemoMIDlet.java
    Made following changes:
    make sure the UIDemoMIDlet extends from uiApplication, like this:
    public class UIDemoMIDlet extends net.rim.device.api.ui.UiApplication  {...}
    
    Add the following methods, copied from Shai's blog:
    public static void main(String[] argv)
        {
            new UIDemoMIDlet().startApp();
        }
     
        public void notifyDestroyed()
        {
            System.exit(0);
        }
     
        public void platformRequest(String s) {
           net.rim.blackberry.api.browser.Browser.getDefaultSession().displayPage(s);
        }
        
    In startApp() method, comment out the following statement, because non touch device need to disable the virtual keypad:
    // VKBImplementationFactory.init();
    
    6. Build the lwuitdemo project.
    7. Sign both lwuitdemo.cod and lwuit.cod fle, load them both into your blackberry device.
    8. If you want to provide OTA download, that is a litttle bit tricky:
       You need to download two artifacts, one for lwuit libray, the other for lwuitdemo, which means user need to download two links:
       1) lwuti library:
          BlackBerryPort.jad
          lwuit.cod
          lwuit-1.cod
          ...
       2) lwuitdemo application:
          lwuitdemo.jad
          lwuitdemo.cod
          lwuitdemo-1.cod
          ...


    You can download the whole source code of the JDE project from here.


    In future I would like to share:
      1. Provide an updated build.xml based on Shai's blog;
      2. provide how to build LWUITDemo MIDlet project for BlackBerry device.