当前位置:网站首页>Correct use of BaseDexClassLoader
Correct use of BaseDexClassLoader
2022-08-09 04:18:00 【lost summer】
Foreword:
When I solved a plug-in problem today, I found that SO was not loaded normally, which was very strange. Finally, I found out that it was caused by the wrong parameter input.This leads to the title of this article. How should the four parameters in BaseDexClassLoader be passed in, and what are passed in?
There are a total of 4 parameters, in terms of points.
1: dexFile (String type)2: optimizedDirectory (File type)3: librarySearchPath (String type)4: parent (ClassLoader type)
One.dexPath(String)
Official note:
* @param dexPath the list of jar/apk files containing classes and* resources, delimited by {@code File.pathSeparator}, which* defaults to {@code ":"} on Android.
List of jar/apk files containing classes and resources, separated by {@code File.pathSeparator}, where the default on Android is {@code":"}.
In other words, a collection can be passed in here.
For example, the following parameters are acceptable:
sdcard/test/aa.apk
sdcard/test/aa.apk:sdcard/test/bb.apk:sdcard/test/cc.apk
sdcard/test/aa.apk:sdcard/test/dd.jar
Where the separator:, to be on the safe side, you can use File.pathSeparator instead.
Sample code below:
private void loadDex(List 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());}
two.optimizedDirectory
Official note:
this parameter is deprecated and has no effect since API level 26.
The path of decompression, the main purpose of passing the path here is to generate the odex folder, which is convenient for subsequent storage of odex files.
As written in the comments, this parameter 26 has initially been disabled.Therefore, it will not be expanded here.
Three.librarySearchPath
Official note:
* @param librarySearchPath the list of directories containing native
Contains the directory list of the native directory. It should be noted here that the incoming directory must be the upper-level directory of so.If it is a higher-level directory, it will not work.The problem in the preface is here, a higher directory address is passed in.
Finding process:
The final place to use librarySearchPath is in the splitPaths method of DexPathList.Generate File and add it to List:
@UnsupportedAppUsageprivate static List splitPaths(String searchPath, boolean directoriesOnly) {List result = new ArrayList<>();if (searchPath != null) {for (String path : searchPath.split(File.pathSeparator)) {...result.add(new File(path));}}return result;}
When using it, the findLibrary method is used, and the for loop facilitates all the paths in the above collection to see if they exist.
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;}
The search will eventually call the findNativeLibrary method of NativeLibraryElement:
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;}
This code is the core of the problem. It directly uses the method of new File(path,name) instead of looping search, so only the upper layer can be used.
four.parent
Official note:
@param parent the parent class loader
This is relatively simple, it is the classLoader of the previous layer.
V. Summary
Both dexPath and librarySearchPath support multi-path incoming.Use File.pathSeparator to separate paths.The dexPath must be the path of the APK or Jar package, and the librarySearchPath must be the upper-level folder of the so file.
边栏推荐
猜你喜欢
随机推荐
Oracle 的开窗函数使用详解
npm package.json
MySQL:redo log日志——笔记自用
NanoDet代码逐行精读与修改(三)辅助训练模块AGM
了解CV和RoboMaster视觉组(五)local-distribution汇聚方法
了解CV和RoboMaster视觉组(五)目标跟踪:概述与光流法
A few words about Microsoft's 2022/2023 autumn recruits
【数学】点积与叉积
了解CV和RoboMaster视觉组(五)滤波器、观测器和预测方法:自适应滤波器的应用
MKNetworkKit replacing domain name wrong solution
LeetCode-从链表中删去总和值为零的连续结点
配置网络接口的“IP“命令
“error“: { “root_cause“: [{ “type“: “circuit_breaking_exception“, “reason“: “[parent] D【已解决】
XJTUSE专业课与实验指南
容易混淆的指针知识点
AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipeline‘
2022年低压电工练习题及模拟考试
阿里云天池大赛赛题(机器学习)——O2O优惠券预测(完整代码)
FFmpeg编译支持x264/openH264/dash
07.1 类的的补充