加载多个dex

本文详细解析了Android多DEX加载的过程,从MultiDexApplication出发,解释了如何通过attachBaseContext方法安装额外的DEX文件。内容包括MultiDex类的内部实现,如检查和加载已存在或新的DEX文件,以及如何在不同Android版本下安装多DEX。通过分析,展示了如何在应用启动时动态加载和管理额外的DEX文件,以解决Dalvik和ART环境下的方法数限制问题。
摘要由CSDN通过智能技术生成

下面是多dex加载的时序图: 



Android项目有两种方式支持多dex:

1. 项目中的Application类继承MultiDexApplication
2. 在自己的Application类的attachBaseContext方法中调用MultiDex.install(this);


我从MultiDexApplication这个类开始分析。


MultiDexApplication类继承了Application,并重载了attachBaseContext方法,在这个方法中调用了MultiDex.install(this);

[java]  view plain   copy
  在CODE上查看代码片 派生到我的代码片
  1. public class MultiDexApplication extends Application {  
  2.   @Override  
  3.   protected void attachBaseContext(Context base) {  
  4.     super.attachBaseContext(base);  
  5.     MultiDex.install(this);  
  6.   }  
  7. }  

MultiDex.install 方法:

[java]  view plain   copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Patches the application context class loader by appending extra dex files 
  3.  * loaded from the application apk. This method should be called in the 
  4.  * attachBaseContext of your {@link Application}, see 
  5.  * {@link MultiDexApplication} for more explanation and an example. 
  6.  * 
  7.  * @param context application context. 
  8.  * @throws RuntimeException if an error occurred preventing the classloader 
  9.  *         extension. 
  10.  */  
  11. public static void install(Context context) {  
  12.     Log.i(TAG, "install");  
  13.   
  14.     ......  
  15.   
  16.     try {  
  17.         ApplicationInfo applicationInfo = getApplicationInfo(context);  
  18.         if (applicationInfo == null) {  
  19.             // Looks like running on a test Context, so just return without patching.  
  20.             return;  
  21.         }  
  22.   
  23.         synchronized (installedApk) {  
  24.             String apkPath = applicationInfo.sourceDir;  
  25.             // installedApk的类型是:Set<String>。  
  26.             // 如果这个apk已经安装,则不重复安装。  
  27.             if (installedApk.contains(apkPath)) {  
  28.                 return;  
  29.             }  
  30.             installedApk.add(apkPath);  
  31.   
  32.             ......  
  33.   
  34.             // 类加载器应该直接或间接继承于BaseDexClassLoader。  
  35.             // 修改BaseDexClassLoader类中的DexPathList pathList字段,追加额外的DEX文件项。  
  36.             /* The patched class loader is expected to be a descendant of 
  37.              * dalvik.system.BaseDexClassLoader. We modify its 
  38.              * dalvik.system.DexPathList pathList field to append additional DEX 
  39.              * file entries. 
  40.              */  
  41.             ClassLoader loader;  
  42.   
  43.             ......  
  44.   
  45.             // dex将会输出到SECONDARY_FOLDER_NAME目录。  
  46.             File dexDir = new File(applicationInfo.dataDir, SECONDARY_FOLDER_NAME);  
  47.             List<File> files = MultiDexExtractor.load(context, applicationInfo, dexDir, false);  
  48.             // 校验这些zip文件是否合法。  
  49.             if (checkValidZipFiles(files)) {  
  50.                 // 安装提取出来的zip文件。  
  51.                 installSecondaryDexes(loader, dexDir, files);  
  52.             } else
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yue31313

感谢打赏,继续分享,给您帮忙。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值