Android项目JNI调用C库遇UnsatisfiedLinkError:.so为64位而非32位
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
Double-check your library's architecture
First, confirm exactly what architecture your.sosupports. On Linux or macOS, run this terminal command:file your-library-name.soIf 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.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.
Restrict your app to supported architectures
To stop Android from trying to load libraries for unsupported architectures, add this to your app's module-levelbuild.gradlefile: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.
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.sofiles, place each in their matchingjniLibsdirectories (e.g.,arm64-v8a/for the 64-bit ARM library).
- For 64-bit ARM: compile for
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

