Setup the Android Build Environment

Doing a local build of the Android Components:

This document describes how to make local builds of the Android bindings in this repository. Most consumers of these bindings do not need to follow this process, but will instead use pre-built bindings.

Prepare your build environment

Typically, this process only needs to be run once, although periodically you may need to repeat some steps (e.g., rust updates should be done periodically)

Setting up Android dependencies

At the end of this process you should have the following environment variables set up.

  • ANDROID_HOME
  • ANDROID_NDK_ROOT
  • JAVA_HOME

The easiest way to install all the dependencies (and automatically handle updates), is by using Android Studio. Once this is installed, it must be run and the Glean project opened to complete initial setup. If Android Studio asks you to upgrade the version of Gradle, decline.

The following dependencies can be installed in Android Studio through Tools > SDK Manager > SDK Tools:

  • Android SDK Tools (may already be selected)
  • NDK r20
  • CMake
  • LLDB

With the dependencies installed, note down the Android SDK Location in Tools > SDK Manager. Set the ANDROID_HOME environment variable to that path. The ANDROID_NDK_ROOT can be set to ANDROID_NDK_ROOT=$ANDROID_HOME/ndk-bundle. Set JAVA_HOME to be the location of Android Studio's JDK which can be found in Glean's "Project Structure" menu. (You may need to escape spaces in the path).

If you want to install the NDK manually:

  1. Download NDK r20 from https://developer.android.com/ndk/downloads.
  2. Extract it and put it somewhere ($HOME/.android-ndk-r20 is a reasonable choice, but it doesn't matter).
  3. Set ANDROID_NDK_ROOT to this path.
    • Set ANDROID_NDK_HOME to match ANDROID_NDK_ROOT, for compatibility with some android Gradle plugins.

Setting up Rust

Rust can be installed using rustup, with the following commands:

  • curl https://sh.rustup.rs -sSf | sh
  • rustup update

Platform specific toolchains need to be installed for Rust. This can be done using the rustup target command. In order to enable building to real devices and Android emulators, the following targets need to be installed:

  • rustup target add aarch64-linux-android
  • rustup target add armv7-linux-androideabi
  • rustup target add i686-linux-android
  • rustup target add x86_64-linux-android

Building

This should be relatively straightforward and painless:

  1. Ensure your repository is up-to-date.

  2. Ensure Rust is up-to-date by running rustup update.

  3. The builds are all performed by ./gradlew and the general syntax used is ./gradlew project:task

    You can see a list of projects by executing ./gradlew projects and a list of tasks by executing ./gradlew tasks.

The above can be skipped if using Android Studio. The project directory can be imported and all the build details can be left to the IDE.

FAQ

  • Q: Android Studio complains about Python not being found when building.

  • A: Make sure that the python binary is on your PATH. On Windows, in addition to that, it might be required to add its full path to the rust.pythonCommand entry in $project_root/local.properties.

  • Q: Android Studio complains about Toolchain for X does not exist.

  • A: On Windows, make sure that the ndk.dir property in $project_root/local.properties points to an existing directory containing the Android NDK.