下面是多dex加载的时序图:
Android项目有两种方式支持多dex:
1. 项目中的Application类继承MultiDexApplication。
2. 在自己的Application类的attachBaseContext方法中调用MultiDex.install(this);。
我从MultiDexApplication这个类开始分析。
MultiDexApplication类继承了Application,并重载了attachBaseContext方法,在这个方法中调用了MultiDex.install(this);。
- public class MultiDexApplication extends Application {
- @Override
- protected void attachBaseContext(Context base) {
- super.attachBaseContext(base);
- MultiDex.install(this);
- }
- }
MultiDex.install
方法:
-
-
-
-
-
-
-
-
-
-
- public static void install(Context context) {
- Log.i(TAG, "install");
-
- ......
-
- try {
- ApplicationInfo applicationInfo = getApplicationInfo(context);
- if (applicationInfo == null) {
-
- return;
- }
-
- synchronized (installedApk) {
- String apkPath = applicationInfo.sourceDir;
-
-
- if (installedApk.contains(apkPath)) {
- return;
- }
- installedApk.add(apkPath);
-
- ......
-
-
-
-
-
-
-
-
- ClassLoader loader;
-
- ......
-
-
- File dexDir = new File(applicationInfo.dataDir, SECONDARY_FOLDER_NAME);
- List<File> files = MultiDexExtractor.load(context, applicationInfo, dexDir, false);
-
- if (checkValidZipFiles(files)) {
-
- installSecondaryDexes(loader, dexDir, files);
- } else