Unity导入含JNI文件的Android库(.aar)时遇UnsatisfiedLinkError错误
Hey there, I’ve dealt with this exact UnsatisfiedLinkError a few times when integrating Android AARs with native libraries into Unity—let’s break down what’s going on and fix it step by step:
The error means Unity’s Android build can’t locate your libnative-lib.so file. Usually this is because the native library isn’t being placed in the right location during the build, there’s a mismatch in supported CPU architectures, or the library name isn’t exactly what the system expects.
Double-check your AAR’s native library structure
Grab a zip tool and unpack your AAR. Look for ajnifolder inside—there should be subfolders for each CPU architecture your library supports (likearmeabi-v7a,arm64-v8a,x86,x86_64). Each of these folders must contain exactlylibnative-lib.so. If any architecture you’re targeting is missing, that’s a problem.Align Unity’s target architectures with your library
Open Unity’s Player Settings (Edit > Project Settings > Player), go to the Android tab, and expand Other Settings > Configuration. Make sure the Target Architectures only includes the ones yourlibnative-lib.sosupports. For example, if your library only works on 64-bit ARM devices, uncheckx86andarmeabi-v7ato avoid Unity trying to load a missing library for those architectures.Manually copy native libraries to Unity’s Plugins folder
Sometimes Unity doesn’t auto-extract native libraries from AARs. Here’s how to fix that:- Unpack your AAR and pull out all the architecture folders from the
jnidirectory. - In your Unity project, create
Assets/Plugins/Android/libsif it doesn’t exist. - Paste the architecture folders (e.g.,
arm64-v8a) into thislibsfolder. - Select each folder in Unity’s Project window, go to the Inspector, set Platform to Android, pick the matching CPU architecture, and check Include in Build.
- Unpack your AAR and pull out all the architecture folders from the
Confirm the library name is exact (case-sensitive!)
Android treats native library names as case-sensitive. The error says it’s looking forlibnative-lib.so—make sure your AAR’s library has exactly that name (no typos, no capitalization differences likeLibNative-Lib.so).Check for conflicting libraries
If your Unity project has other AARs or native libraries with the samelibnative-lib.soname, they might be conflicting. Search your entire project for this file and remove duplicates or ensure all versions are identical.Force Unity to recognize the AAR’s native libraries
Create a.gradlefile with the same name as your AAR inAssets/Plugins/Android(e.g., if your AAR ismylibrary.aar, makemylibrary.gradle). Add this code to it:dependencies { implementation fileTree(dir: 'libs', include: ['*.aar']) } android { sourceSets { main { jniLibs.srcDirs = ['libs'] } } }This tells Unity’s build system explicitly where to find the native libraries in the AAR.
内容的提问来源于stack exchange,提问作者Saurabh Pandey

