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.

5 comments:

  1. Hi.

    In order to make the script to launch the emulator, when adding the JDE platform into Netbeans, I need to call the device same name as the simulator batch file, correct?

    Regards.

    ReplyDelete
  2. yes, for example, for 8300, you need to launch 8300.bat

    ReplyDelete
  3. Hello Steve

    I created build script for my BlackBerry app based on your script. It works fine. Thank you!

    But I have on minor problem: The BlackBerry device does not show the application icon, that is specified in the 'rapc' tag. You configure it with:



    Where is this 'icon.png' located in your case? I tried nearly every place on the filesystem, but do not get it to work.

    Thanks for any help
    Christoph

    ReplyDelete
  4. Hi, Christoph,
    In my sample app, the icon.png is embedded in the lwuit project library, you can check that by unzip the BlackBerryPort.jar.
    If you want to display your own icon, you need to put the your icon png file in your app's class path. for example:
    1) you have a \res folder, put icon.png in the \res folder;
    2) in netbeans add \res folder to your project, right click project, choose properties, click Libraries and Resources, add the res folder.
    3) specify the icon file in your build.xml, remember add "/" in front of your file name.

    ReplyDelete
  5. Hi this was a big help you're doing a great work in BB apps development and I was reading your articale about ant script for BB and LWUIT but I was dealing with few issues:

    1. It run on sumilator using the .cod file but when run on device using .jar file it show error "attempt to use restrictd API".
    2. I did modified the script and added jde home and pasword for my sign key that I got from RIM.
    3. When I try to sign using the sign tool it work but when check files generated from the build that supposed to be signed it show that they are not signed yet.

    Regards and thanks aloooooooooooot ^_^!

    ReplyDelete