本篇内容介绍了“Android之BaseDexClassLoader如何使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
一共有4个参数,分来来讲。
1:dexFile(String类型)
2:optimizedDirectory(File类型)
3:librarySearchPath(String类型)
4:parent(ClassLoader类型)
官方的注释:
* @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());
}
官方的注释:
this parameter is deprecated and has no effect since API level 26.
解压的路径,这里传入路径的最主要目的就是为了生成odex文件夹,方便后续存储odex文件。
如注释中所写,这个参数26开始已经失效了。所以这里就不扩展去讲了。
官方的注释:
* @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)的方式,而不是循环查找,所以只有上一层才可以。
官方的注释:
@param parent the parent class loader
这个比较简单,就是上一层的classLoader。
“Android之BaseDexClassLoader如何使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云提供多种品牌、不同类型SSL证书签发服务,包含:域名型、企业型、企业型专业版、增强型以及增强型专业版,单域名SSL证书300元/年起。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。