Mozilla's mobile Firefox browser is coming to Google's Linux-based Android operating system. Although the porting effort is still at an early stage of development, it is moving forward swiftly. Mozilla's developers achieved an important milestone this week by demonstrating that the browser can run on the Nexus One smartphone.

Due to the highly experimental status of the project, Mozilla has not yet published packages for testing, but that didn't deter us from getting our grubby mitts on the goods. As our readers know, we just can't resist the doughy flavor of half-baked software, and we will gladly brave the bugs for a chance to taste test the new hotness before it really heats up. In order to get our own hands-on look at Firefox on Android, we had to compile it from source code.

About Android and the NDK

Although Android is a Linux-based platform, it has its own totally unique userspace environment that is built on top of a custom Java runtime. Android applications are typically coded in Java and compiled into Google's own bytecode format so that they can be executed on Android's Dalvik virtual machine. Due to the somewhat insular and alien design of the Android userspace stack, porting a conventional Linux application to the platform is a highly complex and non-trivial effort.

Mozilla got the job done with Android's Native Development Kit (NDK). Developers use the NDK to compile C and C++ source code into native ARM binaries that can be loaded and used by standard Java-based Android applications. The purpose of the NDK is principally to make it possible for application developers to use native code for performance-critical computing tasks that would be impractical to perform on Dalvik. One of the secondary advantages of the NDK is that it allows developers to port and reuse existing C and C++ code.

Components that are compiled with the NDK can be used and integrated into an Android Java application via the Java Native Interface (JNI), a framework that serves as a bridge between a Java virtual machine and native code. Native code functions can be exported through JNI and made accessible to higher-level Java applications.

How the Firefox port works

To make Firefox run on Android, Mozilla modified the Firefox browser's Gecko rendering engine so that it could be compiled with the NDK and used in an Android application through the JNI.

Gecko is one of the core components of Firefox's underlying architecture. It handles parsing, layout, drawing, and much of the other functionality that is needed to display and support interaction with a webpage. In addition to rendering Web content, the Gecko engine is also used to render Firefox's user interface, which is described with an XML markup language called XUL.

Mozilla wrote a very small amount of Java code that is used as glue to make the native Gecko components accessible as an application in the Android environment. This code can be found in the embedding/android directory of the Firefox for Android source code. You can see how it works by having a look at the GeckoApp.java file.

The GeckoApp class is an Android Activity that handles the basic window layout and component initialization. The GeckoAppShell class loads the native libraries and starts running a Gecko engine in a thread. The GeckoSurfaceView class is an Android drawing buffer on which Gecko will render the page content and application user interface. The surface is embedded in the GeckoApp activity and painted to the screen. The surface is also responsible for relaying input events, such as screen taps and key-presses, to the underlying engine.

It's really important to understand that Gecko is used to draw the entire browser. In the Android port of Firefox, the menus, toolbars, and dialogs are all coded with XUL and rendered by Gecko instead of being built with standard Android widgets.

If Mozilla wanted to, they could build the user interface with real Android widgets and rely on Gecko solely for rendering Web content (an approach that is similar to the one used by Maemo's MicroB browser), but there are a number of highly compelling advantages offered by XUL that make it more desirable for a mobile Firefox browser. The principal advantage of using XUL for the mobile browser's user interface is that it is more conducive to supporting Firefox's much-loved add-ons.

One of the downsides of XUL as an independent, cross-platform toolkit, is that it doesn't automatically match the look-and-feel of other applications on any given platform. Mozilla compensates for this deficiency on the desktop by using the underlying platform's theming APIs to make the XUL widgets mimic the appearance of their native counterparts.

In mobile environments, Mozilla has taken a somewhat different approach with widget styles. Mozilla's developers have built a unique user interface with XUL that is tailored for small form factors and touch interaction, but doesn't necessarily bear a resemblance to the underlying platform. This tactic has delivered pretty slick results on Maemo.

This custom mobile interface hasn't yet been adapted for use by the Android port, however, which is currently using the same XUL templates as the desktop version of Firefox. This looks silly on a mobile device, but it's a temporary measure that is suitable for testing purposes at the present time. Mozilla says that the style that is used by Firefox Mobile on the N900 will eventually be used for the Android port.