分析google的multidex庫,googlemultidex庫

來源:互聯網
上載者:User

分析google的multidex庫,googlemultidex庫

  我們在開發項目時,喜歡引入好多的第三方包,大大的方便了我們的開發,但同時,因為android方法總數的限制,不能超過65k,然而呢,隨著我們的開發,65k最終還是會超過,所以,google就給出了這個解決方案,但一直好奇它是內部是怎麼實現的,我們今天就來根據源碼來看看這個包到底做了什麼,怎麼把多個dex讀取出來的。先看下這個包裡面都有哪些類:

                

  我們首先看MultiDexApplication,只要我們我們自己的Application繼承MultiDexApplication就可以解決問題,那我們就來看看,它裡面做了什麼,只有一個方法,重寫了attachBaseContext()。

1 protected void attachBaseContext(Context base) {2     super.attachBaseContext(base);3     MultiDex.install(this);4 }

  我們看到,具體的實現是直接調用的Multidex,隨後我們來看下install()裡面有些什麼(關鍵代碼):

 1     public static void install(Context context) { 2         try { 3               . 4               . 5               . 6                 /* The patched class loader is expected to be a descendant of 7                  * dalvik.system.BaseDexClassLoader. We modify its 8                  * dalvik.system.DexPathList pathList field to append additional DEX 9                  * file entries.10                  */11                 ClassLoader loader;12                 try {13                     loader = context.getClassLoader();14                 } catch (RuntimeException e) {15                 }16                 17                 File dexDir = new File(applicationInfo.dataDir, SECONDARY_FOLDER_NAME);18                 List<File> files = MultiDexExtractor.load(context, applicationInfo, dexDir, false);19                 if (checkValidZipFiles(files)) {20                     installSecondaryDexes(loader, dexDir, files);21                 } else {22                   .23                   .24                   .25                 }26             }27         } catch (Exception e) {28         }29     }

  通過ClassLoader的注釋我們已經知道,主dex檔案的路徑被儲存在BaseDexClassLoader中的pathList,這樣就清楚了,下面所要做的就是把其它的dex檔案路徑也找出來,添加到pathList上面即可。這裡面的loader就是BaseDexClassLoader的執行個體

  行18 MultiDexExtractor這個類,從字面上即可知道它是提取dex資訊的,load方法即會根據dex命名規則從指定路徑下提取其它dex檔案(不包含主dex),具體實現可自行看實現,這樣,所有的次dex檔案都被提取出來,賦值給了files

  行19 會檢查所有以上檔案是否是有效zip檔案,如果有一個false,就會重新提取dex檔案

  行20 檢查沒有問題後,就會執行installSecondaryDexes(),已經很明顯知道接下來要做什麼了,我們來看這個方法裡面都做了什麼。

 1     private static void installSecondaryDexes(ClassLoader loader, File dexDir, List<File> files) 2             throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, 3             InvocationTargetException, NoSuchMethodException, IOException { 4         if (!files.isEmpty()) { 5             if (Build.VERSION.SDK_INT >= 19) { 6                 V19.install(loader, files, dexDir); 7             } else if (Build.VERSION.SDK_INT >= 14) { 8                 V14.install(loader, files, dexDir); 9             } else {10                 V4.install(loader, files);11             }12         }13     }

  代碼寫的很明顯,我們來單看V19的實現 

 

 1         private static void install(ClassLoader loader, List<File> additionalClassPathEntries, 2                 File optimizedDirectory) 3                         throws IllegalArgumentException, IllegalAccessException, 4                         NoSuchFieldException, InvocationTargetException, NoSuchMethodException { 5             /* The patched class loader is expected to be a descendant of 6              * dalvik.system.BaseDexClassLoader. We modify its 7              * dalvik.system.DexPathList pathList field to append additional DEX 8              * file entries. 9              */10             Field pathListField = findField(loader, "pathList");11             Object dexPathList = pathListField.get(loader);12             ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>();13             expandFieldArray(dexPathList, "dexElements", makeDexElements(dexPathList,14                     new ArrayList<File>(additionalClassPathEntries), optimizedDirectory,15                     suppressedExceptions));16             .17             .18             .19         }

 

 

 

  我們前面說的注釋又出現了,看來,具體的實現操作就在這裡了,主要是用反射來修改它的值,我們主要來看幾個主要的操作方法:

  行13 這裡面有兩個方法需要注意makeDexElements()和expandFieldArray(),先來說第一個,我們知道在執行個體化BaseDexClassLoader時,會把主dex的路徑資訊存放到pathList裡面,而DexPathList內部其它是把dex的路徑儲存在了一個Element數組中,所以,看makeDexElements()就知道,這個方法,會把傳入的dex檔案通過反射組裝成適合DexPathList內部用的Element數組。

 1         private static Object[] makeDexElements( 2                 Object dexPathList, ArrayList<File> files, File optimizedDirectory, 3                 ArrayList<IOException> suppressedExceptions) 4                         throws IllegalAccessException, InvocationTargetException, 5                         NoSuchMethodException { 6             Method makeDexElements = 7                     findMethod(dexPathList, "makeDexElements", ArrayList.class, File.class, 8                             ArrayList.class); 9 10             return (Object[]) makeDexElements.invoke(dexPathList, files, optimizedDirectory,11                     suppressedExceptions);12         }

  看它代碼是反射調用DexPathList裡面的makeDexElements(),但我一直沒有找到這個方法,不知道是代碼版本的問題還是其它原因,望知道的大神告訴一下。接下來我們再看expandFieldArray()

    private static void expandFieldArray(Object instance, String fieldName,            Object[] extraElements) throws NoSuchFieldException, IllegalArgumentException,            IllegalAccessException {        Field jlrField = findField(instance, fieldName);        Object[] original = (Object[]) jlrField.get(instance);        Object[] combined = (Object[]) Array.newInstance(                original.getClass().getComponentType(), original.length + extraElements.length);        System.arraycopy(original, 0, combined, 0, original.length);        System.arraycopy(extraElements, 0, combined, original.length, extraElements.length);        jlrField.set(instance, combined);    }

  可看出,又重建立立了一個Element[],把原來和新的資料都放上去,最後賦給dexElements。這樣就可以把所有的dex都讀取出來了。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.