Android native library (.so) - unsatisfied link error

别来无恙 提交于 2021-02-10 14:33:56

问题


I have recently created and published an Android App bundle with compiled native libraries for all processors architectures. On most of devices everything is running ok. But on some of devices I can see this error in Crashlytics console Fatal Exception: java.lang.UnsatisfiedLinkError dalvik.system.PathClassLoader[DexPathList[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.someapp-wAu5DoLmLvM_RVnbU1qsCg==/base.apk"],nativeLibraryDirectories=[/data/app/com.someapp-wAu5DoLmLvM_RVnbU1qsCg==/lib/arm64, /system/lib64]]] couldn't find "somemylib.so"

I am wondering - why this may happen. I have libs for x64 processors. This is happening only on some devices - but this crash is the most often in Crashlytics. All my libs are stored inside jniLibs directory THese are some pieces from my build.gradle

bundle {
    density {
        // Different APKs are generated for devices with different screen densities; true by default.
        enableSplit false
    }
    abi {
        // Different APKs are generated for devices with different CPU architectures; true by default.
        enableSplit true
    }
    language {
        // This is disabled so that the App Bundle does NOT split the APK for each language.
        // We're gonna use the same APK for all languages.
        enableSplit false
    }
}
splits {
    abi {
        enable true
        reset()
        include 'armeabi', 'mips', 'x86', 'armeabi-v7a', 'arm64-v8a', 
 'mips64', 'x86_64'
    }
}
sourceSets {

    main {
        jniLibs.srcDirs = ['jniLibs']
    }
}

I have a service class inside my app which is trying to load native library when someone is starting it. My be sometimes this "start" call to service is executing before the OS can detect the native libs in my app? But I don't think that this is so. Can you help me please


回答1:


Your problem is not with the library code, but with the .so files themselves. Android devices have a primary ABI, the instruction set supported by the processor. For example, some devices with 64-bit processors use the ABI arm64-v8a.

However, a lot of libraries and apps don't support 64-bit processors yet. So usually, the newer processors also support a secondary ABI, which is a more common one. arm64-v8a processors usually support armeabi-v7a too, for example.

When the app starts, it starts looking for the native libraries in the directories. First it checks in the primary ABI directory, arm64-v8a in this example. If the .so files are not there, it checks in the secondary ABI directory (armeabi-v7a). If still not found, you will get the error you described.

In your case, you have the files, as you said, so that's not the problem.

One big problem with this selection is, that it always sticks to the primary ABI, if the directory exists. This can lead to crashes if not all libraries supply all .so files for all ABIs. This is described here.

Here's an example: you include 2 libraries in your app: 1. VideoPlayerLibrary, which has .so files for all ABIs 2. GifLibrary, which has .so files only for armeabi and armeabi-v7a.

Now if you start the app on an arm64-v8a device, it will stick to it's primary ABI, because it has found files there (the .so files of VideoPlayerLibrary). But at the moment you want to play gifs, it will try to load the .so files from the arm64-v8a directory, but it will not find them there and it will crash as in your post with a link error.

So what can you do?

You have 2 options:

  1. If it is your own library which has the missing .so files, compile it for all ABIs. If it is not your own, check if there's a newer version which has fixed this issue.
  2. Exclude the incomplete ABIs from the app. This way the newer processors will use the secondary ABI for running the app. This will result in worse performance of course, but it will not crash. From my experience, the most important ABI is armeabi-v7a, this should be supported by probably all Android devices.


来源:https://stackoverflow.com/questions/55136767/android-native-library-so-unsatisfied-link-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!