How to create a mobile oriented, multi-platform, shared library in Java?

I have a Java application that runs on BlackBerry (JDE 4.5). I want to transfer this application to Android and simultaneously support two applications. I can also port this application to other Java platforms (J2ME?).

I understand that a significant part of the code should be specific to each platform (user interface and other materials). But I also feel that many of the code can (should) be separated (domain related classes).

What is the best way to achieve this, and what are the disadvantages that should be avoided?

So far, I have been able to create a JAR with all of my shared classes that I have been able to integrate into my BlackBerry application (using preverify and rapc ). But:

+7
source share
5 answers

These days, the biggest stumbling block to code sharing is that the BlackBerry VM and Android VM support different versions of the Java language. BlackBerry uses a subset of Java 1.3, Android uses a subset of Java 1.5. (Aside, no platform implements a Java virtual machine, both use their own virtual machines. Java is used as a programming language. Java bytecodes must be converted to the corresponding native VM format before they can run on the platform.)

The biggest difference that you will find as a library developer is that BlackBerry lacks the things that were introduced in 1.5, very important things like generics and enumerations. Worse, collection classes are missing from BlackBerry. This is sad, but it has been so for a long time.

This means that in order to be truly portable, you must write in the lowest common denominator, which means using (very) old-style classes such as Hashtable and Vector that do not have generics, folding your own listings (as in the first issue of Effective Java) etc.

Or you create two libraries, a modern version for Android and a truncated version (with just the bare stuff you need) for BlackBerry.

It's hard to say what suits you.

+2
source

This will probably be tricky:

  • As you have already determined, the user interface code must be different for each platform.
  • There are significant differences between the Java SE / Android and Java ME platforms. For example, ME does not have a Collections framework or java.io or java.nio stacks.

It is difficult to predict from the information you provide, but there is a fair likelihood that you will spend more time fighting platform dependencies than you save by sharing the code base.

+4
source

Instead of packing your shared library, I would consider sharing a library project and its dependence on the process of building mobile applications. This will allow you to share the code base, but it was built by the appropriate builders for your target devices. With a little IDE mask and some attention to detail, you'll need to get errors before anything is sent.

Alternatively, set up your library project to use two separate collectors to collect errors. This will allow cleaner distribution, but you may run into problems trying to convince your IDE to consider the project as device-specific to identify problem areas.

You will likely support the lowest common denominator device (cough Blackberry) and abandon the additional features of a more extensive Java implementation on Android.

Unfortunately, the answer will be one of the experiments. Try it and see what happens.

+2
source

The article Porting Android Code to BlackBerry provides some detailed information on how to work with code shared between two platforms.

+1
source

it will be very difficult to create a shared library for BlackBerry and Android. if you want a simple method, create the application as a web application. using

phonegap with jQtouch

0
source

All Articles