为 app_process 配置正确的 ClassLoader namespace
在 app_process 中使用 System.loadLibrary 加载 .so 库时,若默认路径找不到库文件,可通过以下代码动态添加 native library 的搜索路径:
1 | |
但这样存在一个问题,System.loadLibrary(A) 之后,如果 A 中调用 dlopen(B) 尝试加载另一个打包进来的 so,依然会报找不到的问题,应该是 addNativePath 不能影响到 classloader namespace 导致的,需要寻找更 Trivial 的方法
通过 这篇文章 了解到,应用启动时,通过 ApplicationLoaders 创建默认的 CLNS,同时传入动态库等信息,如果我们能模仿这个行为,应该就能获得一个正常的 CLNS。
先看看正常的创建流程,这里从 LoadedApk 出发:
1 | |
ApplicationLoaders 中调用 ClassLoaderFactory#createClassLoader:
1 | |
最终走到 ClassLoaderFactory#createClassloaderNamespace:
1 | |
列表!
| 参数 | 类型 | 取值 |
|---|---|---|
classLoader |
ClassLoader |
字面意思 |
targetSdkVersion |
int |
字面意思 |
librarySearchPath |
String |
System.getProperty("java.library.path");多个则 joinTo(":") |
libraryPermittedPath |
String |
null |
isNamespacesShared |
boolean |
isBundled |
dexPath |
String |
Apk zip 路径 |
sonameList |
String |
mApplicationInfo.sharedLibraryInfos.map { it.name }.joinTo(":") |
1 | |
上面为 Android R 的代码,之后的版本多一个参数,完整版参见:AProc
为 app_process 配置正确的 ClassLoader namespace
https://neo.mufanc.xyz/posts/41575/