官方MultiDex源碼分析

目的是為了解決65535問題,支持的SDK是4以上,低了會拋異常,Android5.0以上的虛擬機本來就可以支持Dex分包加載

主要原理:為應(yīng)用的DexClassLoader動態(tài)地添加dex文件

流程分析

基本流程

1、校驗(Vm是否已經(jīng)支持分包如21+,最低SDK版本是4,是否已經(jīng)分包過了)

2、清理舊的的dex分包的目錄下文件,data/data/packageName/file/secondary-dexes

3、Dex包讀取,存放目錄data/data/packageName/code_cache/secondary-dexes

  • 3.1 主要是讀取apk壓縮包下的的classes2.dex、classesN.dex依次寫入/data/data/pkgName/code_cache/secondary-dexes/base.apk.classesN.zip

4、校驗分包的dex壓縮包是否有效,無效再進行一次分包

5、Dex壓縮包文件安裝加載,通過DexPathList#makeDexElements的方法進行dex的加載,用返回的Element數(shù)組擴充原來ClassLoader下的Elements實現(xiàn)加載

public static void install(Context context) {
    if (IS_VM_MULTIDEX_CAPABLE) {
        Log.i(TAG, "VM has multidex support, MultiDex support library is disabled.");
        return;
    }
    if (Build.VERSION.SDK_INT < MIN_SDK_VERSION) {
        throw new RuntimeException("Multi dex installation failed. SDK " + Build.VERSION.SDK_INT + " is unsupported. Min SDK version is " + MIN_SDK_VERSION + ".");
    }
    try {
        ApplicationInfo applicationInfo = getApplicationInfo(context);
        if (applicationInfo == null) {
            // Looks like running on a test Context, so just return without patching.
            return;
        }
        synchronized (installedApk) {
            String apkPath = applicationInfo.sourceDir;
            if (installedApk.contains(apkPath)) {  //是否已經(jīng)安裝了
                return;
            }
            installedApk.add(apkPath);
            if (Build.VERSION.SDK_INT > MAX_SUPPORTED_SDK_VERSION) {
                //警告:高于20的可以使用內(nèi)建的dex分包能力
            }
            /*
             */
            ClassLoader loader;
            try {
                loader = context.getClassLoader();
            } catch (RuntimeException e) {
                // 測試MockContext
                return;
            }
            if (loader == null) {
                // Robolectric tests
                return;
            }
            try {
              clearOldDexDir(context);  //清理應(yīng)用內(nèi)部文件存儲目錄(一般data/data/pkg-name/)下的secondary-dexes目錄
            } catch (Throwable t) {
            }
            // data/data/packageName/code_cache/secondary-dexes
            File dexDir = new File(applicationInfo.dataDir, SECONDARY_FOLDER_NAME);
            List<File> files = MultiDexExtractor.load(context, applicationInfo, dexDir, false); //返回分包后的zip文件列表
            if (checkValidZipFiles(files)) {  //檢驗zip文件是否有效
                installSecondaryDexes(loader, dexDir, files);
            } else {
                //如果第一失敗了,再進行一次相同的加載操作
            }
        }

    } catch (Exception e) {
    }
}

如何安裝

DexClassLoader在構(gòu)造的時候就會讀取指定目錄下的zip、dex、jar等文件,加載成DexFile,并構(gòu)造成Element數(shù)組,記錄在成員pathList下,以后類的加載都會嘗試在這些DexFile中尋找,而在dex分包后,就需要自己把"新的dex的文件路徑" 告訴DexClassLoader,這里以SDK19+為例子來說(對14,15,16,17and18來說區(qū)別在于DexPathList#makeDexElements方法簽名的改變,4到13的改變稍微有點大,但現(xiàn)在也不會開發(fā)14以下的了就不細(xì)看了)

private static void installSecondaryDexes(ClassLoader loader, File dexDir, List<File> files) {
    if (!files.isEmpty()) {
        if (Build.VERSION.SDK_INT >= 19) {
            V19.install(loader, files, dexDir);
        } else if (Build.VERSION.SDK_INT >= 14) {
            V14.install(loader, files, dexDir);
        } else {
            V4.install(loader, files);
        }
    }
}

主要用DexPathList#makeDexElements的方法進行dex的加載,用返回的Element數(shù)組擴充原來ClassLoader下的Elements

private static final class V19 {

    private static void install(ClassLoader loader, List<File> additionalClassPathEntries, File optimizedDirectory) {

        Field pathListField = findField(loader, "pathList");  //loader#pathList字段,DexPathList類型
        Object dexPathList = pathListField.get(loader);
        ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>();
        expandFieldArray(dexPathList, "dexElements", makeDexElements(dexPathList, new ArrayList<File>(additionalClassPathEntries), optimizedDirectory, suppressedExceptions));
        if (suppressedExceptions.size() > 0) {
            for (IOException e : suppressedExceptions) {
                Log.w(TAG, "Exception in makeDexElement", e);
            }
            //.....
        }
    }

    /**
     * {@code private static final dalvik.system.DexPathList#makeDexElements}.
     * 這個方法用來執(zhí)行DexPathList#makeDexElements的方法輸入需要加載的dex目錄,返回`Element`數(shù)組
     */
    private static Object[] makeDexElements(
            Object dexPathList, ArrayList<File> files, File optimizedDirectory, ArrayList<IOException> suppressedExceptions) {
        Method makeDexElements = findMethod(dexPathList, "makeDexElements", ArrayList.class, File.class,  ArrayList.class);
        return (Object[]) makeDexElements.invoke(dexPathList, files, optimizedDirectory,suppressedExceptions);
    }
}

Dex讀取

Dex的讀取在MultiDexExtractor#load方法進行

MultiDexExtractor.java

static List<File> load(Context context, ApplicationInfo applicationInfo, File dexDir, boolean forceReload) throws IOException {
    final File sourceApk = new File(applicationInfo.sourceDir); //data/app/packageName/base.apk

    long currentCrc = getZipCrc(sourceApk); //返回一個crc32值,類似MD5?反正應(yīng)該是獲取一個文件的標(biāo)志

    List<File> files;
    //檢驗安裝文件是否發(fā)生了改變,如果是重新加載
    if (!forceReload && !isModified(context, sourceApk, currentCrc)) {
            //...
            files = loadExistingExtractions(context, sourceApk, dexDir);
            //...
    } else {
        files = performExtractions(sourceApk, dexDir);
        putStoredApkInfo(context, getTimeStamp(sourceApk), currentCrc, files.size() + 1); //dex分包情況記錄在sp,以便下次可以根據(jù)別配加載
    }
    return files;
}

private static boolean isModified(Context context, File archive, long currentCrc) {
    SharedPreferences prefs = getMultiDexPreferences(context);//multidex.version
    return (prefs.getLong(KEY_TIME_STAMP, NO_VALUE) != getTimeStamp(archive)) || (prefs.getLong(KEY_CRC, NO_VALUE) != currentCrc);
}

主要來看怎么讀取DEX,獲取apk文件的名字classesNdexZipEntry,寫入到文件data/data/packageName/code_cache/secondary-dexes/base.apk.classesN.zip,N為dex的數(shù)量,2開始。因為Android系統(tǒng)在啟動app時只加載了第一個Classes.dex,其他的DEX需要我們?nèi)斯みM行安裝

private static List<File> performExtractions(File sourceApk, File dexDir) throws IOException {

    final String extractedFilePrefix = sourceApk.getName() + "classes"; //base.apk.classes

    // Ensure that whatever deletions happen in prepareDexDir only happen if the zip that
    // contains a secondary dex file in there is not consistent with the latest apk.  Otherwise,
    // multi-process race conditions can cause a crash loop where one process deletes the zip
    // while another had created it.
    prepareDexDir(dexDir, extractedFilePrefix); //刪除非base.apk.classes為前綴的文件

    List<File> files = new ArrayList<File>();

    final ZipFile apk = new ZipFile(sourceApk); //data/app/packageName/base.apk
    try {
        int secondaryNumber = 2;

        ZipEntry dexFile = apk.getEntry("classes" + secondaryNumber + "dex"); //獲取ZipEntry
        while (dexFile != null) {
            String fileName = extractedFilePrefix + secondaryNumber + "zip"; //base.classes2.zip,往后便是base.classes3.dex、base.classes4.dex、base.classesN.dex
            File extractedFile = new File(dexDir, fileName);   //data/data/packageName/code_cache/secondary-dexes/base.classes2.zip
            files.add(extractedFile);

            int numAttempts = 0;
            boolean isExtractionSuccessful = false;
            while (numAttempts < 3 && !isExtractionSuccessful) { //最多3次嘗試
                numAttempts++;
                // Create a zip file (extractedFile) containing only the secondary dex file  (dexFile) from the apk.
                extract(apk, dexFile, extractedFile, extractedFilePrefix);  //ZipEntry寫入到指定文件

                isExtractionSuccessful = verifyZipFile(extractedFile);  //是否是有效的zip文件

                // Log the sha1 of the extracted zip file
                if (!isExtractionSuccessful) {
                    // Delete the extracted file
                    extractedFile.delete();
                    //...
                }
            }
            if (!isExtractionSuccessful) {
                //...
            }
            secondaryNumber++;
            dexFile = apk.getEntry(DEX_PREFIX + secondaryNumber + DEX_SUFFIX);
        } //end while
    } finally {
        //..
    }

    return files;
}

刪除data/data/packageName/code_cache/secondary-dexes/目錄下所有非base.apk.classes開頭的文件

/**
 * This removes any files that do not have the correct prefix.
 */
private static void prepareDexDir(File dexDir, final String extractedFilePrefix) throws IOException {
    /* mkdirs() has some bugs, especially before jb-mr1 and we have only a maximum of one parent
     * to create, lets stick to mkdir().
     */
    File cache = dexDir.getParentFile();
    mkdirChecked(cache);  //`data/data/packageName/code_cache/`
    mkdirChecked(dexDir); //`data/data/packageName/code_cache/secondary-dexes/`

    // Clean possible old files
    FileFilter filter = new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            return !pathname.getName().startsWith(extractedFilePrefix); //過濾base.apk.classes前綴的文件
        }
    };
    File[] files = dexDir.listFiles(filter);
    if (files == null) {
        return;
    }
    for (File oldFile : files) {
        if (!oldFile.delete()) {
            Log.w(TAG, "Failed to delete old file " + oldFile.getPath());
        } else {
            Log.i(TAG, "Deleted old file " + oldFile.getPath());
        }
    }
}

ZipEntry寫入文件,具體文件data/data/packageName/code_cache/secondary-dexes/base.apk.classesN.zip

/**
* apk : apk的壓縮包文件
* dexFile : Apk文件zip解壓后得到的從dex文件,classes2.dex…classesN.dex
* extractTo : data/data/packageName/code_cache/secondary-dexes/base.apk.classesN.zip
* extractedFilePrefix : base.apk.classes
*/
private static void extract(ZipFile apk, ZipEntry dexFile, File extractTo, String extractedFilePrefix) {

    InputStream in = apk.getInputStream(dexFile);
    ZipOutputStream out = null;
    File tmp = File.createTempFile(extractedFilePrefix, "zip", extractTo.getParentFile());
    try {
        out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(tmp)));
        try {
            ZipEntry classesDex = new ZipEntry("classes.dex");
            // keep zip entry time since it is the criteria used by Dalvik
            classesDex.setTime(dexFile.getTime());
            out.putNextEntry(classesDex);

            byte[] buffer = new byte[BUFFER_SIZE];
            int length = in.read(buffer);
            while (length != -1) {
                out.write(buffer, 0, length);
                length = in.read(buffer);
            }
            out.closeEntry();
        } finally {
            out.close();
        }
        if (!tmp.renameTo(extractTo)) {
            //...
        }
    } finally {
        closeQuietly(in);
        tmp.delete(); // return status ignored
    }
}

參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容