Setup up Eclipse for BlackBerry development

来源:互联网 发布:node.js是后端吗 编辑:程序博客网 时间:2024/06/05 20:37

  注:此文章来自.blackberryforums.com,文中的build.xml是存在问题,配置完成不能将文件发布给模拟器

 

This is yet another post describing how to setup Eclipse for BlackBerrydevelopment work. When I was getting started on my BlackBerry project,I must have read a dozen such posts and articles. None of them gave meall that I needed to get everything working. Hopefully this post willdo just that.

I do ask that if you find an error, or something does not work, please post the problem, and hopefully the solution.

However first a caveat. Just 2 weeks ago, when I was downloadingprograms and setting things up, I was downloading the latest versionsof the programs that were available at the time. Now, as I write thispost, some of the programs have newer versions available. I will writethese instructions using the versions I have installed. I hope therewon’t be any significant differences with the newer versions.

1. Download and Install

1.1. JDK from http_://java.sun.com/javase/downloads/index.jsp. I have JDK 6 Update 6, which is the current (non-beta) version.

1.2. BlackBerry JDE from http_://na.blackberry.com/eng/developers/downloads/jde.jsp . I have version 4.3.0. As I write this, the latest is 4.5.0. (There is no 4.4.)

1.3. BlackBerry 8820 Simulator. The 4.3.0 JDE came with simulators forthe BlackBerry 8120 and 8130. I had a problem with the cursor gettingstuck in TextBox fields, in these simulators. So I got the 8820simulator which does not have this problem. Go to http_://na.blackberry.com/eng/developers/downloads/simulators.jspand click the link “Download a BlackBerry Device Simulator”. Then inthe pull down, select “BlackBerry Device Simulators v4.2.2”.

1.4. Eclipse from http_://www.eclipse.org/downloads/ . I have version 3.3.2. As I write this, the latest is 3.4. Also note that I have the Eclipse Classic package.

1.5. bb-ant-tools from http_://sourceforge.net/project/showfiles.php?group_id=195339. I have version 0.7 which is still the latest version.

2. Other useful downloads

2.1. Mobile File Manager from http_://www.rovemobile.com/products/freeproducts/mfm/features/.This awesome program allows you to view and manage the file system onthe BlackBerry, edit files and even setup an FTP from the BlackBerry toa server. I did not use the FTP feature.

2.2. BlackBerry Desktop Manager from https_://www.blackberry.com/Downloads/browseSoftware.do.Some variant of this program comes with the BlackBerry when you buy it.But if you don’t have one, you can get it here. You need this toinstall programs to a physical BlackBerry. I have a variant from Roxiothat also allows me to transfer files between the BlackBerry and the PC.

2.3. Ant from http_://ant.apache.org/. This will let you test Ant scripts if you want to get into writing them. Eclipse also has a built-in Ant interpreter.

3. Eclipse Setup

3.1. Workspace Preferences
3.1.1.Install the JRE with Javadocs. Note that the following steps assumethat in your Eclipse workspace, you will only be doing projects for theBlackBerry. Projects for J2SE or other environments will need to be putinto a different Eclipse workspace.
3.1.1.1. Click Window > Preferences from the main menu in Eclipse
3.1.1.2. Click Java > Installed JREs
3.1.1.3. Click Add.
3.1.1.4. Fill the fields:
3.1.1.4.1. JRE type = Standard VM
3.1.1.4.2. JRE name = ‘RIM JRE’
3.1.1.4.3. JRE home = path to the JDK (eg. C:/jdk1.6.0_06)
3.1.1.4.4. Default VM Arguments leave blank
3.1.1.5. In the JRE system libraries box, select all of the entries and click Remove.
3.1.1.6. Click Add External JARs and navigate to the lib directory of the BlackBerry JDE (eg. C:/jde/lib).
3.1.1.7. Select the file net_rim_api.jar.
3.1.1.8. Back in the JRE system libraries box, select the new entry for net_rim_api.jar and click Javadoc Location
3.1.1.9. In the Javadoc window, use the Browse button to navigate tothe BlackBerry JDE api directory (eg. C:/jde/docs/api). Click OK toclose the Javadoc window.
3.1.1.10. Click OK to close the Add JRE window.
3.1.1.11. In the Installed JREs window, check the box next to the new RIM JRE entry.
3.1.1.12. Click on the prior entry in this list (for jre1.6.0_06) and click the Remove button.
3.1.1.13. Click OK to close the Preferences window.
3.1.1.14. I have seen the above settings not take the first time. So goback into Preferences and make sure they took. If not, do them again.I’ve never seen it not take the second time.
3.1.2. Set the JDK Compliance Level – Since the JRE westarted with was version 6, the default compliance setting will be 6.0.But the BlackBerry JDE is not at that level.
3.1.2.1. Click Window > Preferences from the main menu in Eclipse
3.1.2.2. Click Java > Compiler
3.1.2.3. Change the ‘Compiler compliance level’ to 1.4.
3.1.2.4. Click OK to close the Preferences window.
3.1.3. Setup a Detail Formatter for the String class.
3.1.3.1. Click Window > Preferences from the main menu in Eclipse
3.1.3.2. Click Java > Debug > Detail Formatters
3.1.3.3. Click Add
3.1.3.4. Fill the fields:
3.1.3.4.1. Qualified type name = java.lang.String
3.1.3.4.2. Detail formatter code snippet = toString()
3.1.3.5. Click OK
3.1.3.6. In the Detail Formatters window, select the ‘In detail pane only’ radio button.
3.1.3.7. Click OK to close the Preferences window.
3.1.4. Setup the formatting styles you like
3.1.4.1. Click Window > Preferences from the main menu in Eclipse
3.1.4.2. Click Java > Code Style > Formatter
3.1.4.3. Click the New button and enter a name. Click OK
3.1.4.4. In the Profile window that pops up, go through the tabs and configure the styles you like.
3.1.4.5. Click OK to close the Profile window
3.1.4.6. Click OK to close the Preferences window.
3.1.5. Increase debugger timeouts
3.1.5.1. Click Window > Preferences from the main menu in Eclipse
3.1.5.2. Click Java > Debug
3.1.5.3. Increase the Lauch timout by a factor of 10 (eg. set to 200000)
3.1.5.4. Increase the Debugger timeout by a factor of 5 (eg. set to 15000)
3.1.5.5. Click OK to close the Preferences window.
3.1.6. Other settings
3.1.6.1. Click Window > Preferences from the main menu in Eclipse
3.1.6.2. Click General > Workspace
3.1.6.3. Uncheck ‘Build automatically’
3.1.6.4. Check ‘Refresh automatically’
3.1.6.5. Check ‘Save automatically before build’
3.1.6.6. Click OK to close the Preferences window
3.2. Project Setup. The default situation forEclipse is to have the source code for a project located in thedirectory tree that you specified for the workspace. However I alreadyhad the source files located elsewhere. So I needed to setup theproject differently.
3.2.1. If you are still looking at the Welcome window, click the Workbench button, located on the far right.
3.2.2. Click File > New > Project from the main menu in Eclipse
3.2.3. Give the project a name
3.2.4. Select ‘Create new project in workspace’. The source files areelsewhere, but the other project files can be in the workspace.
3.2.5. Select ‘Use default JRE (Currently ‘RIM JRE’)’
3.2.6. Select ‘Create separate folders for source and class files’
3.2.7. Click Next
3.2.8. Click ‘Link additional source’.
3.2.8.1. Use the browse button to locate the external source folder.
3.2.8.2. Give this source tree a name (in the Folder name field)
3.2.8.3. Click Next
3.2.8.4. If there are files in that tree that need to be excluded, thenadd Exclusion patterns. For example, I needed to exclude:
3.2.8.4.1. **/CVS/**
3.2.8.4.2. *.html
3.2.8.4.3. *.txt
3.2.8.5. Click Finish
3.2.9. If you have additional source trees for theproject, then in the Java Settings window, click ‘Link additionalsource’, and repeat the source configuration for each such source tree.
3.2.10. In the New Java Project window select the ‘Order and Export’ tab.
3.2.11. Check the box next to ‘JRE System Library [RIM JRE]’
3.2.12. Click Finish
3.3. Add bb-ant-tools to the project. This library is needed to build your project’s code for the BlackBerry.
3.3.1. In the Eclipse Package viewer, right mouse click on the Project name, and click New > Folder.
3.3.2. Enter the new Folder name as lib. Verify that the parent folder is the project.
3.3.3. Click Finish
3.3.4. In the Package viewer, right mouse on the new lib folder and click Import.
3.3.5. In the Import window, click General > File System.
3.3.6. Click Next
3.3.7. Use the Browse button to locate the directory containing thebb-ant-tools.jar file that you downloaded earlier. Then in the rightpane, check the box next to the bb-ant-tools.jar file. Verify that the“Into folder” field is the lib folder.
3.3.8. Click Finish
3.3.9. In the Package viewer expand the lib folder and right mouse onthe bb ant tools.jar file. Click Build Path > Add to Build Path.
3.4. Add a build.xml file to the project. This file is theAnt script that will be used to build your project’s code for theBlackBerry.
3.4.1. In the Package viewer, right mouse and click New > File.
3.4.2. Enter the new file name as build.xml. Verify that the parent folder is the project.
3.4.3. Click Finish
3.4.4. Copy the following into the build.xml file.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<project name="AudioTests" default="buildRIM" basedir=".">
<taskdef resource="bb-ant-defs.xml" classpath="lib/bb-ant-tools.jar" />

<property name="jdehome" value="C:/jde" />
<property name="javahome" value="C:/jdk1.6.0_06"/>
<property name="simulator" value="${jdehome}/simulator" />
<property name="bin" value="${jdehome}/bin" />

<property name="output" value="ProjectName" />
<property name="title" value="Project Title" />
<property name="vendor" value="company name" />
<property name="version" value="0.1" />
<property name="description" value="my BlackBerry program" />

<target name="buildRIM" description="Composes RIM" depends="clean">
<rapc jdehome="${jdehome}" jdkhome="${javahome}" output="${output}" >
<jdp title="${title}" vendor="${vendor}"
version="${version}" description="${description}"
runonstartup="false" type="cldc" />
<src>
<fileset dir="C:/srcPath1">
<include name="**/*.java" />
<include name="resources/**/*.*" />
</fileset>
<fileset dir="srcPath2">
<include name="**/*.java" />
<include name="resources/**/*.*" />
</fileset>
</src>
</rapc>
<copy todir="${simulator}" overwrite="true">
<fileset dir=".">
<include name="*.cod" />
<include name="*.debug" />
<include name="*.cso" />
</fileset>
</copy>
</target>

<target name="clean">
<delete>
<fileset dir="bin" includes="**/*.class"/>
<fileset dir=".">
<include name="*.cod"/>
<include name="*.cso"/>
<include name="*.debug"/>
<include name="*.jad"/>
<include name="*.jar"/>
<include name="*.rapc"/>
</fileset>
</delete>
</target>

</project>
3.4.5. Edit the property values at the top of this file, and alsoadjust the fileset directory paths in the BuildRIM target, for thesource trees in your project.
3.4.6. Save and close the editor for the build.xml file.
3.5. Setup custom builders. These tell Eclipse to run the Ant script in build.xml when you build or clean the project.
3.5.1. In the Package viewer, right mouse on the project name and click Properties.
3.5.2. Click Builders
3.5.3. Click New
3.5.4. Click Ant Builder
3.5.5. Click OK
3.5.6. Change the name of the new builder to RIM_Builder.
3.5.7. On the Main tab, under the Buildfile text field, click the Browse Workspace button.
3.5.8. Select build.xml and click OK
3.5.9. On the Targets tab, click the ‘Set Targets’ button to the right of the ‘During a “Clean”’ field.
3.5.10. Uncheck the box next to the ‘buildRIM’ target and check the box next to the ‘clean’ target.
3.5.11. Click OK
3.5.12. Click OK again to close the new builder properties window.
3.5.13. In the Builders window, click on the new builder (RIM_Builder)and click the Up button, moving the new builder to the top of the list.
3.5.14. Click the OK button closing the Builders window.
3.6. Start and setup the JDWP debugger interface. Assumingyou have a project with some source code that has successfully built,the next steps will get the project into the simulator and ready fordebugging. One of the applications that was installed with the RIM JDEis JDWP.
3.6.1. Start JDWP
3.6.2. On the Simulator tab, Verify that the Profile selected matches the simulator to be used. If not, click the New button.
3.6.3. On the Advanced tab, use the Browse button to navigate to thesimulator’s directory (the model number.bat file must be in thisdirectory – eg. 8820.bat).
3.6.4. Click the Edit button and set the command line to

Code:
F:/Java/BlackBerryJDE/simulator/fledge.exe
/app-param=JvmAlxConfigFile:8820.xml
/pin=0x2100000A
/data-port=0x4d44
/data-port=0x4d4e
/app-param=DisableRegistration
/app=Jvm.dll
/keep-lcd-on=true
/handheld=8820
/session=8820
3.6.5. On the General tab, check ‘Launch simulator when debugger connects’.
3.6.6. Click the Launch Simulator button and wait for the simulator to start.
3.6.7. Exit from the simulator (File > Exit) but do not exit the JDWP debugger interface.
3.7. Setup an Eclipse debugger configuration.
3.7.1. In the Eclipse Package viewer, right mouse on the project and click Debug As > Open Debug Dialog.
3.7.2. Right mouse on Remote Java Application, and click New.
3.7.3. Give the configuration a name, such as BB_Debugger.
3.7.4. Click the Debug button.
3.7.5. Eclipse will build the project and will then connect to JDWP, which will start the simulator.
3.7.6. As the simulator starts, have the JDWP window raised to the topon your screen. This will let you see any pop ups from JDWP that saythe simulator can not find a file. If such pop ups occur, it istypically safe to click the “Don’t ask this again” button. So long asJDWP is running, it will not ask about that file again.
3.7.7. Close the simulator
3.8. Starting the simulator/debugger. After the debuggerconfiguration has been created, as described above, the debugger can bestarted as follows:
3.8.1. Locate the toolbar button whose tool tip is “Debug As…”.
3.8.2. Click the pull down arrow located on the right of that button.
3.8.3. At the top of the list will be the Debugger configurationcreated above (BB_Debugger). Click that item in the list to run thatconfiguration.

That pretty much does it for setting upEclipse for BlackBerry development. I do however have some other tipsthat I’ve learned.

BlackBerry Tips

1. Remove unwanted applications from the simulator. In the Simulatordirectory (eg. C:/jde/simulator) is a file called 8820.xml (assumingyou are looking at the 8820 simulator). Remove from this file the linesthat specify applications you don’t care to have loaded in thesimulator. This will remove clutter from the simulator’s main menu. Itmay also speed up loading of the simulator, but not by much.

2. Remove your projects from the simulator. Both of the following steps must be done.

2.1.In the simulator directory (eg. C:/jde/simulator), there are 3 filesfor each of your projects. Delete all 3 of them. They are:
2.1.1. projectName.cod
2.1.2. projectName.cso
2.1.3. projectName.debug
2.2. Run the simulator
2.2.1. Select the Options icon
2.2.2. Select Advanced Options
2.2.3. Select Applications
2.2.4. Click the menu button (the Insert key on the PC keyboard)
2.2.5. Select Modules
2.2.6. Scroll through the list to find your project
2.2.7. Click the menu button (Insert key)
2.2.8. Select Delete

3. Prevent the simulator’s display from going black.

3.1. In the simulator window, select View > Keep LCD On. This will affect only the current simulator session
3.2. In the JDWP window, on the Simulator tab > View tab, check ‘Always keep LCD backlight on’.

4. Installing your project to a physical BlackBerry

4.1. Connect your BlackBerry to your PC with the USB cable.
4.1.1.I get a pop up on the BlackBerry when I connect it to the PC, asking ifI want to enable Mass Storage Mode. No matter what I answer (yes or no)the connection to the PC gets broken. So I just don’t answer the pop upuntil after I’m finished with the connection. After removing the USBcable the pop up goes away on its own.
4.2. On your PC, locate the cod file for your project. Itwill be in the Eclipse project directory, which is typically under theWorkspace directory.
4.3. Open a cmd window in that directory and run the following command. (javaloader comes with RIM’s JDE).
Code:
javaloader -usb load projectName.cod

5. Removing your project from a physical BlackBerry

5.1. Connect your BlackBerry to your PC with the USB cable.
5.2. Open a cmd window in that directory and run the following command. (javaloader comes with RIM’s JDE).
Code:
javaloader -usb erase -f projectName.cod

6. Edit files and navigate the file system on the BlackBerry (physical or simulator).

6.1.Install the Mobile File Manager program to the simulator and/or thephysical BlackBerry. Use the BlackBerry Desktop Manager to install thisprogram.
6.2. Programs installed with the BlackBerry Desktop Manager are also removed using the BlackBerry Desktop Manager.

7. In Eclipse, name your project with a letter less than‘n’. When removing your project from the simulator, the modules listhas tons of RIM modules that start with “net_rim”. You want your modulebefore that long list of RIM modules.

8. Locate the PIN or IMEI numbers in a physical BlackBerry. Open Options > Status.

Eclipse Tips

1. Automatically add an import statement for an unknown type. Place cursor on the unknown type and hit Ctrl-Shift-m.
2. Organize the import statements – hit Ctrl-Shift-o
3. Have only one main() in your entire project; otherwise when you tryto start the project in the simulator, it may not run the desiredmain(). The simulator will give no indication of why it seems that yourprogram is not starting.

RIMs BlackBerry JDE Plug-In for Eclipse

I downloaded version 1.0.0.22 (the latest) of this plug-in. In a word,don't bother. According to RIM support, it only works with version3.3.1 of Eclipse, so it would not work with the 3.3.2 version that Ihave. It also will not work on Windows 2000 (which I also use).

Improvements Needed to the above Instructions

• It would be really nice if only changed java files were recompiled.My project currently has over 200 files and will have over 300 whenfinished.
• It would be nice to have a way to specify the class containing the desired main() to be run.

Acknowledgements

My thanks to the many authors whose posts and articles and answers tomy questions have helped me get this far. I'm sure you see your work inthe above. Thank you.