網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
前言
一共有4個(gè)參數(shù),分來(lái)來(lái)講。
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上的默認(rèn)值為{@code”:“}。
也就是說(shuō)這里其實(shí)是可以傳入一個(gè)集合的。
比如如下的參數(shù)都是可以被接受的:
sdcard/test/aa.apk
sdcard/test/aa.apk:sdcard/test/bb.apk:sdcard/test/cc.apk
sdcard/test/aa.apk:sdcard/test/dd.jar
其中分隔符:,保險(xiǎn)起見(jiàn),可以使用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文件夾,方便后續(xù)存儲(chǔ)odex文件。
如注釋中所寫(xiě),這個(gè)參數(shù)26開(kāi)始已經(jīng)失效了。所以這里就不擴(kuò)展去講了。
三.librarySearchPath
官方的注釋:
?* @param librarySearchPath the list of directories containing native
包含native目錄的目錄列表,這里要注意的,傳入的一定是so的上一級(jí)目錄才可以。如果是更上一層的目錄是不行的。前言中的問(wèn)題就出在這,傳入了一個(gè)更上一層的目錄地址。
排查流程:
最終使用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;
}
而使用的時(shí)候,是使用了findLibrary的方法,for循環(huán)便利上面集合中的所有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;
}
而尋找的最終會(huì)調(diào)用到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;
}
這段代碼也就是問(wèn)題的核心了,直接使用了new File(path,name)的方式,而不是循環(huán)查找,所以只有上一層才可以。
四.parent
官方的注釋:
@param parent the parent class loader
這個(gè)比較簡(jiǎn)單,就是上一層的classLoader。
五.總結(jié)
無(wú)論是dexPath還是librarySearchPath,都是支持多路徑傳入的。路徑之間使用File.pathSeparator進(jìn)行分隔。dexPath中必須是APK或者Jar包的路徑,而librarySearchPath中必須是so文件的上一層級(jí)文件夾才可以。
原文鏈接:https://blog.csdn.net/rzleilei/article/details/126228474
相關(guān)推薦
- 2022-06-29 Android實(shí)用小技巧之利用Lifecycle寫(xiě)出更好維護(hù)的代碼_Android
- 2022-07-18 anaconda常用命令的整理
- 2022-05-31 Python學(xué)習(xí)之日志模塊詳解_python
- 2022-08-28 ubuntu安裝samba文件共享
- 2022-05-19 基于角色的權(quán)限控制模型RBAC圖文教程_相關(guān)技巧
- 2024-03-10 @Controller、@Service和@Repository注解詳解
- 2022-08-15 apollo配置中心的client端分析
- 2022-05-15 Python使用chrome配置selenium操作詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支