網站首頁 編程語言 正文
前言
一共有4個參數,分來來講。
1:dexFile(String類型)
2:optimizedDirectory(File類型)
3:librarySearchPath(String類型)
4:parent(ClassLoader類型)
一.dexPath(String)
官方的注釋:
* @param dexPath the list of jar/apk files containing classes and
* resources, delimited by {@code File.pathSeparator}, which
* defaults to {@code ":"} on Android.
包含類和類的jar/apk文件列表資源,由{@code File.pathSeparator}分隔,其中Android上的默認值為{@code”:“}。
也就是說這里其實是可以傳入一個集合的。
比如如下的參數都是可以被接受的:
sdcard/test/aa.apk
sdcard/test/aa.apk:sdcard/test/bb.apk:sdcard/test/cc.apk
sdcard/test/aa.apk:sdcard/test/dd.jar
其中分隔符:,保險起見,可以使用File.pathSeparator替代。
示例代碼如下:
private void loadDex(List<File> apkList) {
StringBuilder builder = new StringBuilder();
for (File f : apkList) {
builder.append(f.getAbsolutePath());
builder.append(File.separatorChar);
}
DexClassLoader dexClassLoader = new DexClassLoader(builder.toString(), null, null, getClass().getClassLoader());
}
二.optimizedDirectory
官方的注釋:
this parameter is deprecated and has no effect since API level 26.
解壓的路徑,這里傳入路徑的最主要目的就是為了生成odex文件夾,方便后續存儲odex文件。
如注釋中所寫,這個參數26開始已經失效了。所以這里就不擴展去講了。
三.librarySearchPath
官方的注釋:
?* @param librarySearchPath the list of directories containing native
包含native目錄的目錄列表,這里要注意的,傳入的一定是so的上一級目錄才可以。如果是更上一層的目錄是不行的。前言中的問題就出在這,傳入了一個更上一層的目錄地址。
排查流程:
最終使用librarySearchPath的地方是在DexPathList的splitPaths方法中。生成File加入List中:
@UnsupportedAppUsage
private static List<File> splitPaths(String searchPath, boolean directoriesOnly) {
List<File> result = new ArrayList<>();
if (searchPath != null) {
for (String path : searchPath.split(File.pathSeparator)) {
...
result.add(new File(path));
}
}
return result;
}
而使用的時候,是使用了findLibrary的方法,for循環便利上面集合中的所有path,看是否存在。
public String findLibrary(String libraryName) {
String fileName = System.mapLibraryName(libraryName);
for (NativeLibraryElement element : nativeLibraryPathElements) {
String path = element.findNativeLibrary(fileName);
if (path != null) {
return path;
}
}
return null;
}
而尋找的最終會調用到NativeLibraryElement的findNativeLibrary方法:
public String findNativeLibrary(String name) {
maybeInit();
if (zipDir == null) {
String entryPath = new File(path, name).getPath();
if (IoUtils.canOpenReadOnly(entryPath)) {
return entryPath;
}
} else if (urlHandler != null) {
// Having a urlHandler means the element has a zip file.
// In this case Android supports loading the library iff
// it is stored in the zip uncompressed.
String entryName = zipDir + '/' + name;
if (urlHandler.isEntryStored(entryName)) {
return path.getPath() + zipSeparator + entryName;
}
}
return null;
}
這段代碼也就是問題的核心了,直接使用了new File(path,name)的方式,而不是循環查找,所以只有上一層才可以。
四.parent
官方的注釋:
@param parent the parent class loader
這個比較簡單,就是上一層的classLoader。
五.總結
無論是dexPath還是librarySearchPath,都是支持多路徑傳入的。路徑之間使用File.pathSeparator進行分隔。dexPath中必須是APK或者Jar包的路徑,而librarySearchPath中必須是so文件的上一層級文件夾才可以。
原文鏈接:https://blog.csdn.net/rzleilei/article/details/126228474
相關推薦
- 2022-06-19 C++深入探索類真正的形態之struct與class_C 語言
- 2023-01-17 pytest用yaml文件編寫測試用例流程詳解_python
- 2022-05-05 基于Redis分布式BitMap的應用分析_Redis
- 2022-05-08 Python與C語言分別完成排序流程_python
- 2022-08-30 MongoD管理數據庫的方法介紹_MongoDB
- 2022-09-26 網絡瀏覽器中運行Python腳本PyScript剖析_python
- 2022-07-07 C#使用CallContext緩存線程數據_C#教程
- 2022-12-24 c++重載運算符時返回值為類的對象或者返回對象的引用問題_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支