Skip to main content

Android LWUIT Tutorial - or Integration PLEASE!!

4 replies [Last post]
68060
Offline
Joined: 2011-08-29
Points: 0

Well LWUIT works pretty good for MIDP, after some pain it works with Blackberry, but there are claims it also works on Android.. Whilst I dont doubt this and SOMEHOW I once got a project to work and convert fine, I have been trying for weeks with LWUIT 1.5 and the TINY TINY TINY amount of information out there, to get it working.... Ive tried about 5 approaches, once I got a APK but the were errors when it tried to go to a form, so I sat down today and decided I WILL get it working!!! And I made a tutorial of what I did... I managed to get all the way to the end and get BUILD SUCCESSFUL - HOWEVER, STILL NO APK? Can I just get on my knees for a second and plead with Shai and Thorsten and Chen.... and say the following "PLEASE PLEASE PLEASE can you either integrate android into the whole thing so that it works, OR at least give us a tutorial that works and explains everything! Thorstens tutorial is a couple of paragraphs and very very vague..."..

OK my begging is done, my clients are very angry I promised I could do an LWUIT android version and cant deliver and now I owe them cash they paid me upfront.. for what it is worth here is the tutorial I was preparing that SEEMS to work but still ends up with no APK :-(((((

THIS may or may not help someone, or even show what im doing wrong..

COMPILING ANDROID LWUIT TIPSTER APPLICATION:

get the demo project first:
http://www.pader-sync.com/LWUITDemo.zip

Import to nb, right click got to properties->libraries, fix the red ANDROID issue first
android.jar can be fund in the android sdk.
(example C:\android-sdk_r08-windows\android-sdk-windows\platforms\android-7)

Now go itno root dir of project where build.xml is, open command line type ant
it will fail, edit build.xml and adjust:

<property name="sdk" value="C:\Users\totti\android-sdk-windows" />
to point to your own sdk

mine became:
<property name="sdk" value="C:\android-sdk_r08-windows\android-sdk-windows" />

change this;
<property name="target-platform" value="${sdk}\platforms\android-12" />

to match one that exists in your sdk, i chose android.jar in android-7
so lets point to that

Now type ant again see that it needs:
LWUIT_GROUP/IO/Build.xml

in a command line grab what u need:

svn checkout https://svn.java.net/svn/lwuit~svn

So you need the code for IO, UI and LWUIT_Android_Incubator,

IO and UI are ordinary src, so right click nb project hit resolve references and deal
first with UI, this will ask for the UI project which will vary depending on where
you checked out the earlier src to, for me its:
E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\lwuit~svn\LWUIT_1_5\UI

This should now say "the problem was solved"

Now do same for IO,click it hit resolved problem..

now onto last one LWUIT_Android_Incubator

You need to check out thorstons port here:
svn checkout https://svn.java.net/svn/lwuit-incubator~svn

now i cant find this project anywhere, so I made it, make a j2me proj in netbeans (java project with existing sources)
and call it LWUIT_Android_Incubator, so mine edned up in E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT

click next to add sources, hit add folder, and add:

E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\lwuit-incubator~svn\trunk\thorsten_s\android\com

hit finish

now fix errors:

right click on the project. proprties->libraries, hit add projectm add the IO project from:
E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\lwuit~svn\LWUIT_1_5\IO

if this works errors regarding:
import com.sun.lwuit.io.FileSystemStorage;
import com.sun.lwuit.io.util.BufferedInputStream;
import com.sun.lwuit.io.util.BufferedOutputStream;
should vanihs.

ALSO ADD THE UI Porject in same way...

and hit add jar, add the android jar you added earlier (mine was android 7s jar, then all errors in the incubator project
should vanish.

Now clean and build incubator project

Now im faced with these errors:

Compiling 14 source files to E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\build\classes
E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\lwuit-incubator~svn\trunk\thorsten_s\android\com\sun\lwuit\impl

\android\LWUITActivity.java:195: warning: non-varargs call of varargs method with inexact argument type for last parameter;
cast to java.lang.Class<?> for a varargs call
cast to java.lang.Class<?>[] for a non-varargs call and to suppress this warning
Method m = Activity.class.getMethod("invalidateOptionsMenu", null);
E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\lwuit-incubator~svn\trunk\thorsten_s\android\com\sun\lwuit\impl

\android\LWUITActivity.java:199: warning: non-varargs call of varargs method with inexact argument type for last parameter;
cast to java.lang.Object for a varargs call
cast to java.lang.Object[] for a non-varargs call and to suppress this warning
m.invoke(this, null);

THESE ARE WARNINGS, SO LETS PROCEED AND PRAY.. I stil got
"BUILD SUCCESSFUL (total time: 1 minute 13 seconds)"

Now go back to your LWUITDemo project and add this newly created incubator project..

Clean & build,

Now i get:

Compiling 174 source files to E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\LWUITDemo\build\classes
E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\LWUITDemo\lwuit\android\com\sun\lwuit\impl

\ImplementationFactory.java:37: duplicate class: com.sun.lwuit.impl.ImplementationFactory
public class ImplementationFactory {
E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\LWUITDemo\lwuit\android\com\sun\lwuit\io\impl

\PlatformImplementation.java:48: duplicate class: com.sun.lwuit.io.impl.PlatformImplementation
public class PlatformImplementation extends IOImplementation {

TO FIX:

Go to LWUIT DEMO project and remove lwuit/UI -> com.sun.lwuit.impl.ImplementationFactory
Go to LWUIT DEMO project and remove lwuit/IO -> com.sun.lwuit.io.impl.PlatformImplementation

Clean and build again.....

You should get no errors, just them 2 warnings......

So I get:

Created dir: E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\LWUITDemo\build\classes
Created dir: E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\LWUITDemo\build\empty
Created dir: E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\LWUITDemo\build\generated-sources\ap-source-output
Compiling 172 source files to E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\LWUITDemo\build\classes
E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\LWUITDemo\lwuit\android\com\sun\lwuit\impl\android

\LWUITActivity.java:195: warning: non-varargs call of varargs method with inexact argument type for last parameter;
cast to java.lang.Class<?> for a varargs call
cast to java.lang.Class<?>[] for a non-varargs call and to suppress this warning
Method m = Activity.class.getMethod("invalidateOptionsMenu", null);
E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\LWUITDemo\lwuit\android\com\sun\lwuit\impl\android

\LWUITActivity.java:199: warning: non-varargs call of varargs method with inexact argument type for last parameter;
cast to java.lang.Object for a varargs call
cast to java.lang.Object[] for a non-varargs call and to suppress this warning
m.invoke(this, null);
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 warnings
Copying 23 files to E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\LWUITDemo\build\classes
compile:
Created dir: E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\LWUITDemo\dist
Building jar: E:\ALL_CODE\ALL_CODE_STUFF_2011\NETBEANSAPPS\ANDROID_LWUIT\LWUITDemo\dist\LWUITDemo.jar
jar:
BUILD SUCCESSFUL (total time: 57 seconds)

NOW if you build with ant at the command line or netbeans you SHOULD get a directory called: \lwuitdemobuild_android
with your APK in, HOWEVER I DONT!!!

Sorry folks, help me if you know any answers..

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
thorsten_s
Offline
Joined: 2008-08-15
Points: 0

The steps you listed look like a proper way if you are using Netbeans. You download and used the LWUIT projects (UI,IO,..) for Netbeans and set them up in your IDE. Then you downloaded the sources for the Android implementation from the incubator. There is indeed no project for Netbeans. There are just the sources. Personally I use a J2SE project with the android.jar added as a dependency, pretty much as you described. If you get all the LWUIT parts to compile you have to enter the Android world. If you think you properly configured the build.xml file from the sample project then run the 'buildandroid' target. I think you only ran the default 'build' target. The script will copy the LWUIT UI, IO, Android Impl and your application sources and assemble them in a central place. Have a look at the build.xml file to see what is happening.

I agree it is not easy to assemble a working build for Android. But I lack the time to provide a step by step guide. The sources are the best guide here I

believe. The build.xml file contains more ANT targets than the default build. It uses the toolchain from the Android SDK to assemble the apk files directly, without depending on an IDE

plugin. Your alternative to the build script from the sample is to use an IDE

plugin. There is one around for Netbeans and for Eclipse. If you use a plugin then reference the LWUIT

UI, IO

and Android impl. source folders from your app's project. Or reference the created jar files instead. Make sure the com.sun.lwuit.impl.ImplementationFactory and com.sun.lwuit.io.impl.PlatformImplementation classes from the implementation for Android replace the classes from the default LWUIT

UI

and IO

modules. Either by adding them earlier in the classpath or by replacing the files. Finally look at the com.acme.android.TipsterActivity class from the sample zip to see a sample application entry point.

68060
Offline
Joined: 2011-08-29
Points: 0

Firstly, Thank you so much for your reply!! Very rare that anyone seems to reply on here, and guess what you were right, I had it all right but i was just typing "ant"!!! "ant buildandroid" DOES IT!!!! Sorry im lame at understanding them ant xml files I have been working with them for years but its *always* a struggle!!! Actually first compile didnt work, its complaining about the app icons, I think Google changed something, anyway to get it working (right now with no app icon) I adjusted this in the AndroidManifest.xml in com.acme.android/

I made <application android:icon="@drawable/appicon" android:label="Tipster Demo"> BECOME:

<application android:label="Tipster Demo">

AND removed the supports screens bit here:

<!-- <supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" /> -->

Then it DID BUILD!! So sweet! What a beautiful app too - I want to use lwuit instead of android coding basically - Thanks VERY much, due to my frustration in the mean time I made yet another tutorial that explains how to do it if you have netbeans installed with the Android plugin, so I will post that here also, its not well written as it contains the same level of pain and suffering as the one above... :) but it did work, believe me I just finished dancing around and screaming with joy!! And now I come and read your reply and got the other method going too!

Thanks again, its great work, just very esoteric to actually get working! Im praying that there will be a drop of LWUIT including some improvements and fixes to the gui editor/code and also integrating Android properly and effortlessly (well as effortless as the BB port anyway!).. Then LWUIT would really be cooking with some serious gas!! Thanks for all your hard work and help...

And without further ado my second tutorial about this:

OK WHOLE NEW APPROACH!
================================================================

Install android plugin for netbeans, make sample project, hit finish. This suprisingly works and will create apk and launch

on device (using adb logcat), add a system out to prove so.

Drop in the code from your lwuit app from src, into src of your new android project.. now time to fix the errors!

You cant add libs to android proj in nb, nor can u use a project in use from another, this sucks so lets just drop src in

directly for now, the src we are putting in is the lwuit io and ui src, so this may need to be updated later to keep uo to

date with modern versions.

OK drop in the com dir from
lwuit~svn\trunk\UI\src
lwuit~svn\trunk\IO\src

into the src dir of your android proj

lots of errors will vanish, to finish j2me compatibility lets use J2AB code, grab that and drop into src also

things like:

import javax.microedition.io.Connector;
import javax.microedition.rms.RecordStore;

should now be available..

J2AB is incomplete so for thingsl ike this

import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

you will need to comment them out and recode in android specific code..

so do that now, aim here is to get a damn compile :)

I Commented out all this using //ANDROID// so later i can go back and fix, hopefully we can work from one code base
of original lwuit app you wrote which we can copy over with a .bat file if need be, and use preprocessor statements so
that the code can coexist in both projects.

J2AB also has some RMS methods wrong, like

getRecordSize()
getRecord()

So commento ut your RMS code for now we will fix this later!.

We need a reference to the activity you should have something like

org.me.androidapplication

and in there MainActivity.java, for now i make a static activity like this:

public class MainActivity extends Activity {

public static MainActivity me;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// ToDo add your GUI initialization code here
System.out.println("YES IT LOADED!!!");
me=this;
}

}

Now we can get the activity using MainActivity.me SO, go into

javax.microedition.midlet

go into MIDlet.java - find getActivity() and implement like this:

public Activity getActivity()
{

System.out.println("GET ACTIVITY WAS CALLED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");

if (MainActivity.me==null)
{
System.out.println("THE MAIN ACTIVITY WAS NULL!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
return MainActivity.me;
}

We also need to fix notifyDestroyed in here:

again hacky but I do

public final void notifyDestroyed()
{
// this activity is null, we call the main one from here:
MainActivity.finishMe();
}

Now we go and implement finishme() in MainActivity like so

public static void finishMe()
{
System.out.println("MainActivity FINISH ME%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
me.finish();
}

Save, Pray and get onb your knees and do clean n build..

For me this made an apk, time to test it.. ok it runs but it doesnt launch our midlet, so how?

HMMM copy \lwuit-incubator~svn\trunk\thorsten_s\android/com into your src dir of android proj

Now I found errors in com.sun.lwuit.impl.android -> AndroidVideo

so i added this to the top

@Override
//IT COMPLAINED SO I HACKED THIS IN!!!!!!!!!
public void setMediaListener(com.sun.lwuit.events.MediaListener ml)
{
System.out.println("setMediaListener - hacked in***************************************************");
}
public void close()
{
System.out.println("CLOSE() - hacked in***************************************************");
}
///////////////

ALSO ERRORS IN:

@Override
public long getRootSizeBytes(String root) {
String[] roots = this.listFilesystemRoots();
for (int i = 0; i < roots.length; i++) {
if (roots[i].equals(root)) {
File r = new File(root);
return r.getTotalSpace() - r.getUsableSpace();
}
}
return 0;
}

@Override
public long getRootAvailableSpace(String root) {
String[] roots = this.listFilesystemRoots();
for (int i = 0; i < roots.length; i++) {
if (roots[i].equals(root)) {
File r = new File(root);
return r.getUsableSpace();
}
}
return 0;
}

i just removed the return return r.getTotalSpace() - r.getUsableSpace(); and return r.getUsableSpace();
and replaced with -99, with a message, ie:

@Override
public long getRootSizeBytes(String root) {
String[] roots = this.listFilesystemRoots();
for (int i = 0; i < roots.length; i++) {
if (roots[i].equals(root)) {
File r = new File(root);
System.out.println("HACKKKKKED FIX ME&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
return -99;
// return r.getTotalSpace() - r.getUsableSpace();
}
}
return 0;
}

@Override
public long getRootAvailableSpace(String root) {
String[] roots = this.listFilesystemRoots();
for (int i = 0; i < roots.length; i++) {
if (roots[i].equals(root)) {
File r = new File(root);
System.out.println("ZZHACKKKKKED FIX ME&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
return -99;
//return r.getUsableSpace();
}
}
return 0;
}

So we will find them later if we use these methods and deal with them then.

NOW back in MainActivity make it extend LWUITActivity instead of activity!!!

Add this into onCreate() under what you have

Display.init(this);
// storage is initialized with activity as parameter!
Storage.init(this);
Display.getInstance().callSerially(new Runnable(){

@Override
public void run() {
new StateMachine("tipster.res");
}
});

use the corretc name for you .res AND MAKE SURE THERES NO "/" BEFORE THE RES...

add
import com.sun.lwuit.Display;
import com.sun.lwuit.io.Storage;

NOW WHERE TO PLACE THE .RES???

Create a directory called 'assets' at your project root (same dir that AndroidManifest.xml lives)

In /nbproject/project.properties, change

assets.dir=

TO

assets.dir=assets
assets.available=true
asset.dir=assets

build, get on knees again, pray... RUN....

IT WORKED!!! PARTTY TIME!!!!!

bayu.utomo
Offline
Joined: 2011-10-24
Points: 0

great work.

may be you should try android porting in ADT plugin for eclipse from google . They provide build and run perfectly. You don't need to change the build.xml and so on for simple application. The only one you need is integrate the android implementation from thorsten to your lwuit source.

I have tested it and run perfectly

68060
Offline
Joined: 2011-08-29
Points: 0

I have briefly summarised my tutorials online here also, thanks again for help:

http://radiantsilverlabs.com/content/compiling-j2me-lwuit-apps-android-using-netbeans