Tuesday, November 4, 2008

Running Java ME applications on Android

Each new software platform, including Android, at the beginning is struggling with a small number of the available applications. This is why Google spent $10 million trying to attract developers to their Android Developer Challenge before G1 phone release. Taking advantage of an opportunity to run large number of existing Java ME applications may determine a significant value for the Android platform. This is also occasion for developers to reduce cost preparing mobile software for a smaller number of platforms at the same time. MicroEmulator, which is pure Java implementation of Java ME API's in Java SE, seems to be very well suited for the Android. Last few months I've been working extending MicroEmulator to be an executable environment to run Java ME applications on that platform. Here are results of my work with demonstration of popular Java ME game 5ud0ku automatically translated to the Android.

These are screenshots of 5ud0ku running side by side on emulators: Java ME and Android.






Works to complete the effort are not finished yet, but I hope a lot of existing Java ME applications can be already transformed to Android. At the moment code is only available in SVN repository. For anyone interested, here is the short manual how to convert 5ud0ku yourself:

1. Check out MicroEmulator from SVN repository

svn co https://microemulator.svn.sourceforge.net/svnroot/microemulator/trunk/microemulator microemulator -r 1997


2. Build MicroEmulator project using Maven

cd microemulator
mvn


3. Make sure that the following properties in microemu-android/build.xml correctly point to the Android SDK and ASM jar file

<property name="sdk-folder" value="$HOME/android-sdk-linux_x86-1.0_r1"/>
<property name="asm.jar" value="$HOME/asm/asm-3.1.jar"/>


4. Configure properties for 5ud0ku game, edit microemu-android/build.xml file (before you have to save 5ud0ku.jar in microemulator folder)

<property name="midlet.name" value="5ud0ku"/>
<property name="midlet.package" value="5ud0ku.apk"/>
<property name="midlet.icon" value="bin/assets/5ud0ku-icon.png"/>
<property name="midlet.class" value="de.wintermute.sudoku.SudokuMidlet"/>
<property name="midlet.jar" value="../5ud0ku.jar"/>


5. Create Android apk package with Ant

cd microemu-android
ant


6. And finally deploy microemu-android/bin/5ud0ku.apk on emulator or G1 phone. For all of you who don't want to go through this somehow long procedure, here it is already prepared 5ud0ku.apk package.

UPDATE svn checkout command now include revision number

UPDATE Conversion manual has been updated, it is presented in Converting JavaME applications to the Android platform (revisited)

86 comments:

Jerry said...

good. but i doubt how about the performance of it? thanks.

Bartek Teodorczyk said...

Performance is actually pretty good, all Java ME code runs directly on Dalvik VM.

Tomas said...

Bartek,
any plans on making easier process of creating apk packages of developers jar files?

larry said...

There is an J2ME runner already available in the android market and I've tried and it seems to be working fine.

Bartek Teodorczyk said...

Tomas,
Packaging apk exporter is in my plan for future release, the UI idea is very similar to the existing "Save for Web" option that produces applet version of MIDlet.

Bartek Teodorczyk said...

Larry,
I think the most important difference between these project is licensing. MicroEmulator is an open source project under LGPL. Large parts of the project are also Apache licensed.

Anonymous said...

> Performance is actually pretty good, all Java ME code runs directly on Dalvik VM.

All right, I believe that the Sudoku game works fine. But what about games with more cpu-consuming animations (and/or game logic)?

dasorin2000@yahoo.com said...

Does this support porting of JSR 184? I am interesting if there is any of you that tried to port m3g jsr 184 to opengl es 1.0.

Thx,

Bartek Teodorczyk said...

JSR-184 on top of Android OpenGL ES will be available in January 2009 as a commercial extension

Anonymous said...

Bartek,

any rough plan, when would an option to export apk package would be available?

Best regards,
Tomas

Bartek Teodorczyk said...

Apk exporter is also scheduled for January 2009.

Anonymous said...

I have just done all of this for an application I created. On the first screen, I have a form, Login, two TextFields, one for username, one for password. This all work wonderfully within sun WTK2.5.2. and 3.0 When turn my application into a .apk and I run my application through the emulator, the name of the form (i.e. Login) appears at the top followed by:

javax.microedition.lcdui.TextField@43399ed8
javax.microedition.lcdui.TextField@4339a188

Is there no support within microemulator for the lcdui API? Or have I made an error along the way and if so, any suggestions would be greatly appreciated!!

Thank you so much for you help in advance and P.s. I think microemulator is a great idea!!!

Anonymous said...

Sorry, it is the same guy as above with the lcdui problem and question. I just thought I would mention that I am using android-sdk-linux_x86-1.0_r1.

Thank you again!!

Bartek Teodorczyk said...

TextField UI component is not implemented yet.

Andreas said...

Bartek,
does the emulator support connection to the Internet and the location services on the Android?

Bartek Teodorczyk said...

Andreas,
1. Emulator supports the following protocols: datagram, http, https, socket, ssl.
2. Location services (JSR-179), I'm considering to offer this JSR as a commercial implementation for MicroEmulator.

zet said...

Bombus does not work with that :(

Eric said...

I am a Rookie and I can't complie it ! please help me and thank you very much!
my email:summernai@126.com

my Android sdk:android-sdk-windows-1.0_r1

when I Create Android apk package with Ant
error:
dex:
exec error: no command specified


BUILD FAILED
D:\program\Thesis\J2METoAndroid\microemulator\microemu-android\build.xml:107: exec returned: 1

Bartek Teodorczyk said...

Build process was verified for Linux and Mac OS. It seems to fail on Windows.

gunnar said...

BUILD FAILED
/home/myname/android/me/microemulator/microemu-android/build.xml:57: microemu-midp.jar doesn't exist at ../microemu-midp/target/microemu-cldc-3.0.0-SNAPSHOT.jar


What should I do ?

Bartek Teodorczyk said...

It seems that my last changes break the build process when project is checked out from the trunk. The best way to avoid that is to use revision 1997.

Anonymous said...

good. I use android-sdk-windows-1.0_r1 succeed compile Simple Demo and 5ud0ku.

Now Simple Demo run right. but 5ud0ku has mutiple same commands in menu when runing at android.

Today, I complied myself J2ME application mchessgodtry. I got an error "Launcher [no midlets]" when runing at android. Can you help me?

Note 1, when we use android-sdk-windows-1.0_r1, we should comment line arg value="-JXmx384M" in build.xml. The -J option is not support at android-sdk-windows.

Note 2, that above proplems(same command and no midlets) appears at microemulator revision 1997, 1998, 2000.

Bartek Teodorczyk said...

Regarding problems with mchessgodtry application, there should be more information in the emulator logs. What's a error on the console when emulator is running with "-logcat MicroEmulator" command line option.

Anonymous said...

Now, I gain that probream perhaps for microEmurator android build.xml ignore -rf option when building apk package. No -rf option, we will dismiss J2ME application resource.

Anonymous said...

[ERROR] BUILD ERROR
[INFO] Failed to resolve artifact.
Missing:
----------
1) sun:applet-jsobject:jar:1.4


Any ideas?

Bartek Teodorczyk said...

Did you checkout revision 1997? Full command line:
svn co https://microemulator.svn.sourceforge.net/svnroot/microemulator/trunk/microemulator microemulator -r 1997

Or that was just temporary problem with Maven repositories.

foxdemon said...

Jep, tried it with rev1997. Same problem with rev2047.


Since it's the same today, I don't think it's just a temporary problem with Maven repositories.

Bartek Teodorczyk said...

That's strange. I repeated the whole procedure one more time without any problems. Maybe, the problem is platform specific. What's your OS?

foxdemon said...

Linux. Archlinux, to be specific.

Bartek Teodorczyk said...

Just tested on Ubuntu, works fine.

anjosc said...

This is broken with android SDK 1.5.
It doesn't even compile (my version is android-sdk-linux_x86-1.5_r1 on ubuntu 8.10).
Maven compiles the project, but compiling any MIDlet fails on compiling the source from microemulator-android. Android APIs changed ... :-(

Any news when this will be compatible ?

Thanks.

Bartek Teodorczyk said...

anjosc, for revision 1997 you have to use android-sdk-windows-1.0_r1. For the latest SVN trunk it should be possible to compile against all Android SDK version except 1.5 yet.

Laurent said...

Hello, congratulation for your great work!
I have a j2me library that I want to use in android, the issue is that Dalvik VM doesn't like javax.microedition.lcdui.Image; :s

Is the Image object implemented on MicroEmu?

Thanks in advance!

Bartek Teodorczyk said...

Laurent, you can rely on MicroEmu Image implementation on the Android platform

Laurent said...

Thanks for your quick answer! :)

I hope everything will be fine for me now !

Have a nice day... or evening ;)

William Anthony said...

Hi, just a quick question:

how do I connect through proxy with authentication?

I've tried using user:password:host format and http.username. Both of them are not working.

Bartek Teodorczyk said...

William, can you provide more details how you tried using user:password:host format and http.username.

William Anthony said...

I want to, but blogger won't allowed me post xml tags

William Anthony said...

I've emailed you...

sonic said...

I'm trying to convert the DB Railnavigator from Deutsche Bahn. With the J2SE edition of MicroEmulator, it works. When I start it on Android, it crashes with

V/MicroEmulator( 162): Delete RecordStore i18n...Couldn't delete RecordStore i18n
V/MicroEmulator( 162):
V/MicroEmulator( 162): nDelete RecordStore MyCalendar...Couldn't delete RecordStore MyCalendar
V/MicroEmulator( 162):
W/dalvikvm( 162): threadid=21: thread exiting with uncaught exception (group=0x40010e28)
E/AndroidRuntime( 162): Uncaught handler: thread Thread-11 exiting due to uncaught exception
E/AndroidRuntime( 162): java.lang.NullPointerException
E/AndroidRuntime( 162): at java.util.Hashtable.put(Hashtable.java:720)
E/AndroidRuntime( 162): at a.o.a(Unknown Source)
E/AndroidRuntime( 162): at a.o.a(Unknown Source)
E/AndroidRuntime( 162): at de.hafas.j2me.Hafas.a(Unknown Source)
E/AndroidRuntime( 162): at a.aw.run(Unknown Source)
E/AndroidRuntime( 162): at java.lang.Thread.run(Thread.java:935)
I/Process ( 49): Sending signal. PID: 162 SIG: 3
I/dalvikvm( 162): threadid=7: reacting to signal 3
I/dalvikvm( 162): Wrote stack trace to '/data/anr/traces.txt'

sonic said...

JAD, JAR

Bartek Teodorczyk said...

sonic, did you compiled midlet with revision 1997 of MicroEmulator? If so, try to use trunk version from repository.

sonic said...

Then I get

[javac] /microemulator/microemu-android/src/org/microemu/android/device/ui/AndroidAlertUI.java:98: cannot find symbol
[javac] symbol : variable BUTTON_NEUTRAL
[javac] location: interface android.content.DialogInterface
[javac] which = DialogInterface.BUTTON_NEUTRAL;
[javac] ^
[javac] /microemulator/microemu-android/src/org/microemu/android/device/ui/AndroidAlertUI.java:102: cannot find symbol
[javac] symbol : variable BUTTON_POSITIVE
[javac] location: interface android.content.DialogInterface
[javac] which = DialogInterface.BUTTON_POSITIVE;
[javac] ^
[javac] /microemulator/microemu-android/src/org/microemu/android/device/ui/AndroidAlertUI.java:105: cannot find symbol
[javac] symbol : variable BUTTON_NEGATIVE
[javac] location: interface android.content.DialogInterface
[javac] which = DialogInterface.BUTTON_NEGATIVE;
[javac] ^
[javac] /microemulator/microemu-android/src/org/microemu/android/device/ui/AndroidAlertUI.java:112: cannot find symbol
[javac] symbol : method setButton(int,java.lang.String,android.content.DialogInterface.OnClickListener)
[javac] location: class android.app.AlertDialog
[javac] alertDialog.setButton(which, command.getLabel(), onClickListener);
[javac] ^

Bartek Teodorczyk said...

sonic, you need Android 1.5 SDK for building

sonic said...

With 1.5:

BUILD FAILED
/microemulator/microemu-android/build.xml:171: Execute failed: java.io.IOException: Cannot run program "/android-sdk-linux_x86-1.5_r3/tools/aapt": java.io.IOException: error=2, No such file or directory

sonic said...

Sorry, wrong path...

sonic said...

Hmm, now:

Launcher
[no midlets]

Now to the O2 shop to get a real phone: I'm optimistic ;-)

chethan said...

Buildfile: build.xml

clean:
[delete] Deleting directory D:\microemulator\microemu-android\bin

check:

dirs:
[mkdir] Created dir: D:\microemulator\microemu-android\bin
[mkdir] Created dir: D:\microemulator\microemu-android\bin\assets
[mkdir] Created dir: D:\microemulator\microemu-android\bin\classes
[mkdir] Created dir: D:\microemulator\microemu-android\bin\producer
[mkdir] Created dir: D:\microemulator\microemu-android\bin\res
[mkdir] Created dir: D:\microemulator\microemu-android\bin\libs

compile-producer:
[javac] Compiling 2 source files to D:\microemulator\microemu-android\bin\
oducer

prepare-jars:

prepare-assets:
[unzip] Expanding: D:\microemulator\SampleLabel.jar into D:\microemulator\
croemu-android\bin\assets

prepare-res:
[mkdir] Created dir: D:\microemulator\microemu-android\bin\res\values
[mkdir] Created dir: D:\microemulator\microemu-android\bin\res\drawable
[copy] Copying 1 file to D:\microemulator\microemu-android\bin\res\drawab


package-res:

BUILD FAILED
D:\microemulator\microemu-android\build.xml:171: Execute failed: java.io.IOExc
tion: CreateProcess: D:\android-sdk-windows-1.5_r3\tools\aapt package -f -m -J
rc -M bin/AndroidManifest.xml -S bin/res -A bin/assets -I D:/android-sdk-windo
-1.5_r3/android.jar -F bin/SampleLabel.apk.res error=2

"I am getting the above mentioned error" i have followed all the steps that are mentioned in the blog.

Bartek Teodorczyk said...

chethan, make sure that you have path to the aapt file set correctly and double check if that file really exists there

chethan said...

HI i am not able execute ant command, am getting following error

BUILD FAILED
D:\microemulator\microemu-android\build.xml:171: Execute failed: java.io.IOExcep
tion: CreateProcess: D:\android-sdk-windows-1.5_r3\tools\aapt package -f -m -J s
rc -M bin/AndroidManifest.xml -S bin/res -A bin/assets -I D:/android-sdk-windows
-1.5_r3/android.jar -F bin/SampleLabel.apk.res error=2
-----------------------------------
am using android sdk1.5 r3,

svn co https://microemulator.svn.sourceforge.net/svnroot/microemulator/trunk/microemulator microemulator -r 1997 "I didnt find this in the svn" kind could anyone help me regarding this i have an application compiled using j2me (which is in .jar format) i need to convert it into .apk to run on android based mobile.

Regards
chethan

Bartek Teodorczyk said...

chethan, is D:\android-sdk-windows-1.5_r3\tools\aapt file existing there?

chethan said...

hi Bartek Teodorczyk, you are right i dont have that folder aapt folder under android sdk, i have downloaded from android offical website ie "http://developer.android.com/sdk/1.5_r3/index.html" for windows when i unzipped i didnt find any files.
COuld you please help me out with this

Bartek Teodorczyk said...

chethan, aapt is a file, in Windows the full name is aapt.exe and it is located in platforms\android-1.5\tools folder starting from SDK root

chethan said...

hi i didnt find aapt.exe in latest android version ie 1.5 with r3. I'm downloading 1.5 with r1 is that fine will i be able to run with the r1 of android 1.5 version

chethan said...

hi bartek i found aapt.exe inside \android-sdk-windows-1.5_r3\platforms\android-1.5\tools\aapt.exe but am getting error failed to find aapt.exe under \android-sdk-windows-1.5_r3\tools i didnt find aapt .exe in the specified path

Regards
chethan N

Saw said...

Hi,Bartek
my problem is I would like to run a J2ME application in android phone.Can you tell me what do i need to attempt to solve this problem?

Bartek Teodorczyk said...

Saw, follow the build instructions. In case of problems ask questions on http://groups.google.com/group/microemulator-support group.

[TuRiM] said...

Hi,

I'd like, how to do start an Activity with the Microemulator?
I need to use the startActivity() method.
thanks.

Bartek Teodorczyk said...

[TuRiM], I think starting new activity from running MIDlet should be possible by using platformRequest method. At the moment Android implementation looks like:

public boolean platformRequest(String url)
{
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

return true;
}

alvaro543 said...

Hi Bartek, I was testing this tool six month ago testing ours j2me apps with incredible results, I was compiling with Android sdk 1.0 rc 1 and everything works ok.

Now I am trying to compile with Android sdk 1.5 r3, 1.6 r1, 1.0 r2 in in linux and I always get the same message
....
/home/alvaro/Programas/android/microemulator/microemu-android/src/org/microemu/android/device/ui/AndroidAlertUI.java:27: cannot find symbol
[javac] symbol : variable styleable
[javac] location: class android.R
[javac] TypedArray a = titleView.getContext().obtainStyledAttributes(android.R.styleable.Theme);


etc....


Can you Help me?

Thanks in advance.

Bartek Teodorczyk said...

alvaro543,

You can try to checkout the MicroEmulator source code from new location:
svn co http://microemu.googlecode.com/svn/trunk/microemulator microemulator -r 2211

I'm working now to update the build procedure and this location will become an official one very soon.

Alvaro said...

Thanks for your answer!. It all works ok

Tomas Kaminskas said...

Bartek,
I have a question, do you think it would be possible to integrate 3rd party Android library for ads (for example Greystripe or AdMob) into converted midlet? If yes, how can I add a jar library to the project (I am using Ant for building)?

Bartek Teodorczyk said...

Tomas,

I put the answer on the support group list: http://groups.google.com/group/microemulator-support/browse_thread/thread/d68538df5b97a057

Jui said...

Hello Bartek,

I have J2ME application using XML Data parser.When i try to run converted .apk file.it gives an error.Is xml data parsing not supported in microemulator??

Bartek Teodorczyk said...

Jui,

What kind of parser do you use? Has this error also some more detailed information?

Jui said...

Hello, bartek

Thanks for your reply.

To answer your question , I am using XMLParser.java in J2ME application.Basically the application reads data from xml file and displays in the form.

Ant tool successfully builds and .apk file is generated. But when i run application on emulator, it gives

"The application has stopped unexpectedly.Please try again"

Also while building i get warning :

Ignoring InnerClasses attribute for an anonymous inner class that doesn't come with an associated EnclosingMethod attribute. (This class was probably produced by a broken compiler.)

Bartek Teodorczyk said...

Jui,

You have to watch what LogCat console in Eclipse is saying.

Jui said...

Hello Bartek,

Thanks a ton for your help.

Now i have proper error log and exact location of error.Hopefully i will be able to debug and run whole application successfully.

Thanks once again for your help.

unnam said...

We plan to run a simple java applet in android emulator.. Can you help us? How can we use the web sources to run it directly?

Bartek Teodorczyk said...

unnam,

Of course I can help you. For easier communication I encourage to use the following channels:
- open source projects: http://groups.google.com/group/microemulator-support
- commercial projects: info@microemu.com

Purohit D said...

I appreciate the information. Java is one of the consistent player from the development industry which has been providing the wider scope for developers to come out with different solutions.

Marcio Andrey Oliveira said...

Hi. I've successfully generated 5ud0ku.apk so I believe my environment is correctly configured.

I've converted my j2me app to apk and when I try to run it on android emulator a null pointer exception is thrown.

At first I thought that some resource was not found because they were moved to assets folder while on my original application they are at the root on jar. I don't use subdirectories.

Looking at the log file it seems that the one of my classes was not found. This class is named Resource.

My app uses this class to load strings and images from jar. All methods and attributes are static.

I've generated the apk file without signature then I moved resources out of assets folder, manually signed the package but the problem persist exactly the same.

Could you give some insights to help solve my problem?

I don't know if these information are useful or not but
- if I use netmind the converted application works fine.]
- The apk file generated by netmind keep all resources in the file root.
- The jar used send to netmind was obfuscated.

PS: I've tried both obfuscated and unobfuscated jars with microemu.

Thanks a lot.

Bartek Teodorczyk said...

Marcio Andrey Oliveira,

It would be more convenient to continue this topic on the support group. Already started a new thread: http://groups.google.com/group/microemulator-support/browse_thread/thread/b4a762fd79bf1c14

Marcio Andrey Oliveira said...

Thanks for your reply.

I didn't know about this forum. I will use it.

Regards.

Namit Rana said...

Hi all,

It does not work with j2me RMS (RecordStore Management System) . Also the size of the converted .apk file is too big as compared to the midlet.

Bartek Teodorczyk said...

Namit Rana,

You should be more specific about your problem with RMS. The best would be to use http://groups.google.com/group/microemulator-support for that.

For reducing the apk size, you can always sponsor adding obfuscation feature.

David said...

I believe running java apps on Android is interesting but as well as interesting as running Android from an applet the sealed java android classes can be recursivly wrapped and emulated the preripherals then exposed tanks to creation of public methods and properties Android OS final libraries so can be executed code via javascript with an getWindow(..) createObject(..) and eval(..) so ie can be run all android software even java mobile apps in facebook or from any web page

David said...

Its interesting the fact of trying to load java apps from android but its also interesting the fact of running Android on an java Applet wrapping recursivly libraries needed then to be exposed by the Applet so can be executed via javascript through an getWindow(..) createObject(..) eval(..) thus it will be possible to run Android apps/games in any webpage or as a facebook app/game

Namit Rana said...

Hi,

Thanks Bartek for your quick reply.

It was my mistake (my last posted query) and i want to correct it for those who read this blog.

RMS works properly which i thought it didnt. But bluetooth does not work. In j2me, we have Local.getBluetoothDevice().getBluetoothAddress() api.

My j2me application just tries to get bluetooth address.

After converting to .apk, it shows "Some unexpected error has occured..Force close."

Also I will provide appropriate info. at http://groups.google.com/group/microemulator.

thanks

Aba said...

i made a lwuit midlet and built it following the steps and it crashes at start.
5ud0ku and the simple demo run fine nut i also made a simple hello world with the lcdui which adds text from a textfield to the form and it always crashes after the second press?
Any ideas on how to fix it?