How to package native library (*.so) in Android Studio


Recently I was working on some projects where the native library (*.so) is used. It builds in Android Studio without any problem, but when I run it on the Android devices, it crashes with the following error message:

Can’t load xxx library: java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file “/data/app/some.package.name/abc.apk“],nativeLibraryDirectories=[/vendor/lib, /system/lib]]] couldn’t find “abcxyz.so

After millions of trial and error, here comes the solution:

    1. Create a folder called “lib” (must be exactly this name)
    2. Create subfolders in lib with desired architecture name such as “armeabi-v7a”, “armeabi” etc.
    3. Put all your *.so file in the just created folder
    4. Now your folder structure should be like this:
      lib\armeabi-v7a

      lib\armeabi
    5. Zip the lib folder into a file, e.g. abc.zip;
    6. Rename this abc.zip –> abc.jar
    7. Copy this abc.jar to your project subfolder, anywhere should do, but I would typically put it in the app\libs\abc,jar
    8. In the build.gradle, add:

dependencies {
compile files(‘libs/abc.jar’)
    …
}

Rebuild, that is all! Happy Coding!

 

 

4 thoughts on “How to package native library (*.so) in Android Studio

Leave a comment