SoFunction
Updated on 2025-05-11

A complete guide to cross-compilation of Android NDK version iteration and FFmpeg

1. Android NDK version iteration dividing line

  1. Features of NDK r23 and previous versions

• Device Support: Compatible with non-Neon devices

• API level: Support for Jelly Bean (API 16-18)

• Toolchain: Contains GNU binutils toolchain

• Feature support: Support RenderScript

• Debugging tool: Debugging using GDB

  1. Major changes to NDK r24+ version

• Device requirements: Only Neon devices are supported

• Minimum API: Raised to KitKat (API 19)

• Toolchain: Turn completely to the LLVM toolchain, remove GNU binutils

• Debugging tool: Use LLDB instead of GDB by default

• Feature adjustment: Removal of RenderScript support

  1. New features of NDK r25+ version

• Platform support: Supports new features of Android 14

• Language support: Improved C++20 support

• ABI requirements: Implement stricter ABI specifications

• Performance optimization: Optimization for new architectures

2. Key precautions for cross-compilation of FFmpeg

  1. Version matching principle

• Newer FFmpeg versions usually require newer NDK version support

• It is recommended to check the official FFmpeg documentation for the recommended NDK version

  1. API level selection policy

• General recommendation: No less than API 21 (Android 5.0)

• Compatibility considerations: To support older devices, you can drop to API 16

• New Feature Requirements: If you need to use new features, you should choose a higher API

  1. Toolchain selection guide

• NDK r21+: LLVM toolchain must be used

• NDK r16-r20: GCC toolchain can be used

• Pay attention to check whether the tool link path is correct

  1. Output naming specification

• FFmpeg configuration file needs to be modified

• Make sure the output library naming meets Android requirements

• It is recommended to use standard naming specifications:

3. Complete compilation script example

  1. Higher version NDK (r21+) compile script (ARM64 architecture)
#!/bin/bash

# Parameter configuration areaAPI=21
ARCH=arm64
ARCH_PREFIX=aarch64-linux-android
FFMPEG_PATH=$(pwd)
NDK_PATH=/path/to/your/ndk  # Replace with the actual NDK pathTOOLCHAIN=$NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64
PREFIX=$FFMPEG_PATH/android/$ARCH

# Clean up old buildsmake clean
rm -rf $PREFIX

# Configure the compile parameters./configure \
    --prefix=$PREFIX \
    --enable-shared \
    --disable-static \
    --disable-doc \
    --disable-ffmpeg \
    --disable-ffplay \
    --disable-ffprobe \
    --disable-symver \
    --enable-cross-compile \
    --target-os=android \
    --arch=$ARCH \
    --cross-prefix=$TOOLCHAIN/bin/$ARCH_PREFIX- \
    --cc=$TOOLCHAIN/bin/${ARCH_PREFIX}${API}-clang \
    --cxx=$TOOLCHAIN/bin/${ARCH_PREFIX}${API}-clang++ \
    --sysroot=$TOOLCHAIN/sysroot \
    --extra-cflags="-fPIC -O3" \
    --extra-ldflags="-pie" \
    --extra-libs="-lm"

# Execute Compilationmake -j$(nproc)
make install

echo "Compilation is complete! Output directory: $PREFIX"
  • Low version NDK (r16-r20) compile script (ARMv7 architecture)
#!/bin/bash

# Parameter configuration areaAPI=16
ARCH=arm
ARCH_PREFIX=arm-linux-androideabi
FFMPEG_PATH=$(pwd)
NDK_PATH=/path/to/your/ndk  # Replace with the actual NDK pathTOOLCHAIN=$NDK_PATH/toolchains/$ARCH_PREFIX-4.9/prebuilt/linux-x86_64
PLATFORM=$NDK_PATH/platforms/android-$API/arch-$ARCH
PREFIX=$FFMPEG_PATH/android/$ARCH

# Clean up old buildsmake clean
rm -rf $PREFIX

# Configure the compile parameters./configure \
    --prefix=$PREFIX \
    --enable-shared \
    --disable-static \
    --disable-doc \
    --disable-ffmpeg \
    --disable-ffplay \
    --disable-ffprobe \
    --disable-symver \
    --enable-cross-compile \
    --target-os=linux \
    --arch=$ARCH \
    --cross-prefix=$TOOLCHAIN/bin/$ARCH_PREFIX- \
    --sysroot=$PLATFORM \
    --extra-cflags="-march=armv7-a -mfloat-abi=softfp -mfpu=neon -fPIC -O2" \
    --extra-ldflags="-march=armv7-a -Wl,--fix-cortex-a8" \
    --extra-libs="-lgcc -lm"

# Execute Compilationmake -j$(nproc)
make install

echo "Compilation is complete! Output directory: $PREFIX"

4. Detailed explanation of key configurations

  • Target platform settings:

• Higher version:--target-os=android

• Low version:--target-os=linux

  • Architecture selection:

• ARMv7:--arch=arm

• ARM64:--arch=arm64

• x86:--arch=x86

  • Optimization flag:

• -O2/-O3: Optimization level

• -fPIC: Location-independent code

• -pie: Location-independent executable

  • NEON optimization:

• ARMv7 needs to specify NEON support clearly

• ARM64 supports NEON by default

5. Frequently Asked Questions

  • Steps to troubleshoot compilation failure:

• Check if the NDK path is correct

• Verify script execution permissions (chmod +x )

• View to get detailed error information

  • Link error handling:

• Ensure all dependency libraries are available

• examine--extra-libsWhether to include the required library

• Verify toolchain integrity

  • Version compatibility issues:

• Try to match FFmpeg and NDK versions

• Downgrade the FFmpeg version if necessary

  • API level issues:

• Adjust API level according to target device

• High API may not run on lower version devices

6. Best Practice Suggestions

  • Environmental preparation:

• Using Ubuntu 20.04 or later

• Installation required dependencies:sudo apt-get install make yasm clang

  • Version selection policy:

• New project recommended to use the latest stable version of NDK

• Maintenance project keeps NDK version stable

  • Compilation optimization:

• Select the appropriate optimization level according to the target device

• Optimized for specific CPU architectures

  • Product Verification:

• usefileCommand to verify the file schema

• Perform actual testing on the target device

7. Summary

This article analyzes the version iteration history of Android NDK in detail and provides a complete solution for cross-compilation of FFmpeg for different NDK versions. In practical applications, developers are advised to select appropriate NDK versions and compilation parameters according to the actual situation of the target device to obtain the best performance and compatibility.

The above is the detailed guide to cross-compilation of Android NDK version iteration and FFmpeg. For more information about Android NDK version iteration, please pay attention to my other related articles!