Converting From J2ME

Version 23, last updated by complier at 2011-03-20

All these steps are to be done under Eclipse on Windows, I don't know whether they work using a command line build, or Eclipse under any other platform. It builds against the current version of Andriod, which is 1.0 r2 at the time of writing. [some comments added in parenthesis, I hope that is ok, otherwise feel free to remove - Hans]

Create a new workspace

In Eclipse select Switch Workspace -> Other... and create a new workspace somewhere.

Technically you don't need to do this, but the instructions assume you have so give it a go.

[If you just want to test J2AB, the easiest is to download it into a directory parallel to the new workspace (e. g. using "svn checkout http://svn.assembla.com/svn/j2ab").

If you want to contribute, j2ab should also be an Eclipse project linked to the repository. In this setup, you MUST use a different workspace because Eclipse does not allow the re-use of part of a project as a new project. One way to do this is to create a third workspace just for J2AB: File->Switch Workspace->Other..., create "j2abWS", Window->Open Perspective->Other.., select "SVN Repository Exploring", right click "New..." in the left panel, use the URL "http://svn.assembla.com/svn/j2ab", right click the URL and select checkout.

Now you can go back to the workspace you created above. Use the j2abWS workspace only to upload your changes to the J2AB repository.]

Create a new Android project in Eclipse

You should have the android plug in for Eclipse installed. If you do, then there will be an option to create an Android project under the new project menu. Select that option.

You want to choose the option to "Create project from existing source" then navigate to the j2ab_adapter folder. [use .../j2ab/trunk/j2ab_adapter NOT e. g. .../j2ab/m5-r15/j2ab_adapter, call the project j2ab_adapter]

You should have a skeleton project created for you, but it will have compile errors.

Create a new non-Android project in Eclipse

Create a normal Java project in Eclipse now. Again you want to choose "Create project from existing source" and navigate to the j2ab_midpapi folder. [call the project j2ab_midpapi]

In the properties of the Android project (j2ab_adapter), make that project depend on this project. [select the project, open Project->Properties, select "Java Build Path", open the "Projects" tab, click "Add.." and select the j2ab_midpapi project]

Right click on the project and go to properties, you'll need to remove the standard Java library from the classpath and add in a dependency on the android.jar file from your Android SDK. [open the "Java Build Path" as explained above, open the "Libraries" tab, highlight and remove, then "Add External JARs...", browse to one of the Android.jar under e. g. android-sdk-mac_x86/platforms/android-8/Android.jar]

The compile errors should disappear.

Import your project

Assuming you've been developing your MIDP application in Eclipse, find the project(s) for the application and "Import" them into this workspace. [open File->Import..., open "General", select "Existing projects into workspace", next, browse for the project root directory, select "Copy projects into workspace"]

In the properties of the Android project (j2ab_adapter), make that project depend on the project(s) you just imported.

Also, for the project(s) you just imported, make those projects depend on the j2ab_midpapi project and remove any dependencies on the jar files that come with the WTK (very important, you will get verification errors if you don't do this). [also add the same Android.jar as external JAR, as above]

See Errors? Fix 'em.

The bridge is incomplete, so you may find you use classes or methods that aren't implemented yet. Eclipse will make this pretty obvious as you'll have compile errors with your classes. You have two options

1) remove the dependency on the unimplemented parts of the API, or

2) implement the bits of the API you need (see below)

Your choice.

Tell the Bridge About your App

This file location will probably change in future, but look for the file

j2ab_bridge/src/jad.properties

[now in j2ab_adapter/src/jad.properties]

This is the equivalent of the jad file for a normal midlet.  Any information you want to have available through the MIDlet.getAppProperty method should be put in here, however you MUST specify one value, the class name of your MIDlet. For example

midlet=name.of.my.midlet.package.NameOfMyMIDlet
param1=The first application parameter
param2=The second application parameter 
xyz123=The third application parameter

GO! 

OK, so you're done. Run up the class J2ABMIDletActivity in the Android debugger (under Eclipse) and see what happens. Hopefully you'll see your application in all its glory.

[Do Windows->Show View->Other... and select Android->LogCat for a more detailed trace]

Any System.out.println statements should be appended to the Android logger, so keep an eye on it.

Implement the Missing Bits of the Adapter

Take a look at the existing code to get an idea of how to implement the missing parts. Of particular note are the lcdui widgets, which use the Android layout xml to configure them. You don't have to do this, but it adds a certain amount of configurability to the style that you wouldn't otherwise enjoy.

Apart from that, lay off the static factory pattern as much as you can while still being faithful to the J2ME spec.

Turn on screen scaling (optional)

If your app doesn't handle multiple screen resolutions, you might want to consider turning on screen scaling. In your jad.properties file add the following lines

j2ab_scaleCanvas=true

j2ab_preferredCanvasWidth=240

j2ab_preferredCanvasHeight=320

Obviously the canvas width and height can be whatever value you think best suits your app. This will make every Canvas subclass think it's running in a window of those dimensions and scale the rendering operations to fill the screen. There may be performance trade-offs for doing this and the scaling may not be high-quality depending on your hardware. 

Make a Splash Screen

For technical reasons, we've introduced a splash screen into the loading phase of the bridge. This is used to ascertain the size of the client portion of the screen. You can, however, change the splash screen to look like anything you want in res/layout/splash.xml file, provided it has the attributes

android:layout_width="fill_parent"
android:layout_height="fill_parent"

If that isn't appealing, work out how to return the portion of the screen usable to Display implementations at the time of creation of the Display and fix up the code.


2 Comments

    Attach files by dragging & dropping, selecting them from your computer, pasting from the clipboard or more
    [Ctrl/Cmd] + [Enter]