前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android 逆向】整体加固脱壳 ( DexClassLoader 加载 dex 流程分析 | DexPathList 中根据 File 加载 DexFile | loadDexFile 分析 )

【Android 逆向】整体加固脱壳 ( DexClassLoader 加载 dex 流程分析 | DexPathList 中根据 File 加载 DexFile | loadDexFile 分析 )

作者头像
韩曙亮
发布2023-03-30 09:22:26
3570
发布2023-03-30 09:22:26
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

前言


上一篇博客 【Android 逆向】整体加固脱壳 ( DexClassLoader 加载 dex 流程分析 | DexPathList 构造函数分析 | makeDexElements 函数分析 ) 中 , 介绍了在 DexPathList 构造函数中调用了 makeDexElements 方法 , 在 makeDexElements 方法中执行了加载 dex 文件的操作 , 将加载后的 dex 文件封装在了 Element 实例对象中 , 并生成了 Element[] 数组 , 每个 dex 文件都对应 Element[] 数组 中的一个元素 ;

本篇博客中重点介绍 dex 文件加载的细节 ;

一、根据 File 加载 DexFile


在 DexPathList 中的 makeDexElements 方法中 , 调用了 loadDexFile 方法 , 根据 Dex 文件的 File 对象 , 创建了 DexFile 对象 ;

在 文件名称 以 .dex 后缀时 与 .apk / .jar / .zip 后缀进行不同的处理 ;

代码语言:javascript
复制
/*package*/ final class DexPathList {
    private static Element[] makeDexElements(ArrayList<File> files, File optimizedDirectory,
                                             ArrayList<IOException> suppressedExceptions) {
        ArrayList<Element> elements = new ArrayList<Element>();

        for (File file : files) {
            File zip = null;
            DexFile dex = null;
            String name = file.getName();

            if (name.endsWith(DEX_SUFFIX)) {
                // Raw dex file (not inside a zip/jar).
                try {
                    dex = loadDexFile(file, optimizedDirectory);
                } catch (IOException ex) {
                }
            } else if (name.endsWith(APK_SUFFIX) || name.endsWith(JAR_SUFFIX)
                    || name.endsWith(ZIP_SUFFIX)) {
                zip = file;

                try {
                    dex = loadDexFile(file, optimizedDirectory);
                } catch (IOException suppressed) {
                }
			}
		}
	}
}

源码路径 : /libcore/dalvik/src/main/java/dalvik/system/DexPathList.java

二、DexPathList.loadDexFile 函数分析


在 DexPathList. loadDexFile 方法中 , 主要是调用了 DexFile.loadDex 方法 生成 DexFile 实例对象 ;

执行 DexFile.loadDex , 先调用了 optimizedPathFor 方法 , 根据 dex 文件路径 和 优化目录 生成一个相关的 优化 dex 文件路径 ;

代码语言:javascript
复制
/*package*/ final class DexPathList {
    /**
     * Constructs a {@code DexFile} instance, as appropriate depending
     * on whether {@code optimizedDirectory} is {@code null}.
     */
    private static DexFile loadDexFile(File file, File optimizedDirectory)
            throws IOException {
        if (optimizedDirectory == null) {
            return new DexFile(file);
        } else {
            String optimizedPath = optimizedPathFor(file, optimizedDirectory);
            return DexFile.loadDex(file.getPath(), optimizedPath, 0);
        }
    }
}

源码路径 : /libcore/dalvik/src/main/java/dalvik/system/DexPathList.java

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-12-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 前言
  • 一、根据 File 加载 DexFile
  • 二、DexPathList.loadDexFile 函数分析
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档