Setting Up Android Development Builds for Expo Projects in Linux

Setting Up Android Development Builds for Expo Projects in Linux

Expo is a powerful platform that simplifies the development process for building cross-platform mobile applications using React Native. It provides an easy way to develop, test, and deploy mobile apps without the need for complex configuration. In this guide, we will walk you through the process of setting up local development builds for Expo projects in a Linux environment. A development build is required when a project requires a custom native code, a config plugin or a custom runtime version and it lets us iterate as quickly as possible.

To begin, we'll assume that you already have Node.js and npm or any other package manager installed on your Linux system. If not, you can install them by following the instructions provided by the Node.js website for your specific Linux distribution. Once you have Node.js and npm set up, you can proceed with the Expo development build setup.

Installing Java Development Kit

To run Android SDK, it is required to have Java Development Kit installed, It includes the Java Runtime Environment (JRE), compilers, and various tools necessary for Java development. We will be using the popular OpenJDK which is available across many different platforms. OpenJDK (Open Java Development Kit) is a free and open-source implementation of the Java Platform, Standard Edition (Java SE). It serves as an alternative to Oracle's proprietary Java Development Kit (JDK) and provides developers with a fully functional Java development environment.

To install it on different Linux distros, it is mostly available on all major package repositories including Debian, Arch based etc.

  • Ubuntu/Debian-based distributions:

      sudo apt update
      sudo apt install openjdk-17-jdk
    
  • Red Hat-based distributions (Fedora, CentOS, RHEL):

      sudo yum install java-17-openjdk
    
  • Arch-based distributions (Arch Linux, Manjaro):

      sudo pacman -Syu jdk17-openjdk
    

    Note: At the moment of writing this article OpenJDK 17 is the recent LTS(Long-Term Support) version available.

After the installation is complete, you can verify it by checking the Java version:

java -version

To find the location where OpenJDK is installed on your Linux system, you can use the which command. As we will be needing the installed location for setting a JAVA_HOME environment variable.

which java

Additionally, you can use the readlink command with the --canonicalize option to get the canonicalized path of the Java executable. This command will resolve any symbolic links and provide the actual file path. For example

readlink --canonicalize $(which java)

An output like this will be printed on the console.

/usr/lib/jvm/java-17-openjdk-amd64/bin/java

Note: This might vary depending on the Linux distro or machine where the command is run.

We can then open our terminal and navigate to our home directory. Edit the .bashrc or .bash_profile file using a text editor. For example, you can use the command nano ~/.bashrc. Add the following lines to the file, replacing <opendk-root> with the actual path to your Java SDK root directory:

export JAVA_HOME=<opendk-root> 
#eg on Ubunut export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ 
export PATH=$PATH:$JAVA_HOME/bin

Android Command Line Tools

Unlike using Android Studio's SDK Manager which automatically downloads, installs and updates Android SDK packages. In Expo apps, since we will probably be using a simple IDE like Visual Studio Code and not Android Studio, we can use the sdkmanager command line tools for installing and updating Android SDK packages. Head over to the Android Studio download page and download the Command Line Tools for Linux.

The sdkmanager is a command-line tool that lets you view, install, update, and uninstall packages for the Android SDK. To use the SDK Manager to install a version of the command-line tools, follow these steps:

  1. Download the latest "command line tools only" package from the Android Studio downloads page and unzip the package.

  2. Move the unzipped cmdline-tools directory into a new directory of your choice, such as android_sdk. This is your Android SDK root.

  3. In the unzipped cmdline-tools directory, create a sub-directory called latest.

  4. Move the original cmdline-tools directory contents, including the lib directory, bin directory, NOTICE.txt file, and source.properties file, into the newly created latest directory. You can now use the command-line tools from this location.

To use Android command line tools from any directory in the terminal, we need to set up the required Environment Variables

  1. Open a terminal and navigate to your home directory.

  2. Edit the .bashrc or .bash_profile file using a text editor. For example, you can use the command nano ~/.bashrc.

  3. Add the following lines to the file, replacing <sdk-root> with the actual path to your Android SDK root directory:

export ANDROID_HOME=/home/<username>/<sdk-root>
export PATH=$PATH:$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/platform-tools

Save the file and exit the text editor. e. To apply the changes, either restart the terminal or run the command source ~/.bashrc or source ~/.bash_profile.

To be able to use the Android SDK tools we need to accept licenses. Execute the following command in your terminal:

sdkmanager --licenses

After the licenses have been accepted we can then install platform-tools using the sdkmanager cli tool, which includes Android adb and other tools which will be used to connect our Android devices for debugging, It is necessary to enable USB debugging on the device and allow it before a device can be used for debugging. To install platform-tools execute the following command.

sdkmanager "platform-tools"

Set up a new expo project, in the root of the project open a terminal and run expo run:android -d , before running this command make sure the android device has been connected through a USB, it will display the connected device and click enter. Depending on the Expo version, it will automatically download and set up Gradle, and download Android SDK packages including Android NDK, CMAKE, Build Tools etc to the Android SDK root directory. Running this for the first time would take some time. After a successful build, it will request to allow the installation of the built Android application, press allow, we now have a local development build installed on our device.

This guide, when properly followed can be used to setup up a local development build for Android on other Operating Systems including both macOS and Windows, what varies will be how to install the OpenJDK and the Android command line tools for each platform and how we set the environment variables for each platform.