One game, 150 handsets: shipping J2ME games in 2005
How we shipped one Java phone game to around 150 handsets in 2005: a preprocessor, per-screen resources, three jar-size tiers, fixed-point maths and a 120-line build tool.
In 2004 we started shipping arcade games for mobile phones. Our first, AstralX, was a shooter with 40 levels and five bosses. By 2005 each of our games was running on around 150 different handsets from Nokia, Sony Ericsson, Motorola, Samsung and Siemens. The download page for one of them, Fishing, listed 168 named models.
There was no app store and no single platform. "Java phone" was a promise that the same code would run everywhere. It didn't, and closing that gap was the job.
The problem: "write once" met real phones
Every handset was a slightly different machine:
- Screens ranged from 128×128 and 120×160 up to 176×220, and some phones reported a different drawable area from the one you actually got.
- Profiles differed. Some phones ran MIDP 1.0 with no sprite or sound APIs worth the name; others had MIDP 2.0. Under both sat CLDC 1.0 or 1.1, which decided whether you even had floating point.
- Jar size was capped per handset and per operator portal. Our Fishing page promised "only 27 KB to 35 KB depending on device".
- Heap was tiny and garbage collection was slow enough to show up as a dropped frame.
- Keys had different codes on every brand, including the two soft keys under the screen.
A game that felt right on a Sony Ericsson K700 could crash, stutter or simply not install on a Nokia Series 40. Testing every phone by hand was not an option, so the variation had to be handled by the build.
Architecture: one source tree, many builds
We kept one source tree per game and let the build produce the variants.
one source tree (preprocessor //#if flags)
+ per-resolution resource folders (res_m2_176x220 ...)
+ one build "configuration" per screen and profile
-> compile -> preprocess -> obfuscate -> preverify -> jar per config
-> JarNamer clones each jar/jad per handset name from a device list - Configurations, not branches. A client game like Esselte had eight configurations: six MIDP 2.0 screen sizes plus Series 60 and Series 60 MIDP 2.0. Each had its own resource folder and its own SDK target.
- A small shared framework. The same handful of classes (constants, game manager, sound, MIDlet) was reused across several client games, so each new title started from code that already knew how to behave on difficult phones.
- Handsets were data. The last step turned one build per configuration into one named build per handset.
The engineering
Compile-time constants for every screen
Java ME had no preprocessor, so we used the one in our build tools. Layout constants lived in a single interface, with one block per configuration. Only the active block was uncommented at build time:
//#if m2_128x128 || Series40
//-- public static final int SCREEN_WIDTH =128;
//-- public static final int SCREEN_HEIGHT =128;
//-- public static final int MENU_STARTY =68;
//#endif
//#if Series60 || Series60M2
//-- public static final int SCREEN_WIDTH =176;
//-- public static final int SCREEN_HEIGHT =208;
//-- public static final int MENU_STARTX =SCREEN_WIDTH>>1;
//-- public static final int MENU_STARTY =118;
//#endif Because these were static final constants, the compiler folded them away. No lookup cost at runtime, and no code for screens the build wasn't targeting.
Three jar sizes from one game
When a handset's jar limit was smaller than the game, we didn't write a second game. We cut the same one down. AstralX shipped in three tiers from one code base:
| Build | Jar size | Profile |
|---|---|---|
| AstralX | 120,038 bytes | MIDP 2.0 |
| AstralX100k | 92,174 bytes | MIDP 2.0 |
| AstralXMini | 54,265 bytes | MIDP 1.0 |
The Mini build dropped two of the bosses, two enemy types, a bitmap font and a bonus module, and shipped 20 image files instead of 37. Deciding what to cut was a design decision as much as an engineering one: the small build still had to feel like the same game.
Two tools helped every tier. An obfuscator renamed classes down to single letters, which saved real bytes when every class name is stored in the jar. And a small packer of ours, mpacker, folded a folder of resources into one .pak file with a header of names, offsets and sizes. The game opened one stream and seeked to what it needed instead of paying the jar's per-file overhead. The packer wrote its integers big-endian, so Java's DataInputStream could read them straight off the phone without conversion.
Integer maths and no allocation in the hot path
Most of our target phones had no floating point. Positions and velocities were stored in fixed point, as the sprite class says: "Positional information is in fixed point 24:8". Sine and cosine came from lookup tables scaled to 256, and sprite sizes were stored as power-of-two shifts. Movement was scaled by the frame time, so the game ran at the same speed on a fast phone and a slow one:
m_deltaX = ((m_posX - m_x)<<8) >> 7;
m_deltaY = ((m_posY - m_y)<<8) >> 7;
m_y += ((m_deltaY * CookieJar.m_dt)>>8);
m_x += ((m_deltaX * CookieJar.m_dt)>>8); Sprites came from a fixed-size pool, so nothing was allocated during play. Images were loaded per module and released when the player moved between the menu, a level and a boss, with a deliberate System.gc() at each switch. Collection happened when we chose, on a loading screen, never in the middle of a wave. If you have read our GBA engine story, this is the same discipline in a different language.
A 120-line build tool
The last step was the least glamorous and saved the most time. Operators and portals wanted one file per handset, with the handset in the name. JarNamer took a built jar and jad, a device list and an output folder, and wrote a named copy for every line, rewriting the jad's MIDlet-Jar-URL to match:
while(ifsList.good())
{
ifsList.getline(device,256);
// ...build "<name>_<device>.jad" and open it...
string sNewJadFile(inputJadData);
int i = sNewJadFile.find("MIDlet-Jar-URL: ",0)+16;
string newName = sFileName;
newName.append("_");
newName.append(device);
newName.append(".jar");
sNewJadFile.erase(i,sFileName.length()+4);
sNewJadFile.insert(i, newName);
ofsJad.write(sNewJadFile.c_str(), sNewJadFile.length());
// ...then write the jar bytes unchanged
} It is not clever code. It turned a manual, error-prone release step into a command, which is what most build tooling should do.
Where AI fits
The intelligence in these games was game AI. AstralX enemies ran small finite state machines: enter the screen, find a spot in the formation, retreat, attack left or right. How often an enemy fired was a random roll scaled by a difficulty level, so later waves felt meaner without any new code. A 2,250-line level manager drove formations and waves. We cover how that kind of behaviour was built in our post on game AI before AI.
The more lasting lesson is about budgets. Choosing which features fit a 54 KB jar is the same question we now ask when we fit an AI model onto a phone or a pair of glasses: what has to run on this device, what is it allowed to cost, and what do we cut so the result still does its job? Wearables and edge AI work starts from that question.
What it taught us
- Build for the matrix, not the device. Configurations, resource folders and a device list scaled to 150 phones. Hand-made builds would not have.
- Tools are for other people too. We built toolchains and editors that other developers used, and we worked with client artists so their artwork fitted each device's screen and memory before it reached a build.
- Show it running. We also wrote MEmu, a small Java applet that re-implemented enough of the MIDP display APIs on top of the desktop JVM to run our games in a web page, inside a handset skin. A new handset skin was a properties file and some images. Visitors to our website could play a game before installing anything.
- Porting is a service. The same pipeline let us port licensed titles from BREW to J2ME for publishers, including Shrek 2 and MTX: Mototrax.
Why it still matters
Android today is the same problem with better tools: thousands of models, different screens, different vendor quirks and very different amounts of memory. The habits we formed in 2005 still decide whether a field app runs well on the cheapest phone a crew carries: treat devices as data, set budgets early, keep allocation out of hot paths and automate the boring release steps. That's how we approach mobile and field apps now, and the Mobstar Games story has more from the era.