Compiling from source (Android) - dav1312/Wiki-test GitHub Wiki

Android

Warning

Running Stockfish, especially with multiple threads, is computationally intensive and can drain your device's battery quickly.

Compile on Android with Termux

You can build Stockfish directly on your Android device using the Termux app. This avoids the need for a separate computer or the Android NDK.

Open Termux and run the following commands:

# Update packages and install required tools
pkg update
pkg install git build-essential clang make wget

# Download the Stockfish source code
git clone https://github.com/official-stockfish/Stockfish.git

# Navigate into the source directory and compile
cd Stockfish/src
make -j profile-build COMP=clang

After the compilation finishes, the binary will be located at Stockfish/src/stockfish.

Cross-Compile on a PC

This method requires a computer to build the Android binary.

Supported Architectures

You must choose the correct architecture for your device.
Most modern devices use armv8.

  • armv8-dotprod: For the latest ARM CPUs with dot product support (best performance).
  • armv8: For modern 64-bit ARM CPUs (most common).
  • armv7-neon: For older 32-bit ARM CPUs with NEON support.
  • armv7: For very old 32-bit ARM CPUs without NEON.

Step 1: Environment Setup

The setup process differs for Windows and Linux.

For Windows (using MSYS2)
  1. Install MSYS2. Please see how to compile on Windows for detailed instructions on installing and updating MSYS2.

    Note: You only need to install the basic MSYS environment. You do not need any of the MINGW64, MINGW32 nor the CLANG64 compiler toolchain for an Android build. We will use the Android NDK compiler toolchain instead.

  2. Install Build Tools. Once MSYS2 is installed, open the MSYS2 MINGW64 terminal and run the following command to install the necessary tools.

    pacman -S --needed base-devel git wget curl expect

    Note: The expect package is optional and only needed if you want to run the test suite.

  3. Download the Android NDK. You have two options:

    • Option A: Direct Download (Recommended). Go to the official NDK downloads page and download the latest version for Windows. Unzip it to a simple, memorable location, for example: C:\Android\Sdk\ndk.
    • Option B: Using Android Studio. If you have Android Studio installed, you can use its SDK manager to install the NDK. It will typically be installed under your Android SDK location (e.g., C:\Android\Sdk\ndk).

    Note: The minimum version required is r21e (21.4.7075529).

  4. Link the NDK to MSYS2. To make the NDK easily accessible, we will create a symbolic link.

    • First, open the Windows Command Prompt (CMD) as an Administrator.
    • Run the command below. You must replace <ndk_version> with the actual folder name of the NDK you downloaded (e.g., 23.1.7779620).
    mklink /D "C:\msys64\android-ndk" "C:\Android\Sdk\ndk\<ndk_version>\toolchains\llvm\prebuilt\windows-x86_64"
  5. Set the NDK Path and Verify. Now, go back to your MSYS2 terminal. Add the NDK to your session's PATH and verify that the compiler is found.

    export PATH=/android-ndk/bin:$PATH

    Note: This export command is only for the current terminal session.

    Now, check that you can access the compiler. The output should be similar to below.

    $ aarch64-linux-android21-clang++ --version
    Android (7019983 based on r365631c3) clang version 9.0.9 (...)
    Target: aarch64-unknown-linux-android21
    Thread model: posix
    InstalledDir: C:\msys64\android-ndk\bin

    If you get a "command not found" error, please check your paths.

For Linux
  1. Install Build Tools. Use your system's package manager to install the necessary tools. On Debian/Ubuntu-based systems, run:

    sudo apt update && sudo apt install build-essential git wget curl expect

    Note: The expect package is optional and only needed if you want to run the test suite.

  2. Download the Android NDK. Go to the official NDK downloads page and download the latest version for Linux. Unzip it to a location you can easily access (e.g., /home/user/Android/Sdk/ndk/).

    Note: The minimum version required is r21e (21.4.7075529).

  3. Set the NDK Path and Verify. Add the NDK compiler to your terminal's PATH. Replace <path_to_your_ndk_folder> with the actual path where you unzipped the NDK.

    # Example for Linux: export PATH="/home/user/Android/Sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH"
    export PATH="<path_to_your_ndk_folder>/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH"

    Note: This export command is only for the current terminal session.

    Now, check that you can access the compiler:

    $ aarch64-linux-android21-clang++ --version

Step 2: Build Stockfish

These steps are the same for all PC operating systems.

  1. Download Stockfish Source Code.

    git clone https://github.com/official-stockfish/Stockfish.git
    cd Stockfish/src
  2. Download the Neural Network. This command prepares the network to be embedded in the binary.

    make net
  3. Compile the Engine. Run the make command, specifying your target architecture. We will use armv8 as the example.

    make -j build ARCH=armv8 COMP=ndk

    When finished, you will have a stockfish file in the src directory. A successful compilation will end with messages like this:

    aarch64-linux-android21-clang++ -o stockfish ...
    make[1]: Leaving directory '.../Stockfish/src'
    

Step 3: Prepare and Use the Binary

  1. Optimize the Binary. Make the binary smaller and slightly faster by stripping debugging symbols. First, check the file type of the unstripped binary:

    $ file stockfish
    stockfish: ELF 64-bit LSB shared object, ARM aarch64, ... with debug_info, not stripped

    Now, run the strip command:

    make strip ARCH=armv8 COMP=ndk

    Checking the file again shows it has been stripped:

    $ file stockfish
    stockfish: ELF 64-bit LSB shared object, ARM aarch64, ... stripped
  2. Rename the Binary. It is good practice to rename the file so you can easily identify your self-compiled development version in the GUI.

    mv stockfish stockfish_DEV_armv8
  3. Transfer and Install.

    • Copy the final binary (e.g., stockfish_DEV_armv8) to your Android device. An easy way to do this is to place it in a shared network folder and access it from your phone.
    • Using a file manager on your phone, move the binary into your chess GUI's engine directory. For Droidfish, this is often a folder named uci. You can find more details in the Droidfish documentation.
    • Select your new engine from within the app's settings. For better performance, consider increasing the Hash memory (e.g., to 512 MB) and Threads in the engine configuration.
⚠️ **GitHub.com Fallback** ⚠️