解决apk中有.so,实际运行时找不到的问题
排查方向:
- ①、.so安装位置是否实际存在文件(
context.getApplicationInfo().nativeLibraryDir
) - ②、当前ARM架构适配配置或者匹配(
armeabi-v7a
,arm64-v8a
,x86_64
,...
) - ③、加载方式是否正确[
System.loadLibrary("so_name_but_no_'lib'_prefix")
/System.load("so_absolute_path")
] - ④、Android版本(
Android 31+
),AGP版本(AGP 4.2.0+
)参考下方配置↓↓↓
- ⑤、
android:extractNativeLibs=true
是否配置(或者DSL选项useLegacyPackaging=true
) - ⑥、库文件没有访问权限
- Android .so存储位置
可以通过如下方式获取so文件的加载位置
String so_path = context.getApplicationInfo().nativeLibraryDir; //查看加载 .so 的位置
举个栗子
系统应用:/system/lib/xxx
[/vendor/lib
(三方OEM厂商)] 或者 system/app/xxx/lib
三方应用: data/app/[package-name]/lib
注意:package-name
可能会是一串随机数字,可以通过如下adb
命令行根据包名查看app的安装位置:
adb shell
pm list package -f |grep com.axxxx
您可以通过如下代码查找.so
文件所在的目录,参考自:
https://blog.csdn.net/wangbaochu/article/details/47805921
/**
* The function use to find so path for compatible android system
* Android OS >= 2.3
*/
public static String findLibrary1(Context context, String libName) {
String result = null;
ClassLoader classLoader = (context.getClassLoader());
if (classLoader != null) {
try {
Method findLibraryMethod = classLoader.getClass().getMethod("findLibrary", new Class<?>[] {
String.class });
if (findLibraryMethod != null) {
Object objPath = findLibraryMethod.invoke