Location of Chilkat JNI Shared Libraries in an Android APK Directory Tree
On Android, the JNI shared library (".so" file) should be located in the correct directory within the APK under the "lib/" folder, organized by architecture. Android expects these shared libraries to match the architecture of the device (e.g., "arm64-v8a", "armeabi-v7a", "x86", etc.).
Correct Location for JNI Shared Libraries
When you build your Android application, the ".so" files should be included in the APK with this structure:
lib/ armeabi-v7a/ libchilkat.so arm64-v8a/ libchilkat.so x86/ libchilkat.so x86_64/ libchilkat.so
- Each folder corresponds to a specific ABI (Application Binary Interface) for the device's architecture.
- Android dynamically loads the ".so" file matching the architecture of the device.
How to Ensure Proper Placement
- Include ".so" Files in Your Project:
- Place the ".so" files in the appropriate directory within your project:
app/src/main/jniLibs/ armeabi-v7a/ libchilkat.so arm64-v8a/ libchilkat.so x86/ libchilkat.so x86_64/ libchilkat.so
- Place the ".so" files in the appropriate directory within your project:
- Build the APK:
- When you build your project, Gradle will automatically package the ".so" files into the correct locations in the APK.
- Load the Library in Code:
- Use "System.loadLibrary()" to load the library in your Java code:
System.loadLibrary("your_library"); // without the "lib" prefix or ".so" extension
- Use "System.loadLibrary()" to load the library in your Java code:
- Ensure Compatibility:
- Verify that the ".so" file is compiled for the same architecture as the device (e.g., "arm64-v8a" for 64-bit ARM devices).
- Verify that the ".so" file is compiled for the same architecture as the device (e.g., "arm64-v8a" for 64-bit ARM devices).
Troubleshooting UnsatisfiedLinkError
- Verify the ".so" File Location:
- Extract the APK (".apk" is a ZIP file) and check that the ".so" file is in the "lib/<architecture>/" folder.
- Check Architecture Compatibility:
- Ensure the library matches the device's ABI (e.g., "arm64-v8a" for 64-bit ARM devices).
- If your library only supports certain architectures, make sure your app explicitly targets those in the "build.gradle" file:
ndk { abiFilters "armeabi-v7a", "arm64-v8a" }
- Check Library Name:
- Ensure the name passed to "System.loadLibrary()" matches the library's name without the "lib" prefix or ".so" extension.
- Logcat for Debugging:
- Use "adb logcat" to check for additional errors or information about why the library failed to load.
By ensuring the ".so" files are correctly placed and compatible with the device's architecture, the "UnsatisfiedLinkError" should be resolved.