Location>code7788 >text

Ubuntu 64 compiles android arm64-v8a with the openssl static library and the

Popularity:910 ℃/2024-09-19 11:18:30
#!/bin/bash
# Cross-compile environment for Android on ARM64 and x86
#
# Contents licensed under the terms of the OpenSSL license
# /source/
#
# See //FIPS_Library_and_Android
#   and //Android

#####################################################################

# Set ANDROID_NDK_ROOT to your NDK location. For example,
# /opt/android-ndk-r8e or /opt/android-ndk-r9. This can be done in a
# login script. If ANDROID_NDK_ROOT is not specified, the script will
# try to pick it up with the value of _ANDROID_NDK_ROOT below. If
# ANDROID_NDK_ROOT is set, then the value is ignored.
_ANDROID_NDK="android-ndk-r14b"
# _ANDROID_NDK="android-ndk-r10"

# Set _ANDROID_EABI to the EABI you want to use. You can find the
# list in $ANDROID_NDK_ROOT/toolchains. This value is always used.
_ANDROID_EABI="aarch64-linux-android-4.9"

# Set _ANDROID_ARCH to the architecture you are building for.
_ANDROID_ARCH=arch-arm64

# Set _ANDROID_API to the API you want to use. You should set it
# to one of: android-14, android-9, android-8, android-14, android-5
# android-4, or android-3. You can't set it to the latest (for
# example, API-17) because the NDK does not supply the platform.
_ANDROID_API="android-24"

#####################################################################

# If the user did not specify the NDK location, try and pick it up.
if [ -z "$ANDROID_NDK_ROOT" ]; then

  _ANDROID_NDK_ROOT=""
  if [ -d "/usr/local/$_ANDROID_NDK" ]; then
    _ANDROID_NDK_ROOT="/usr/local/$_ANDROID_NDK"
  fi

  if [ -d "/opt/$_ANDROID_NDK" ]; then
    _ANDROID_NDK_ROOT="/opt/$_ANDROID_NDK"
  fi

  if [ -d "$HOME/$_ANDROID_NDK" ]; then
    _ANDROID_NDK_ROOT="$HOME/$_ANDROID_NDK"
  fi

  if [ -d "$PWD/$_ANDROID_NDK" ]; then
    _ANDROID_NDK_ROOT="$PWD/$_ANDROID_NDK"
  fi

  # If a path was set, then export it
  if [ ! -z "$_ANDROID_NDK_ROOT" ] && [ -d "$_ANDROID_NDK_ROOT" ]; then
    export ANDROID_NDK_ROOT="$_ANDROID_NDK_ROOT"
  fi
fi

# Error checking
if [ -z "$ANDROID_NDK_ROOT" ] || [ ! -d "$ANDROID_NDK_ROOT" ]; then
  echo "Error: ANDROID_NDK_ROOT is not a valid path. Please edit this script."
  exit 1
fi

if [ ! -d "$ANDROID_NDK_ROOT/toolchains" ]; then
  echo "Error: ANDROID_NDK_ROOT/toolchains is not a valid path. Please edit this script."
  exit 1
fi

if [ ! -d "$ANDROID_NDK_ROOT/toolchains/$_ANDROID_EABI" ]; then
  echo "Error: ANDROID_EABI is not a valid path. Please edit this script."
  exit 1
fi

#####################################################################

# Based on ANDROID_NDK_ROOT, try and pick up the required toolchain.
ANDROID_TOOLCHAIN=""
for host in "linux-x86_64" "linux-x86" "darwin-x86_64" "darwin-x86"
do
  if [ -d "$ANDROID_NDK_ROOT/toolchains/$_ANDROID_EABI/prebuilt/$host/bin" ]; then
    ANDROID_TOOLCHAIN="$ANDROID_NDK_ROOT/toolchains/$_ANDROID_EABI/prebuilt/$host/bin"
    break
  fi
done

# Error checking
if [ -z "$ANDROID_TOOLCHAIN" ] || [ ! -d "$ANDROID_TOOLCHAIN" ]; then
  echo "Error: ANDROID_TOOLCHAIN is not valid. Please edit this script."
  exit 1
fi

case $_ANDROID_ARCH in
  arch-arm)
    ANDROID_TOOLS="arm-linux-androideabi-gcc arm-linux-androideabi-ranlib arm-linux-androideabi-ld"
    CROSS_COMPILE="arm-linux-androideabi-"
    ;;
  arch-arm64)
    ANDROID_TOOLS="aarch64-linux-android-gcc aarch64-linux-android-ranlib aarch64-linux-android-ld"
    CROSS_COMPILE="aarch64-linux-android-"
    ;;
  arch-x86)
    ANDROID_TOOLS="i686-linux-android-gcc i686-linux-android-ranlib i686-linux-android-ld"
    CROSS_COMPILE="i686-linux-android-"
    ;;
  *)
    echo "ERROR ERROR ERROR: Unknown architecture $_ANDROID_ARCH"
    exit 1
    ;;
esac

for tool in $ANDROID_TOOLS
do
  # Error checking
  if [ ! -e "$ANDROID_TOOLCHAIN/$tool" ]; then
    echo "Error: Failed to find $tool. Please edit this script."
    exit 1
  fi
done

# Only modify/export PATH if ANDROID_TOOLCHAIN is good
if [ ! -z "$ANDROID_TOOLCHAIN" ]; then
  export ANDROID_TOOLCHAIN="$ANDROID_TOOLCHAIN"
  export PATH="$ANDROID_TOOLCHAIN":"$PATH"
fi

#####################################################################

# For the Android SYSROOT. Can be used on the command line with --sysroot
export ANDROID_SYSROOT="$ANDROID_NDK_ROOT/platforms/$_ANDROID_API/$_ANDROID_ARCH"
export CROSS_SYSROOT="$ANDROID_SYSROOT"
export NDK_SYSROOT="$ANDROID_SYSROOT"

# Error checking
if [ -z "$ANDROID_SYSROOT" ] || [ ! -d "$ANDROID_SYSROOT" ]; then
  echo "Error: ANDROID_SYSROOT is not valid. Please edit this script."
  exit 1
fi

#####################################################################

# Set other environment variables for the build
export MACHINE=aarch64
export RELEASE=2.6.37
export SYSTEM=android
export ARCH=arm64
export CROSS_COMPILE="aarch64-linux-android-"

export ANDROID_DEV="$ANDROID_NDK_ROOT/platforms/$_ANDROID_API/$_ANDROID_ARCH/usr"
export HOSTCC=gcc

VERBOSE=1
if [ ! -z "$VERBOSE" ] && [ "$VERBOSE" != "0" ]; then
  echo "ANDROID_NDK_ROOT: $ANDROID_NDK_ROOT"
  echo "ANDROID_ARCH: $_ANDROID_ARCH"
  echo "ANDROID_EABI: $_ANDROID_EABI"
  echo "ANDROID_API: $ANDROID_API"
  echo "ANDROID_SYSROOT: $ANDROID_SYSROOT"
  echo "ANDROID_TOOLCHAIN: $ANDROID_TOOLCHAIN"
  echo "CROSS_COMPILE: $CROSS_COMPILE"
  echo "ANDROID_DEV: $ANDROID_DEV"
fi

The code above isconfiguredOpenSSL compilation options (shell script). Copy the code and save it as.

Openssl repository download link:

1. Start compiling

Terminal set ndk path: export ANDROID_NDK_ROOT=/home/lipan/androidsdk/android-ndk-r14b

2. Execute shell scripts: source command

commander-in-chief (military) Move to the end of the openssl source folder and start a terminal there and type: source . /

3. Create a folder for outputting static libraries(Desktop path)

   mkdir /home/lipan/Desktop/output/  

4. Cleaning up

     make clean

   
5、Configuring openssl
./Configure android-arm64 \
    no-shared \
    no-ssl2 \
    no-ssl3 \
    no-comp \
    no-hw \
    no-engine \
    --openssldir=/home/lipan/Desktop/output/$ANDROID_API \
    --prefix=/home/lipan/Desktop/output/$ANDROID_API

6、Compile (these 2 lines of code below are correct)
make depend

make all -j$(nproc)
make all -j$(nproc) command to enable parallelization when compiling software,to utilize the system's multiple CPU crux
-j$(nproc)-j Flags are used to specify concurrently running jobs(or process)quantities。$(nproc) an shell command,It returns the number of available processing units(assume (office) CPU Number of cores)
When you run make all -j$(nproc) hour,It will tell make Comparison of the use of CPU Parallel jobs with equal number of cores,This can significantly speed up the build process。

7, the final step: generate a static library
make install