Compile iperf for Android

2023/02/18


Edit: it seems this does not work properly, please see at the end of this post for an update.

Download the NDK from the official page and unzip in a directory of your choice. To simplify the usage later with iperf (which uses autotools), I furthermore created a shell script as described in the NDK documentation:

#!/bin/sh

export NDK=$(pwd)

export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
#export TARGET=aarch64-linux-android
export TARGET=armv7a-linux-androideabi
#export TARGET=i686-linux-android
#export TARGET=x86_64-linux-android
# Set this to your minSdkVersion.
export API=21
# Configure and build.
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

This exports all the necessary environment variables such that the configure script can pick up the right tools. Source it in the current shell:

. env.sh

Next, download iperf. It does not export any version tags, so I searched for 2.0.10 in the git log to find the right commit:

git clone https://git.code.sf.net/p/iperf2/code iperf2-code
cd iperf2-code
git checkout ce9d976ae6534f0cc93c4a4050f0a395ac8d9887
CXXFLAGS="-static-libstdc++" ./configure --host $TARGET
make -j$(nproc)

The -static-libstdc++ is necessary since otherwise you get the following error while executing iperf on the android device:

CANNOT LINK EXECUTABLE "/data/local/tmp/iperf-2.0.10": library "libc++_shared.so" not found

It should now be possible to use iperf v2.0.10 on the device.


Update: I was told that there is an easier correct way to achieve this:

simpler way to install crossbuild stuff: for arm 32bits:

sudo apt install crossbuild-essential-armel

for arm 64bits:

sudo apt install crossbuild-essential-arm64

then, for 32bits:

./configure --host=arm-linux-gnueabi

and for 64bits:

./configure --host=aarch64-linux-gnu

(this is on ubuntu systems, for others I don’t know)