Skip to content

Building for Android

Rohit Zambre edited this page Aug 4, 2016 · 52 revisions

Set up toolchain

First, we need some android development tools like the NDK, SDK, and toolchain, for cross-compiling:

To install the NDK and SDK, refer to the guidelines on the website. When you install the SDK, run the UI tool at tools/android (on Mac, this would be under /Users/<username>/Library/Android/sdk) and update the SDK in order to install a full set of Android libraries. You may use the Android-18 platform. From the commandline, you may also do tools/android - update sdk --no-ui.

First, create an Android toolchain from the NDK. Example command to setup standalone Android tool chain:
'ndk root'/build/tools/make-standalone-toolchain.sh --platform="android-18" --toolchain=arm-linux-androideabi-4.8 --install-dir='dir to install' --ndk-dir='ndk dir' --arch=arm

Set the following environment variables while building. (You may want to export them from a configuration file like .bashrc (~/.bash_profile for Mac OS X).)

ANDROID_SDK="/path/to/sdk"
ANDROID_NDK="/path/to/ndk"
ANDROID_TOOLCHAIN="/path/to/toolchain"
PATH="$PATH:$ANDROID_TOOLCHAIN/bin" # add the toolchain to your $PATH

If the source of error that caused issue #11920 still exists, then you will need to set these additional variables:

ANDROID_NATIVE_API_LEVEL=android-18
ANDROID_PLATFORM=android-18

If you're on Debian (not Ubuntu), you may need to install the packages below.

sudo apt-get install curl ia32-libs ant

If you're on Ubuntu, you may need to do:

sudo apt-get install curl libc6:i386 ant lib32z1 openjdk-7-jdk

If you're on OSX, install these packages:

brew install nspr ant

Build Servo

git clone https://github.com/mozilla/servo.git
cd servo

# Make sure these env vars are set:
export ANDROID_TOOLCHAIN="/path/to/toolchain"
export ANDROID_NDK="/path/to/ndk"
export PATH="$PATH:/path/to/toolchain/bin"

# Replace "--release" with "--dev" to create an unoptimized debug build.
./mach build --release --android
./mach package --release --android

Installing and running on-device

To install Servo on a hardware device, first set up your device for development.

Servo depends on some files that are not (yet) packaged as part of the APK. Before running Servo for the first time, you'll need to copy these files to your device. (You don't need to do this every time you build and install Servo; you only need to do it if files in the "resources" directory have changed.)

adb push resources /sdcard/servo

Then run this command to install the Servo package on your device. Replace --release with --dev if you are building in debug mode.

./mach install --release

To start Servo, tap the "Servo" icon in your launcher screen, or run this :

./mach run --android https://www.mozilla.org/

Force-stop:

adb shell am force-stop com.mozilla.servo/com.mozilla.servo.MainActivity

If the above doesn't work, try this:

adb shell am force-stop com.mozilla.servo

Uninstall:

adb uninstall com.mozilla.servo

Profiling

We are currently using a Nexus 9 for profiling, because it has an NVidia chipset and supports the NVidia System Profiler. First, install the profiler.

You will then need to root your Nexus 9. There are a variety of options, but I found the CF-Auto-Root version the easiest. Just follow the instructions on that page (download, do the OEM unlock, adb reboot bootloader, fastboot boot image/CF-Auto-Root-flounder-volantis-nexus9.img) and you should end up with a rooted device.

If you want reasonable stack backtraces, you should add the flags -fno-omit-frame-pointer -marm -funwind-tables to the CFLAGS (simplest place to do so is in the mach python scripts that set up the env for Android). Also, remember to build with -r for release!

Installing and running in the emulator

To set up the emulator, use the android tool installed with the SDK. Create a default Nexus7 device with an SDCard of size greater than 100MB. After creating it, open the file ~/.android/avd/nexus7.avd/config.ini and change the hw.dPad and hw.mainKeys configuration files to yes.

Installation - note that we install using -s to install to the SDCard, as there is insufficient space in default device images on the main disk:

adb push resources /sdcard/servo
cd target/arm-linux-androideabi/debug
adb install -s servo.apk

Running:

./mach run --android https://www.mozilla.org/

Force-stop:

adb shell am force-stop com.mozilla.servo

Uninstall:

adb uninstall com.mozilla.servo

Viewing RUST_LOG output

If you have enabled the RUST_LOG environment variable in main.rs, we will redirect those messages generated by the Rust runtime to the Android logging facilities. To display those items, use:

adb logcat

Debugging on-device

First, you will need to enable debugging in the project files by adding android:debuggable="true" to the application tag in servo/support/android/apk/AndroidManifest.xml.

cd target/arm-linux-androideabi/debug
~/android-ndk-r9c/ndk-gdb \
    --adb=/Users/larsberg/android-sdk-macosx/platform-tools/adb \
    --project=support/android/apk/
    --launch=com.mozilla.servo.MainActivity \
    --verbose

To get symbols resolved, you may need to provide additional library paths (at the gdb prompt):
set solib-search-path /Users/larsberg/servo/support/android/apk/obj/local/armeabi/:/Users/larsberg/servo/support/android/apk/libs/armeabi

OR you may need to enter the same path names as above in the support/android/apk/libs/armeabi/gdb.setup file.

If you are not able to get past the "Application Servo (process com.mozilla.servo) is waiting for the debugger to attach." prompt after entering continue at (gdb) prompt, you might have to set Servo as the debug app (Use the "Select debug app" option under "Developer Options" in the Settings app). If this doesn't work, Stack Overflow will help you.

The ndk-gdb debugger may complain about ... function not defined when you try to set breakpoints. Just answer y when it asks you to set breakpoints on future library loads. You will be able to catch your breakpoints during execution.

PandaBoard

If you are using a PandaBoard, Servo is known to run on Android with the instructions above using the following build of Android for PandaBoard: http://releases.linaro.org/12.10/android/leb-panda

Important Notices.

Different from Linux or Mac, on Android, Servo's program entry is in the library, not executable. So we can't execute the program with command line arguments. To approximate command line arguments, we have a hack for program entry on android: You can put command-line arguments, one per line, in the file /sdcard/servo/android_params on your device. You can find a default android_params property under resources in the Servo repo.

Default settings:

default font directory : /system/fonts (in android)
default resource path : /sdcard/servo (in android)
default font configuration : /sdcard/servo/.fcconfig (in android)
default fallback font : Roboto
Clone this wiki locally