You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android项目JNI调用C库遇UnsatisfiedLinkError:.so为64位而非32位

Fixing java.lang.UnsatisfiedLinkError: dlopen failed: .so library is 64-bit instead of 32-bit

Hey there, let's break down what's going on here and fix that frustrating error!

The Core Issue

Your C library .so file is compiled for x86-64 (64-bit x86) architecture, but you're dropping it into directories meant for 32-bit architectures—like armeabi-v7a (32-bit ARM) or x86 (32-bit x86). Android gets confused when it tries to load a 64-bit library from a 32-bit architecture directory, hence the mismatch error.

Step-by-Step Fixes

  1. Double-check your library's architecture
    First, confirm exactly what architecture your .so supports. On Linux or macOS, run this terminal command:

    file your-library-name.so
    

    If the output mentions x86-64, that confirms it's a 64-bit x86 library—it won't work on 32-bit x86 or any ARM devices unless you recompile it.

  2. Move the .so to the correct directory
    If your library only has an x86-64 version, relocate it to:

    src/main/jniLibs/x86_64/
    

    Remove it from all other architecture directories (arm64-v8a, armeabi-v7a, x86) — mixing mismatched architectures here will keep triggering errors.

  3. Restrict your app to supported architectures
    To stop Android from trying to load libraries for unsupported architectures, add this to your app's module-level build.gradle file:

    android {
        defaultConfig {
            ndk {
                // Only include the architecture your library supports
                abiFilters "x86_64"
            }
        }
    }
    

    This tells the build system to only package the x86_64 version of your app, avoiding pointless attempts to load libraries for other architectures.

  4. Add ARM support (if you need it)
    If you want your app to work on standard ARM-based Android phones, you'll need to recompile your original C library for ARM architectures:

    • For 64-bit ARM: compile for arm64-v8a
    • For 32-bit ARM: compile for armeabi-v7a
      Once you have those ARM-specific .so files, place each in their matching jniLibs directories (e.g., arm64-v8a/ for the 64-bit ARM library).
  5. Verify your test device/emulator's architecture
    Make sure the device or emulator you're testing on matches your library's architecture. For example, if you're using an x86_64 library, test on an x86_64 emulator or a rare x86_64 Android device. 32-bit devices will ignore the x86_64 directory and throw a "library not found" error unless you have a 32-bit version of the library.


内容的提问来源于stack exchange,提问作者Daniel Rust

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:55:31