android NDK开发过程中,APP调用so库出现如下问题
主要原因是google在N上对.so库的加载进行了限制,限制了so库指从部分指定的路径进行加载,不在这个路径的so提示
java.lang.UnsatisfiedLinkError: dlopen failed: library “xxx.so” not found 或
java.lang.UnsatisfiedLinkError: dlopen failed: library “/vendor/lib64/xxx.so” needed or dlopened by “/system/lib64/libnativeloader.so” is not accessible for the namespace “classloader-namespace” 或 其他异常错误提示。
N上对so库加载的搜索路径方式为ld_library_path, runtime path, permit path,不在这个搜索路径下则加载失败。从代码层面看,主要是类加载器ClassLoader的相关处理,
//code1: (loadedApk.java getClassLoader()) check sdk version // DO NOT SHIP: this is a workaround for apps loading native libraries // provided by 3rd party apps using absolute path instead of corresponding // classloader; see http://b/26954419 for example. if (mApplicationInfo.targetSdkVersion <= 23) { libraryPermittedPath += File.pathSeparator + “/data/app”; }//Code2: (loadedApk.java getClassLoader()) N add a new PermittedPath String libraryPermittedPath = mDataDir;//Code3: (native_loader.cpp) use the new namespace rule with search path: ld_library_path, //runtime path, permit path.
将自己的so加入到允许路径的白名单里面,具体操作为,如果不改代码实现,则导出设备的/vendor/etc/public.libraries.txt 或/etc/public.libraries.txt文件,将so名字添加进去,在push到设备,重启即可。
来源地址:https://blog.csdn.net/BersonKing/article/details/130124071