Make Android Great (Again) II - Termux advanced compilation manual

1. Before we start

The first article of the series: Make Android Great (Again) - Dive into Termux

This article assumes that you have learned the following:

  1. Know how to use an Android phone
  2. Install Termux on the mobile phone
  3. Know how to run commands on Termux
  4. Be familiar with using Linux

This article also supposes you've got the following equipment:

  1. An Android phone for installing and running Termux
  2. A computer, install Ubuntu LTS version (docker is recommended).

2. Some best practices that can improve efficiency

Before starting to explain how to compile Termux package compilation, it is necessary to share some of the best practices that can significantly improve efficiency.
Best practices can greatly improve efficiency, make the mood more pleasant, greatly reduce the time from getting started to giving up and reduce the probability.

Best practices for improving the efficiency of running commands in Termux

  1. Connect the Android phone to a Bluetooth keyboard
  2. Or, run the sshd server on your mobile phone (see below for details), and use computer to connect to the mobile phone's ssh through the LAN wifi, to control the mobile phone more easily

How to make Termux run sshd as openssh server?

Run the following commands on mobile phone's Termux APP:

pkg install openssh
passwd # set current user's password
sshd # start openssh-server

Run the following commands on the computer, or use other ssh connection software to fill in the corresponding parameters to connect:

ssh -p 8022 xxx@[your phone IP]

Best practices for improving compilation efficiency

Compile directly with Docker.
This way you will have fewer weird operating system exclusive problems. Regardless of whether the host machine is Windows, Mac or Linux itself, it is recommended to use docker to run the official Ubuntu image provided by Termux packages to compile.
In this way, the compilation environment can be started with one click, without installing too many toolchain program on your Ubuntu system, and there will be no environmental configuration problems.

3. Preparations before compilation

Common preparations before entering Docker

Clone the latest Termux packages repository:

git clone https://github.com/termux/termux-packages.git
cd termux-packages

Run the docker script inside the Termux packages to create a compilation environment:

bash scripts/run-docker.sh

After entering the docker environment, you can freely run various compilation commands.

Advanced tips for the Termux packages docker environment

Custom parameters before entering Docker:

# Customize container name, e.g.:
export CONTAINER_NAME=hhj_termux_armv8

# Customize the mirror address, e.g.:
export TERMUX_BUILDER_IMAGE_NAME=ghcr.io/termux/package-builder

# Finally, run the docker environment and automatically apply the above parameters
bash scripts/run-docker.sh

How to access host machine's 127.0.0.1 inside docker?
Edit scripts/run-docker.sh, add --network host \ in the appropriate position below $SUDO docker run \

How to mount more local directories?
Add --volume [host directory]:[docker directory] \ below --volume $VOLUME \

Some useful custom settings after entering Docker

After entering docker, you can set the following environment variables and set them as needed.

# Network proxy settings
export https_proxy=http://127.0.0.1:7071
export http_proxy=http://127.0.0.1:7071
export no_proxy="localhost,127.0.0.1,dl.google.com"

# Set compilation parameters, adjust as needed
export TERMUX_PKG_API_LEVEL=24
export TERMUX_PKG_MAINTAINER=HHJ
export TERMUX_ARCH=aarch64 # arm, aarch64, i686 or x86_64.
export TERMUX_TOPDIR=/home/builder/termux-packages/termux-build # compile output directory

# Path setting, enhance compatibility
export ANDROID_HOME=/home/builder/lib/android-sdk-9123335
export NDK=/home/builder/lib/android-ndk-r25c
export PATH=/home/builder/termux-packages/termux-build/_cache/cmake-3.24.2/bin:$PATH

Manually run compilation commands without scripts (optional)

Usually suitable for debugging, when you need to manually run compilation commands in docker, without using scripts.
The following environment variables can be used to configure the environment for cmake, make and other commands.
For more detailed information, please refer to: https://developer.android.com/ndk/guides/other_build_systems?hl=zh-cn

Note: Do not use host build or host build
Note 2: If you still don't understand what this section is talking about, just skip this section.

export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
export TARGET=aarch64-linux-android
export API=24
export AR=$TOOLCHAIN/bin/llvm-ar
export CC=$TOOLCHAIN/bin/$TARGET$API-clang
export AS=$CC
export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
export LD=$TOOLCHAIN/bin/ld
export RANLIB=$TOOLCHAIN/bin/llvm-ranlib
export STRIP=$TOOLCHAIN/bin/llvm-strip

export SETUPTOOLS_USE_DISTUTILS=local

4. Start compiling the Termux packages

First make sure that you have configured and entered docker according to the previous section.

Take compiling Python as an example. By default, the dependencies are compiled first, and then the ontology is compiled:

./build-package.sh python

If you want to download dependencies from the official apt repo, only compile python itself locally:

./build-package.sh -I python

How to compile legacy software?

For example, inside Termux's official apt repo, python has reached python 3.11, and the official apt repo only keeps the latest version, meaning the old version of Python can no longer be installed through apt install or pkg install.

If we want to use Python3.10 again, what should we do? We can compile her by ourselves. The following are the detailed steps.

  1. Switch the git repository back to the python 3.10 script
cd [git repository directory of your termux-packages]
git checkout bootstrap-2022.10.23-r1+apt-android-7 # The latest version that supports python 3.10

Confirm the Python version number in packages/python/build.sh, namely _MAJOR_VERSION=3.10, which proves that the switching was successful.

  1. Clean up the compilation cache
# Clear the flags of compiled packages
rm -rf /data/data/.built-packages

# Clear all compiled caches
rm -rf $TERMUX_TOPDIR && mkdir $TERMUX_TOPDIR
  1. Start compiling

Familiar formula:

./build-package.sh python

Note: It is not recommended to use the -I parameter, because many apt software inside the official apt repo have been updated, and old versions of Python dependencies may have compatibility issues. It is better to recompile them all through source code.

  1. Installation

After the compilation is successful, copy all the files in the output directory to the phone
Then execute the following command on your mobile phone's Termux APP, to install them manually (note that the dependencies should be installed first and then the main ones):

dpkg -i xxx.deb

How to self-host the apt service after compiling?

After compiling, we might find that dpkg installation is too troublesome to deal with various dependencies, so how to create an apt service that can be hosted by yourself?

Suppose you already have a web service.

Inside docker or Linux host machine, use the following command to build apt related files and directories:

pip3 install termux-apt-repo # https://github.com/termux/termux-apt-repo

# Generate the apt directory files in the directory named "backup"
cd xxx/termux-packages
termux-apt-repo --use-hard-links output backup stable main

# Synchronize to the directory of the web service (if you use ftp or other methods, please work around it yourself)
rsync --delete -rvP backup/dists xxx/www/html/termux-packages # Assume that this directory corresponds to https://hhj.name/termux-packages

Then execute the following command on the Termux of the mobile phone

echo "deb [trusted=yes] https://hhj.name/termux-packages/ stable main" > $PREFIX/etc/apt/sources.list
cat <<EOF > $PREFIX /etc/termux/chosen_mirrors
WEIGHT=999
https://hhj.name/termux-packages/
EOF

apt update
pkg search python # Confirm that python is the version compiled by yourself
pkg install python # Happy to install the python compiled by myself

5. More reference documents

Leave a comment