Hacking Gaia

该页面是为Gaia开发者写的。如果您在寻找关于如何编译和运行Firefox OS 的信息,可以参考 Building and installing Firefox OS page .

Gaia 是web apps 的集合,并且由它组合成  Firefox OS的前端. 在Firefox OS 屏幕上看到的都是由Web技术构建的。其中包括主屏幕和所有的原生app。本文则提供了修改Gaia代码的详细说明。


要获取Gaia的源码, fork us on GitHub 并且使用 git 将您fork的代码clone到本地。

$ git clone https://github.com/mozilla-b2g/gaia.git



B2G 桌面

B2G 桌面是在Firefox OS设备上运行应用程序的桌面版本,您可以使用它在电脑桌面上运行Gaia。

您可以从Firefox Nightly站点上下载B2G桌面的nightly版本。其中有关于Linux (32位 and 64 位), Mac OS X 以及 Windows的版本.

Nightly 版本都会对近期的gaia版本打包。一旦您下载完成压缩包,只需要解压到文件夹中,并从解压到的文件夹中运行b2g二进制文件就可以了。 

$ cd b2g
$ ./b2g

如果您出于开发的目的来运行带有指定版本Gaia的B2G, 则需要从您clone的代码位置构建一个配置文件:

$ cd /path/to/gaia
$ DEBUG=1 DESKTOP=0 make

上面的命令会在gaia 生成一个名称为profile的文件夹。 DEBUG选项会将 Gaia 作为托管的应用在内置的Web服务器上运行,而不是在每次更改后都需要重新打包的原生应用。在运行上面的命令后,您可以在最后的输出行中找到配置文件夹的路径,大体是 

Profile Ready: please run [b2g|firefox] -profile /path/to/gaia/profile


$ ./b2g /path/to/gaia/profile


注意 : 在 Mac OS X,  b2g 二进制文件会存在于 B2G.app.您需要运行下面命令:

./B2G.app/Contents/MacOS/b2g /path/to/gaia/profile


也可以在Firefox内部运行Gaia。这种方式会让您的开发周期大大缩短,同时也可以使用标准的web开发工具和调试器。 可以参考Gaia开发快速指南 获取更多的信息.


如果您有一个兼容的移动设备,也可以使用Firefox OS 烧机来运行Gaia。您可以参考编译和安装Firefox OS 获取更多信息. 我们也有关于如何测试Firefox OS的文章。


参考Gaia单元测试  来获取如何创建和在Gaia层运行单元测试的文章。


在Bugzilla的 Firefox OS > Gaia 中提交bug。在Gaia组件  (或者是子组件)中提交bug.

为 Gaia做贡献




  • 背景:
  • Make sure HTML files are declared <!DOCTYPE html> (that is, as HTML5 documents). If you don't, Internet Explorer 9 and later will load them in compatibility mode.
  • Include the "use strict"; statement (just like that, including the quotes) to the top of your JavaScript files to put them into strict mode.
  • Always use two spaces for indentation, rather than tabs.
  • Please use line breaks to separate logical bits of code!
  • Multi-word file names should use the "underscore" character to separate words, like_this.js.
  • Use single quotes instead of double quotes for strings.

Additional rules


if (expression) doSomething();


if (expression) {

If you're working on the system app, check out the guidance listed here.

Before submitting a patch we recommend you run gjslint on it to check for any style errors:

gjslint --nojsdoc my_file.js

提交一个 patch

First file or assign a bug to yourself on Bugzilla, you'll need a Bugzilla account.

Then create a branch on your fork of Gaia:

$ git branch branchname
$ git checkout branchname

Commit your changes:

$ git add /file/to/add
$ git commit -m "Bug XXXXX - Fix the broken Gaia and save the world"

Push your branch:

$ git push origin branchname

Send a pull request by navigating to the branch in your fork on GitHub and finding the pull request button.

Note: Except under unusual circumstances, patches should be landing first on the master branch, not a release branch like v1-train, v1.3, etc. If they need to land on a release branch, they must go through the usual approval process as outlined on the B2G Landing wiki page.

To request a review of your patch, attach the pull request to the bug in Bugzilla by referencing the URL of the pull request, and set the review ("r") flag to "?" and enter the bugzilla ID of one of the module owners and peers (very important - otherwise your bug will not likely be seen by anyone). The Github tweaks for bugzilla extension on AMO can help automate this process by automatically creating the attachment and adding it to the bug; you will still need to set the review flag on Bugzilla.

The reviewer may ask you to make some changes; you may need to amend the original commit and force push it to the original branch/pull request. Once they're happy with your patch, they will merge it into the master branch for you. Before they do this they would prefer it if you could squash all your changes into a single commit, so your contribution can be tracked easily.

The person who merges the commit (usually the reviewer) would add a r= flag in the comment of the merge commit.

Make 选项

you use the make command inside the Gaia repo to create a Gaia profile that can be loaded onto your device or run in a B2G Desktop build. This section looks in detail at the different make options available.

There are many environment variables present in the Makefile. Do not depend on them as they may be removed in the future.

Created profiles are stored in /gaia/profile, and contain the following items:

  • defaults: Directory containing default settings to be reloaded after you reset the phone.
  • extensions: Directory containing extensions.
  • settings.json: Settings file.
  • user.js: Another file containing more settings/preferences.
  • webapps: Directory containing all the web apps that are to be installed on the phone.

Note: When you've already made a profile and you want to build a new one, you must delete the existing profile directory before trying to generate a new one.



This simply gives you an unbranded, non-debug build. For a branded build build you need to use Official Mozilla branding make; for a debug build you need Debug make.


make install-gaia

make reset-gaia

With ADB (Android Debug Bridge) setup, these make targets will push Gaia to the device. install-gaia will just push updates of Gaia from your working directory to your device. reset-gaia will purge all existing configuration, profiles, web apps and database entries (a new settings database will be initialized) before pushing Gaia.


APP=system make

APP=system make install-gaia

When a profile already exists, APP allows you to specify which app to re-package, instead of re-packing and re-pushing all the Gaia apps.


You can specify a custom directory to build your profile in, using PROFILE_FOLDER, for example:

PROFILE_FOLDER=profile-b2g-desktop make


There are a few make options that create builds for different devices, with different purposes.

Create a phone build of Gaia


This build gets apps from /gaia/build/config/phone/apps-engineering.list.

Create a tablet build of Gaia

GAIA_DEVICE_TYPE=tablet make

This build gets apps from /gaia/build/config/phone/apps-engineering.list.


There are a few make options that create different types of build, with different purposes.

Production make


This creates a production build of Gaia:

  • Gaia is run as packaged apps, which are harder to debug, but are the best available state for apps in terms of available API permissions, etc.
  • Test apps are not included in the build
  • Remote debugging is turned off by default
  • Lock screen is turned on (which in turn will cut USB connections)
  • Marionette is turned off
  • First time user experience is turned on
  • Offline cache is used.

Note: You can also use the alias make production.

Debug make

DEBUG=1 make

The DEBUG variable runs Gaia as hosted apps on a built-in web server on a specific GAIA_PORT, rather than the default of packaged apps which have to be re-packaged after every change; this makes things easier to test. Launching the profile with the latest Firefox Nightly will also give you nice B2G specific panels on the Firefox Developer Tools.

In addition:

  • Test apps are included in the build.
  • Remote debugging is turned on by default.
  • Lock screen is turned off (USB connections won't be interrupted.)
  • Marionette is turned on, which is needed when running Gaia unit tests.
  • First time user experience is turned off.
  • Offline cache is not used, even if it is generated.

Device debug make


This disables screen lock on the device, and enables debugging with the ADB tool, so is useful for device debugging.

In Firefox OS version > 1.2, specify this param when you want to debug Firefox OS webapps with the App Manager.

Debug desktop make


This option creates a desktop debug version, for running inside B2G desktop.

Official Mozilla branding make


Use this to make an official Mozilla-branded build.

Dogfood make

DOGFOOD=1 make

Dogfooding options and utilities are turned on, for example the Feedback app, which allows doog fooders to easily submit feedback on the OS.

System apps make


This environment variable lets you push an app to /system/b2g instead of /data/local. You should use this when you work with a user build. This variable is automatically set when running make production. This can be used for install-gaia or reset-gaia too.

Distribution and market customization build


Note: Read Market Customizations for more details.


There are also make options for adding/removing features or changing settings, for debugging purposes.

Enable remote debugging


This enables remote debugging on the device, the same as using the option in the developer settings.

JavaScript optimization make


This triggers an optimization pass on Gaia's JavaScript, concatenating/compressing the files. This is automatically set when running make production. This can be used for install-gaia or reset-gaia too.

High resolution image assets


When packaging the app, this option replaces images with their *@1.5x.(gif|jpg|png) equivalents if such images exist. You need to use the above option as part of a standard make command, for example:

GAIA_DEV_PIXELS_PER_PX=1.5 make reset-gaia

GAIA_DEV_PIXELS_PER_PX=1.5 make install-gaia

Gaia is currently targetting the following screen resolutions:

  • qHD: ~540×960; device pixel ratio = 1.6875
  • WVGA: ~480×800; device pixel ratio = 1.5
  • HBGA (320x240); device pixel ratio = 1

use GAIA_DEV_PIXELS_PER_PX to make sure the images looks sharp on qHD and WVGA devices. see A pixel is not a pixel for more information about device pixels per css pixels.

Haida features

HAIDA=1 make reset-gaia

This build enables the Haida feature set, which is currently limited to rocketbar and edge gestures, but will likely include other features in the near future.

Disable first time use experience (FTU)


Disable the FTU with this environment variable.

Disable lockscreen

You can disable the Firefox OS lockscreen using the NO_LOCK_SCREEN option, for example:


Reference Workloads

Reference workloads allow developers/testers to quickly install a large amount of data in several applications, typically on a newly-flashed phone.

The commands are (from the gaia directory):

make reference-workload-light
  • 200 contacts
  • 200 sms messages
  • 50 dialer history entries
  • 20 gallery images
  • 20 songs
  • 5 videos
make reference-workload-medium
  • 500 contacts
  • 500 sms messages
  • 100 dialer history entries
  • 50 gallery images
  • 50 songs
  • 10 videos
make reference-workload-heavy
  • 1000 contacts
  • 1000 sms messages
  • 200 dialer history entries
  • 100 gallery images
  • 100 songs
  • 20 videos
make reference-workload-x-heavy
  • 2000 contacts
  • 2000 sms messages
  • 500 dialer history entries
  • 250 gallery images
  • 250 songs
  • 50 videos

These targets accept the APP environment variable, or an APPS environment variable that should contain the app names separated by a space, e.g.:

APP=sms make reference-workload-light
APPS="sms communications/contacts" make reference-workload-heavy

The apps available are:

APPS="gallery music video communications/contacts sms communications/dialer"

In order to install music (songs) with reference workloads, the utility mid3v2 must be installed. This utility can be installed with:

sudo apt-get install python-mutagen

If you run Fedora or RHEL instead, use:

sudo yum install python-mutagen

Documentation make

Gaia docs can be built, via jsdoc3. To generate these, you can use the following command:

make docs

Enabling IME layout and dictionaries

To enable keyboard IME layout and dictionaries enabled, use following command structure:

GAIA_KEYBOARD_LAYOUTS=en,zh-Hant-Zhuyin,el,de,fr,zh-Hans-Pinyin make

自定义 build-time 应用

The apps that run on Firefox OS are all contained within the Gaia source tree, in one of two locations:

  • gaia/apps: This is where the system default apps are found, such as calendar, email, settings, etc.
  • gaia/external-apps: This is where the Firefox Marketplace app is found (marketplace.firefox.com), and where apps subsequently installed by the user are stored.
  • gaia/showcase-apps: This is a container for multiple showcase apps, for example a 3D Crystal Skull to show off WebGL performance on the device.
  • gaia/test-apps: This directory is a repository for simple tests, designed to test simple B2G features.
  • gaia/external-apps: This directory contains more tests.
  • There may be oters too, depending on the version of Gaia you have cloned.

Note: If you are building B2G rather than Gaia, the paths will of course have B2G/ on the front, e.g. B2G/gaia/apps and B2G/gaia/external-apps.

If you want to omit some of these apps from your build of Gaia/B2G, you can do this in a few different ways:

  1. The "brute force" method is to simply delete the apps you don't want to be present at build time, before building.

  2. The more refined method is to edit the gaia/build/config/apps-*.list files to include the paths to the apps you want to include at build time. For example, gaia/build/config/apps-production.list looks something like this:


    But you could also include specific apps rather than just picking them all, for example:


    The mechanism for choosing which apps-*.list file is used during the build to determine the available apps is contained inside gaia/Makefile:

    ifeq ($(MAKECMDGOALS), demo)
    else ifeq ($(MAKECMDGOALS), dogfood)
    else ifeq ($(MAKECMDGOALS), production)
    ifeq ($(PRODUCTION), 1)
    ifeq ($(DOGFOOD), 1)
    ifndef GAIA_APP_CONFIG

    Initially, the GAIA_APP_TARGET variable is set to engineering, so by default building gaia from source will use app-engineering.list (which includes all the tests, demos, etc.):


    To specify usage of a different apps list you specify different options when running the make command. To build with apps-production.list, for example, you'd use

    PRODUCTION=1 make
    • If you specifically build with DEMO=1 specified, then it will use apps-demo.list.
    • If you specifically build with DOGFOOD=1 specified, then it will use apps-dogfood.list.
    • You can completely override the decision by using GAIA_APP_CONFIG and providing your own apps-*.list file.

    gaia/Android.mk contains these lines:

    ifneq ($(filter user userdebug, $(TARGET_BUILD_VARIANT)),)
    B2G_SYSTEM_APPS := 1

    When you build, if VARIANT=user or VARIANT=userdebug are set (these wind up getting reflected in the TARGET_BUILD_VARIANT variable), PRODUCTION=1 is automatically set when building gaia.

  3. The third, and most refined (but most complex) method is to use customizations. These allow you to specify build-time customization instructions in separate difrectories, without modifying the core Gaia repo. You can include your own customizations in distinct directories, or use the preexisting directories that come with the source.

    For example, the basic Firefox tablet customized app list is defined in apps.list under the distribution_tablet folder (gaia/distribution_tablet). These customizations can be applied at build time using options like this:

    GAIA_DISTRIBUTION_DIR=distribution_tablet make

    Note: Customizations is its own separate topic entirely. To learn more about it, read Market Customizations.

    Note: If you want to include custom external apps as part of your Gaia build, you need to build them in a specific way, and then place them into the gaia/external-apps/ folder. Read Building Prebundled web apps to find out how.

Important: If you are a device vendor creating a custom B2G/Gaia build for distribution, you need to satisfy certain criteria before you are allowed to include the Firefox Marketplace app on your phones/tablets/etc. Contact Mozilla for more details.