Compiling from source (Android) - dav1312/Wiki-test GitHub Wiki
Warning
Running Stockfish, especially with multiple threads, is computationally intensive and can drain your device's battery quickly.
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=clangAfter the compilation finishes, the binary will be located at Stockfish/src/stockfish.
This method requires a computer to build the Android binary.
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.
The setup process differs for Windows and Linux.
For Windows (using MSYS2)
-
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.
-
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
expectpackage is optional and only needed if you want to run the test suite. -
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).
-
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:
-
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"
-
Set the NDK Path and Verify. Now, go back to your MSYS2 terminal. Add the NDK to your session's
PATHand verify that the compiler is found.export PATH=/android-ndk/bin:$PATH
Note: This
exportcommand 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
-
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 expectNote: The
expectpackage is optional and only needed if you want to run the test suite. -
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).
-
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
exportcommand is only for the current terminal session.Now, check that you can access the compiler:
$ aarch64-linux-android21-clang++ --version
These steps are the same for all PC operating systems.
-
Download Stockfish Source Code.
git clone https://github.com/official-stockfish/Stockfish.git cd Stockfish/src -
Download the Neural Network. This command prepares the network to be embedded in the binary.
make net
-
Compile the Engine. Run the
makecommand, specifying your target architecture. We will usearmv8as the example.make -j build ARCH=armv8 COMP=ndk
When finished, you will have a
stockfishfile in thesrcdirectory. A successful compilation will end with messages like this:aarch64-linux-android21-clang++ -o stockfish ... make[1]: Leaving directory '.../Stockfish/src'
-
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
-
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
-
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.
- Copy the final binary (e.g.,