I write this article so that no one will take anymore his knowledges only for himself about this argument whitout sharing (then helping) just to boast his ego.
When you look at Android you must think to Linux. Android was originally made as a Linux ‘hack’ to be put on mobile devices (smartphones and tablets); subsequently its use spreaded on other devices, for example Google Home.
My prevision is that soon we will start to see personal computers running Android instead of Windows, Linux or macOS and always more mobile devices instead of personal computers.
I’m saying this because I constantly tracked down the activities on my website: while at the begins it was a ‘computer only’ with a 0.8-1% of mobiles traffic, now the smartphone and tablet users reached something like the 48%(!) of the total usage of it.
This is the way I made (PC ONLY!), I’ve tried with MSYS2 in Windows but it didn’t work. I’m not saying that is the only way to do it, but I’m saying that it works!
1) Install Linux Mint on your computer or on a virtual machine you have created: you can use the free VMware Workstation Player for Windows to do that without even paying anything. The system must be installed on the (virtual or physical) hard drive, instead of ran on the fly like many Linux distribution do nowadays otherwise it won't work.
By installing this distribution you will have both the make and the gcc packages already installed on your Linux, so you won’t have to make any more effort in this sense.
2) Download the NDK software from here
3) Unpack the folder inside the NDK zip package in your Home folder. When you install Linux you have by default a ‘Home’ folder.
4) Let’s say the user you created in the installation is max, you will have to open the file .bashrc inside your home folder (if you don’t see it click on View on Nemo, the default file browser, then click on Show Hidden Files).
Once opened type
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
Save your .bashrc file and close the text editor. Now you have all the compilers installed.
5) Download the Stockfish source package from https://github.com/official-stockfish/Stockfish (click on Code -> Download ZIP)
6) Extract the Stockfish package directly in Home
7) Start the command line and go in the Stockfish-master/src directory (usually type cd Stockfish-master/src then press Enter,
then cd src
8) Do a make profile-build ARCH=x86-64 so the compiler downloads the NNUE file (the actual is nn-baeb9ef2d183.nnue)
9)Type make clean
10) To compile for android you can just use these three commands:
- make build ARCH=armv8 COMP=ndk
make stockfish COMP=ndk COMPCC="/home/max/ndk/android-ndk-r21d/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=aarch64-linux-android21" ARCH=armv8
if you want to compile for 64bit Androids (the nowadays widest used),
- make build ARCH=armv7-neon COMP=ndk
make stockfish_polyglot COMP=ndk COMPCC="/home/max/ndk/android-ndk-r21d/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=armv7a-linux-androideabi21" ARCH=armv7-neon
if you want to compile for 32bit Androids supporting the neon instructions
- make build ARCH=armv7 COMP=ndk
make stockfish_polyglot COMP=ndk COMPCC="/home/max/ndk/android-ndk-r21d/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=armv7a-linux-androideabi21" ARCH=armv7
if you want to compile for standard 32bit Androids (oldest devices)
11) Now you’ll have to strip the stockfish binary, if your compilation succeeded. To strip means to reduce the size of the binary so that it’s faster.
Let’s still take that your user is max:
to strip the 64bit produced binary
to strip the two 32bit binaries mentioned above.
12) Move the produced ‘stockfish’ binary in some other folder or directly in your phone (inside for example Droidfish/uci).
13) Type make clean on your command prompt in order to clean the Stockfish-master/src folder.
If you compile program like Cfish supporting the non embedding make sure to set embed=yes in the Makefile. This will make it working on Android everywhere.
If you want to give a different name than just "stockfish" or "cfish" or "stockfish_polyglot" please don't rename the compiled binary but change the name in the Makefile before the compilation, above in the text file, and then make a compilation (after having saved the modified Makefile with the new name): you should see an EXE = stockfish, or EXE = cfish, change the name there otherwise the program won't be accepted by Acid Ape.
You will get a lot of errors and make a lot of misunderstandings the first times you try these procedures, but don’t worry: with training things will become a lot easier and you'll succeed at the end. If you need help I'm here.
If you have to compile other Stockfish-like programs like Cfish, Stockfish Polyglot or CorChess please bear in mind that they produce binaries will different names:
- cfish for Cfish
- stocfish_polyglot for Stockfish Polyglot
- corchess for CorChess
so you will have to change the name at step 11) ( for example strip stockfish_polyglot).
Stockfish source: https://github.com/official-stockfish/Stockfish
Stockfish Polyglot source: https://github.com/massimilianogoi/Stockfish-Polyglot
Cfish source: https://github.com/syzygy1/Cfish
CorChess source: https://github.com/IIvec/Stockfish/tree/corchess