如何为Android的arm/arm64/x86/x86_64架构交叉编译FreeType2库?
Hey there! Compiling FreeType2 for multiple Android architectures (arm, arm64, x86, x86_64) is totally manageable using the Android NDK's standalone toolchains. Let me walk you through the full, actionable process step by step:
- Make sure you've downloaded and extracted the Android NDK (r18+ is recommended for compatibility with
make_standalone_toolchain.py; newer NDK versions work too, though the toolchain script is marked deprecated in 25+—we'll stick with it here for broad support). - Grab the latest stable FreeType2 source code, extract it to a local directory of your choice.
Run the make_standalone_toolchain.py command tailored to each target architecture. Adjust the --api value to match your minimum supported Android version (e.g., 26 = Android 8.0) and set a unique --install-dir for each toolchain to avoid conflicts.
For arm (armeabi-v7a)
python $NDK_PATH/build/tools/make_standalone_toolchain.py \ --arch arm \ --api 26 \ --stl=libc++ \ --install-dir=my-arm-toolchain
For arm64 (arm64-v8a)
python $NDK_PATH/build/tools/make_standalone_toolchain.py \ --arch arm64 \ --api 26 \ --stl=libc++ \ --install-dir=my-arm64-toolchain
For x86
python $NDK_PATH/build/tools/make_standalone_toolchain.py \ --arch x86 \ --api 26 \ --stl=libc++ \ --install-dir=my-x86-toolchain
For x86_64
python $NDK_PATH/build/tools/make_standalone_toolchain.py \ --arch x86_64 \ --api 26 \ --stl=libc++ \ --install-dir=my-x86_64-toolchain
Note: If you're on NDK 25+, you can switch to ndk-build or CMake workflows instead, but the above commands work reliably for most commonly used NDK versions.
You'll repeat this section for each architecture—just swap out the toolchain path and target host value as needed. Let's use arm64 as an example:
- Navigate to the FreeType2 source directory
cd path/to/your/freetype2-source
- Configure cross-compilation environment variables
# Point to the toolchain for your target architecture export TOOLCHAIN=`pwd`/../my-arm64-toolchain export PATH=$TOOLCHAIN/bin:$PATH # Set target host (match this to your architecture): # arm → arm-linux-androideabi # arm64 → aarch64-linux-android # x86 → i686-linux-android # x86_64 → x86_64-linux-android export TARGET_HOST=aarch64-linux-android # Map NDK tools to environment variables export AR=$TARGET_HOST-ar export AS=$TARGET_HOST-as export CC=$TARGET_HOST-clang export CXX=$TARGET_HOST-clang++ export LD=$TARGET_HOST-ld export RANLIB=$TARGET_HOST-ranlib export STRIP=$TARGET_HOST-strip
Pro tip: Using clang instead of gcc is recommended—It's the official NDK compiler and has better compatibility with modern Android versions.
- Configure FreeType2 build options
# Create a dedicated build directory for the architecture mkdir -p build/arm64 cd build/arm64 # Run configure with cross-compilation settings ../../configure \ --prefix=`pwd`/output \ --host=$TARGET_HOST \ --enable-static=yes \ --enable-shared=yes \ --without-harfbuzz \ --without-bzip2 \ --without-png \ --without-zlib
*Explanation:
--prefix: Sets where compiled headers and libraries will be installed--enable-static/--enable-shared: Toggle static/dynamic library generation--without-xxx: Disable unused dependencies (remove these flags if you need HarfBuzz, PNG, etc.—just make sure you've compiled those libraries first)*
- Compile and install
# Use all available CPU cores to speed up compilation make -j$(nproc) # Install the compiled files to the prefix directory make install
Once done, the build/arm64/output folder will contain your architecture-specific headers (include) and libraries (lib).
Repeat this entire section for arm, x86, and x86_64—just update the toolchain path and TARGET_HOST value each time.
To confirm you've built the correct architecture, use the file command on the output library:
file build/arm64/output/lib/libfreetype.so
You should see output like ELF 64-bit LSB shared object, ARM aarch64 for arm64, or matching architecture details for other targets.
内容的提问来源于stack exchange,提问作者Vygintas B

