官方MultiDex源碼分析

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

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

流程分析

基本流程

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

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)) {  //是否已經安裝了
                return;
            }
            installedApk.add(apkPath);
            if (Build.VERSION.SDK_INT > MAX_SUPPORTED_SDK_VERSION) {
                //警告:高于20的可以使用內建的dex分包能力
            }
            /*
             */
            ClassLoader loader;
            try {
                loader = context.getClassLoader();
            } catch (RuntimeException e) {
                // 測試MockContext
                return;
            }
            if (loader == null) {
                // Robolectric tests
                return;
            }
            try {
              clearOldDexDir(context);  //清理應用內部文件存儲目錄(一般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在構造的時候就會讀取指定目錄下的zip、dex、jar等文件,加載成DexFile,并構造成Element數(shù)組,記錄在成員pathList下,以后類的加載都會嘗試在這些DexFile中尋找,而在dex分包后,就需要自己把"新的dex的文件路徑" 告訴DexClassLoader,這里以SDK19+為例子來說(對14,15,16,17and18來說區(qū)別在于DexPathList#makeDexElements方法簽名的改變,4到13的改變稍微有點大,但現(xiàn)在也不會開發(fā)14以下的了就不細看了)

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?反正應該是獲取一個文件的標志

    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需要我們人工進行安裝

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
    }
}

參考

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,182評論 6 543
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,489評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 178,290評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,776評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,510評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,866評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,860評論 3 447
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,036評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 49,585評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,331評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,536評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,058評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,754評論 3 349
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,154評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,469評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,273評論 3 399
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,505評論 2 379

推薦閱讀更多精彩內容