Skip to content
Bobby Galli edited this page Apr 16, 2025 · 2 revisions

Source: https://curl.se/docs/install.html#android

Steps

  1. Clone curl
git clone https://github.com/curl/curl
  1. Install the Android NDK (tested with 27.2.12479018)
  2. Create a new build_android.sh script with the following contents
#!/bin/bash

set -e

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SOURCE_DIR="$SCRIPT_DIR" # Assuming the script is in the curl source root

# --- Configuration ---
DEFAULT_ANDROID_API_LEVEL=29
ABIS=("armeabi-v7a" "arm64-v8a" "x86" "x86_64")
# ---

# --- Argument Parsing ---
if [ -z "$1" ]; then
  echo "Usage: $0 <path_to_android_ndk>"
  echo "You can also set the ANDROID_NDK_HOME environment variable."
  if [ -z "$ANDROID_NDK_HOME" ]; then
    exit 1
  else
    echo "Using ANDROID_NDK_HOME from environment: $ANDROID_NDK_HOME"
  fi
else
  export ANDROID_NDK_HOME="$1"
  echo "Using ANDROID_NDK_HOME from argument: $ANDROID_NDK_HOME"
fi

if [ ! -d "$ANDROID_NDK_HOME" ]; then
  echo "Error: ANDROID_NDK_HOME directory not found: $ANDROID_NDK_HOME"
  exit 1
fi

CMAKE_TOOLCHAIN_FILE="$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake"
if [ ! -f "$CMAKE_TOOLCHAIN_FILE" ]; then
    echo "Error: CMake toolchain file not found: $CMAKE_TOOLCHAIN_FILE"
    exit 1
fi

ANDROID_API_LEVEL=${ANDROID_API_LEVEL:-$DEFAULT_ANDROID_API_LEVEL}
echo "Using Android API Level: $ANDROID_API_LEVEL"
# ---

echo "Building curl for Android ABIs: ${ABIS[*]}"
echo "Source directory: $SOURCE_DIR"

for ABI in "${ABIS[@]}"; do
  BUILD_DIR="$SOURCE_DIR/_build/android/$ABI"
  INSTALL_DIR="$SOURCE_DIR/_install/android/$ABI"

  echo ""
  echo "--------------------------------------------------"
  echo " Building for ABI: $ABI"
  echo " Build directory: $BUILD_DIR"
  echo " Install directory: $INSTALL_DIR"
  echo "--------------------------------------------------"

  mkdir -p "$BUILD_DIR"
  rm -rf "$BUILD_DIR"/* # Clean previous build

  # Use a subshell to avoid polluting the main environment and manage cd
  (
    cd "$BUILD_DIR" && \
    cmake "$SOURCE_DIR" \
        -DCMAKE_BUILD_TYPE=Release \
        -DANDROID_ABI="$ABI" \
        -DANDROID_PLATFORM="android-$ANDROID_API_LEVEL" \
        -DCMAKE_TOOLCHAIN_FILE="$CMAKE_TOOLCHAIN_FILE" \
        -DCMAKE_INSTALL_PREFIX="$INSTALL_DIR" \
        -DBUILD_SHARED_LIBS=ON \
        -DBUILD_TESTING=OFF \
        -DCURL_ENABLE_SSL=OFF \
        -DCURL_USE_LIBPSL=OFF && \
    cmake --build . --target install --config Release -j $(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)
  ) || { echo "Error building for ABI $ABI"; exit 1; }

  echo "Successfully built and installed for ABI: $ABI"
done

echo ""
echo "--------------------------------------------------"
echo "All Android ABIs built successfully!"
echo "Install directories:"
for ABI in "${ABIS[@]}"; do
  echo "  $SOURCE_DIR/_install/android/$ABI"
done
echo "--------------------------------------------------" 
  1. Run the build script passing the path to your NDK install
./build_android.sh ~/Library/Android/sdk/ndk/27.2.12479018
  1. The output libcurl.so files can be found in the ABI directory inside of the _installs folder
Clone this wiki locally