Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Flutter and app store releases

The arm64-v8a build will not link a prebuilt Android .so file

Your error

ld.lld: error: ../../../../libs/armeabi-v7a/libfoo.so is incompatible with aarch64linux1 more line it can show

Fix it fast

Most common cause, 1 of 3

CMake imports the library from a fixed ABI folder

Use the ANDROID_ABI variable in the path, so each ABI build picks the file from its own folder, and keep one copy of the library per ABI in those folders.

Fix
add_library(foo SHARED IMPORTED)
set_target_properties(foo PROPERTIES IMPORTED_LOCATION
    ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libfoo.so)
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

ld.lld: error: libfoo.so is incompatible with aarch64linux means the linker, building for arm64-v8a, was handed a library compiled for another architecture, such as 32-bit ARM or x86-64. Check the file with llvm-readelf -h; it must say Machine: AArch64. Point CMake at the ${ANDROID_ABI} folder, or rebuild the library with the NDK toolchain for arm64-v8a. Applies to NDK r30.

  • Applies to Android NDK r30 (30.0.16248370), CMake builds through the Android Gradle plugin, arm64-v8a target
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Read the ELF header of the exact file named in the error with the NDK's own llvm-readelf. Machine: ARM or X86-64 in a file used for arm64-v8a is the fault; the folder it came from and how it was built separate the three causes.

Run this first
# TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64 (or darwin-x86_64)
$TOOLCHAIN/bin/llvm-readelf -h path/to/libfoo.so | grep -E "Class|Machine"

Then find the line you see in this list and open its fix:

  1. 1CMake imports the library from a fixed ABI folderThe path in the error contains armeabi-v7a, x86 or x86_64 while Gradle is building arm64-v8a
  2. 2The vendor only ships a 32-bit or x86 build of the libraryEven the copy inside the arm64-v8a folder reports Class: ELF32 and Machine: ARM
  3. 3The library was compiled with the host compiler, not the NDKllvm-readelf shows Machine: Advanced Micro Devices X86-64, and the library was built with the system gcc or clang on a Linux PC

What does the error look like?

  • ld.lld: error: ../../../../libs/armeabi-v7a/libfoo.so is incompatible with aarch64linux
  • ld.lld: error: libs/x86_64/libfoo.so is incompatible with aarch64linux

Cause 1 of 3

CMake imports the library from a fixed ABI folder

You seeThe path in the error contains armeabi-v7a, x86 or x86_64 while Gradle is building arm64-v8a

Check

A prebuilt library added with add_library(... IMPORTED) and a hard-coded IMPORTED_LOCATION is linked into every ABI that Gradle builds. When the path names the 32-bit folder, the 32-bit file reaches the 64-bit link and lld rejects it as incompatible with aarch64linux.

Check
grep -n "IMPORTED_LOCATION" app/src/main/cpp/CMakeLists.txt

Fix

Use the ANDROID_ABI variable in the path, so each ABI build picks the file from its own folder, and keep one copy of the library per ABI in those folders.

Fix
add_library(foo SHARED IMPORTED)
set_target_properties(foo PROPERTIES IMPORTED_LOCATION
    ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libfoo.so)

Verify

The arm64-v8a build links, and llvm-readelf on libs/arm64-v8a/libfoo.so reports ELF64 and AArch64.

Verify
./gradlew assembleDebug

Confirmed by developer.android.com

Did this fix it?

Cause 2 of 3

The vendor only ships a 32-bit or x86 build of the library

You seeEven the copy inside the arm64-v8a folder reports Class: ELF32 and Machine: ARM

Check

Renaming or moving a file does not change what it was compiled for. If the SDK provider never built an AArch64 version, there is no file that can link into arm64-v8a, and Google Play requires the 64-bit equivalent for every 32-bit ABI that an app ships.

Check
$TOOLCHAIN/bin/llvm-readelf -h libs/arm64-v8a/libfoo.so | grep Machine

Fix

Get an arm64-v8a build from the vendor, or build the library from source for that ABI. Dropping arm64-v8a from abiFilters only hides the problem, because Play requires a 64-bit copy for each 32-bit ABI the app ships. Once the 64-bit file is in place, abiFilters can limit the build to the ABIs you really have.

Fix
// libs/arm64-v8a/libfoo.so   <- the vendor's 64-bit build
// libs/armeabi-v7a/libfoo.so
// app/build.gradle
android {
    defaultConfig {
        ndk { abiFilters 'arm64-v8a', 'armeabi-v7a' }
    }
}

Verify

Each libs/<abi>/libfoo.so reports the machine that matches its folder, and the arm64-v8a build links.

Verify
for f in libs/*/libfoo.so; do echo $f; $TOOLCHAIN/bin/llvm-readelf -h $f | grep Machine; done

Confirmed by developer.android.com

Did this fix it?

Cause 3 of 3

The library was compiled with the host compiler, not the NDK

You seellvm-readelf shows Machine: Advanced Micro Devices X86-64, and the library was built with the system gcc or clang on a Linux PC

Check

A .so made by the desktop toolchain targets the build machine, so its machine type does not match the Android target. CMake cross-compiles for Android only when it gets the NDK's android.toolchain.cmake together with ANDROID_ABI.

Check
$TOOLCHAIN/bin/llvm-readelf -h libfoo.so | grep Machine

Fix

Rebuild with the NDK toolchain file and ANDROID_ABI set to arm64-v8a, once per ABI in separate build folders. Autoconf projects take the NDK clang through CC with a target triple instead.

Fix
cmake -B build-arm64 \
  -DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake \
  -DANDROID_ABI=arm64-v8a \
  -DANDROID_PLATFORM=android-24
cmake --build build-arm64

Verify

The new libfoo.so reports ELF64 and AArch64, and the app build links it.

Verify
$TOOLCHAIN/bin/llvm-readelf -h build-arm64/libfoo.so | grep -E "Class|Machine"

Confirmed by developer.android.com

Did this fix it?

How do I stop it coming back?

  • Keep prebuilt libraries in libs/<abi>/ folders and reference them only through ${ANDROID_ABI}.
  • Add a CI step that runs llvm-readelf -h on every bundled .so and fails if the Machine does not match its folder.
  • Ask SDK vendors for an arm64-v8a build before you integrate them, not when the release build breaks.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: QistLock, Nuclear Home Video.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.