如何外部添加.so文件?解决java.lang.UnsatisfiedLinkError找不到库异常
java.lang.UnsatisfiedLinkError by Adding External .so Files Hey, let's work through this UnsatisfiedLinkError you're hitting. The error log clearly states it can't locate liblibAndroidImageFilter.so.so—first, notice that double .so suffix might be a typo in your code or filename, but let's walk through the step-by-step process to properly add external .so files to your Android project.
Step 1: Verify .so File Architecture Compatibility
Your device is running on an arm64-v8a architecture (seen in the log: /data/app/.../lib/arm64). Make sure you have .so files built for all the architectures you want to support (common ones: arm64-v8a, armeabi-v7a, x86, x86_64). Don't just include one architecture unless you explicitly restrict your app to it.
Step 2: Create the jniLibs Directory in Your Project
Android Studio looks for native libraries in the jniLibs directory by default. Here's how to set it up:
- Navigate to
app/src/mainin your project. - If the
jniLibsfolder doesn't exist, create it. - Inside
jniLibs, create subdirectories named after each architecture you support (e.g.,arm64-v8a,armeabi-v7a). - Place your corresponding
.sofiles into the matching architecture subdirectories.
Important: Double-check the filename! The error is looking for
liblibAndroidImageFilter.so.so—ensure your file matches this name, or fix the code that loads the library (more on that in Step 4).
Step 3: Configure Your Module's build.gradle
Make sure your Gradle build file recognizes the jniLibs directory and properly packages the native libraries. Add this to your module-level build.gradle (under the android block):
android { // ... other configurations sourceSets { main { jniLibs.srcDirs = ['src/main/jniLibs'] // If you placed .so files in the "libs" folder instead, use: // jniLibs.srcDirs = ['libs'] } } // Optional: Restrict to specific architectures to reduce APK size defaultConfig { // ... other configurations ndk { abiFilters 'arm64-v8a', 'armeabi-v7a' } } }
Step 4: Fix the Library Loading Code
Looking at the stack trace, the library is loaded in LightFilter.java's static initializer with System.loadLibrary(). Remember:
- The argument to
System.loadLibrary()should be the name of the library without thelibprefix and.sosuffix. - The error shows it's searching for
liblibAndroidImageFilter.so.so—this means your code probably has something likeSystem.loadLibrary("libAndroidImageFilter.so")(extra.sosuffix). Correct it to:
Match this to your actual// If your .so file is named liblibAndroidImageFilter.so System.loadLibrary("libAndroidImageFilter"); // Or if the file should be libAndroidImageFilter.so (more likely, fixing the typo) System.loadLibrary("AndroidImageFilter");.sofilename to avoid the double.soissue.
Step 5: Clean and Rebuild Your Project
Cached build files can cause unexpected issues. Do the following:
- Go to
Build > Clean Projectin Android Studio. - Then go to
Build > Rebuild Project. - Run your app again.
Step 6: Verify the APK Includes the .so File
To confirm the library is properly packaged:
- Locate your generated APK (usually in
app/build/outputs/apk/debug/). - Unzip the APK and check the
lib/[architecture]/directory (e.g.,lib/arm64-v8a/). - Ensure your
.sofile is present there.
Common Pitfalls to Watch For
- File Permissions: Make sure your
.sofiles have read permissions (most files copied from valid sources will already have this, but it's worth checking). - SDK Compatibility: Ensure the
.sofiles were compiled against an SDK version that's compatible with your app'sminSdkVersion. - Android App Bundle: If you're using App Bundles, test with a full APK first—Bundles deliver architecture-specific libraries on demand, which can cause issues during local testing.
内容的提问来源于stack exchange,提问作者ishan gengadiya

