当前位置:网站首页>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 nativeContains 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 loaderThis 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.
边栏推荐
- AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipeline‘
- 2022R1快开门式压力容器操作考试模拟100题及在线模拟考试
- 新一代CMDB构建方法,是能够给企业带来收益的
- 【平衡二叉搜索树】细撕AVL树的插入操作
- Moonriver与Shiden的XCM集成现已上线
- 遗传力缺失的案例
- gopacket使用示例
- 服务端修改Cookie——跨域cookie发送机——通信加密——异或加密
- 了解CV和RoboMaster视觉组(五)参数自适应与稳健特征
- wift3.0 set the navigation bar, title, font, item color and font size
猜你喜欢
随机推荐
2022年安全员-B证考试练习题及在线模拟考试
简单的数学公式计算
模型包装,答辩吹牛方法论!
数据库指标是怎么个意思
分布式数据库怎样才能“叫好又卖座”
32 基本统计知识——假设检验
Poly1CrossEntropyLoss的pytorch实现
了解CV和RoboMaster视觉组(五)滤波器、观测器和预测方法:卡尔曼滤波器
NanoDet代码逐行精读与修改(五.1)检测头的构造和前向传播
How to resolve the conflict between LAN segment and WAN segment when Honor router (WS831) is used as wireless relay
2022年低压电工练习题及模拟考试
UI中级操作(倾斜和雷达效果)
【每日一题】761. 特殊的二进制序列
Detailed explanation of Oracle's windowing function
已解决ModuleNotFoundError: No module named ‘Workbook‘
NanoDet代码逐行精读与修改(一)Backbone
『HarmonyOS』Page与AbilitySlice的生命周期
给电脑重装系统后修改远程桌面端口的方法
钉钉与RStudio快捷方式冲突--钉钉快捷键设置
软件质效领航者 | 优秀案例•东风集团DevOps改革项目









