日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Multidex实现简要分析

發(fā)布時(shí)間:2024/4/14 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Multidex实现简要分析 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、Multidex的產(chǎn)生

在android5.0之前,每一個(gè)android應(yīng)用中只會(huì)含有一個(gè)dex文件,但是因?yàn)锳ndroid系統(tǒng)本身的BUG,使得這個(gè)dex的方法數(shù)量被限制在65535之內(nèi),這就是著名的"64K(64*1024)"事件。為了解決這個(gè)問題,Google官方推出了這個(gè)類似于補(bǔ)丁一樣的support-library。關(guān)于這個(gè)庫的詳細(xì)使用,可以參考官方文檔,當(dāng)然使用起來也會(huì)有些坑的,美團(tuán)填坑記或者這位老兄。使用這個(gè)庫后,我們的APP不再只會(huì)僅有一個(gè)dex文件,可能會(huì)產(chǎn)生多個(gè)dex文件,這樣就避免了64K問題。

2、使用方式

對于Multidex的使用,大致有以下幾種方式
直接繼承MultiDexApplication

public class MyApplication extends MultiDexApplication{// ........... }

直接調(diào)用MultiDex.install(Context);

public class MyApplication extends Application{public void onCreate(){MultiDex.install(this);} }

這兩種使用方式,其實(shí)本質(zhì)是一樣的,都是通過MultiDex.install(this)來完成dex的加載,看看MultiDexApplication的實(shí)現(xiàn):

public class MultiDexApplication extends Application {@Overrideprotected void attachBaseContext(Context base) {super.attachBaseContext(base);MultiDex.install(this);} }

3、android中的類加載機(jī)制

在分析MultiDex.install(Context)之前,先了解一下android中的類是如何加載的。在android中,類的加載可以分為DexClassLoader.和PathClassLoader,這里先看看他們各自的實(shí)現(xiàn):
DexClassLoader

/*** A class loader that loads classes from {@code .jar} and {@code .apk} files* containing a {@code classes.dex} entry. This can be used to execute code not* installed as part of an application.* 從包含dex文件的jar或是apk中加載classes。該ClassLoader可以用來加載外部的classes,* 也就是可以加載沒有預(yù)先安裝的含有dex文件的jar或是apk。** <p>This class loader requires an application-private, writable directory to* cache optimized classes. Use {@code Context.getDir(String, int)} to create* such a directory: <pre> {@code* File dexOutputDir = context.getDir("dex", 0);* }</pre>** <p><strong>Do not cache optimized classes on external storage.</strong>* External storage does not provide access controls necessary to protect your* application from code injection attacks.*/ public class DexClassLoader extends BaseDexClassLoader {/*** Creates a {@code DexClassLoader} that finds interpreted and native* code. Interpreted classes are found in a set of DEX files contained* in Jar or APK files.** <p>The path lists are separated using the character specified by the* {@code path.separator} system property, which defaults to {@code :}.** @param dexPath the list of jar/apk files containing classes and* resources, delimited by {@code File.pathSeparator}, which* defaults to {@code ":"} on Android* 含有classes和resources的文件(jar/apk)路徑,多個(gè)文件以 File.pathSeparator(Linux為":"Windows為";")分割開來,比如:xx/xx/aa.apk:yy/yy/bb.apk* @param optimizedDirectory directory where optimized dex files* should be written; must not be {@code null}* dex文件所在的根路徑,不能為null* @param libraryPath the list of directories containing native* libraries, delimited by {@code File.pathSeparator}; may be* {@code null}* SO文件所在的路徑* @param parent the parent class loader*/public DexClassLoader(String dexPath, String optimizedDirectory,String libraryPath, ClassLoader parent) {super(dexPath, new File(optimizedDirectory), libraryPath, parent);} }

PathClassLoader

/*** Provides a simple {@link ClassLoader} implementation that operates on a list* of files and directories in the local file system, but does not attempt to* load classes from the network. Android uses this class for its system class* loader and for its application class loader(s).* 不能加載從網(wǎng)絡(luò)上獲取的classes,Android用這個(gè)類來加載系統(tǒng)classes和已經(jīng)安裝的應(yīng)用的classes*/ public class PathClassLoader extends BaseDexClassLoader {/*** Creates a {@code PathClassLoader} that operates on a given list of files* and directories. This method is equivalent to calling* {@link #PathClassLoader(String, String, ClassLoader)} with a* {@code null} value for the second argument (see description there).** @param dexPath the list of jar/apk files containing classes and* resources, delimited by {@code File.pathSeparator}, which* defaults to {@code ":"} on Android* @param parent the parent class loader*/public PathClassLoader(String dexPath, ClassLoader parent) {super(dexPath, null, null, parent);}/*** Creates a {@code PathClassLoader} that operates on two given* lists of files and directories. The entries of the first list* should be one of the following:** <ul>* <li>JAR/ZIP/APK files, possibly containing a "classes.dex" file as* well as arbitrary resources.* <li>Raw ".dex" files (not inside a zip file).* </ul>* dexPath只支持jar/zip/apk/dex四種文件** The entries of the second list should be directories containing* native library files.** @param dexPath the list of jar/apk files containing classes and* resources, delimited by {@code File.pathSeparator}, which* defaults to {@code ":"} on Android* @param libraryPath the list of directories containing native* libraries, delimited by {@code File.pathSeparator}; may be* {@code null}* @param parent the parent class loader*/public PathClassLoader(String dexPath, String libraryPath,ClassLoader parent) {super(dexPath, null, libraryPath, parent);} }

從類的說明以及構(gòu)造函數(shù)可以明顯的知道DexClassLoader和PathClassLoader區(qū)別:DexClassLoader可以加載未安裝的含有dex文件的jar或是apk,而PathClassLoader只能加載已安裝好的含有dex文件的jar/apk/zip/dex。

BaseDexClassLoader

public class BaseDexClassLoader extends ClassLoader {/** structured lists of path elements */private final DexPathList pathList;public BaseDexClassLoader(String dexPath, File optimizedDirectory,String libraryPath, ClassLoader parent) {super(parent);this.originalPath = dexPath;this.pathList =new DexPathList(this, dexPath, libraryPath, optimizedDirectory);} }

DexPathList

/*** A pair of lists of entries, associated with a {@code ClassLoader}.* One of the lists is a dex/resource path &mdash; typically referred* to as a "class path" &mdash; list, and the other names directories* containing native code libraries. Class path entries may be any of:* a {@code .jar} or {@code .zip} file containing an optional* top-level {@code classes.dex} file as well as arbitrary resources,* or a plain {@code .dex} file (with no possibility of associated* resources).** <p>This class also contains methods to use these lists to look up* classes and resources.</p>*/ /*package*/ final class DexPathList {private static final String DEX_SUFFIX = ".dex";private static final String JAR_SUFFIX = ".jar";private static final String ZIP_SUFFIX = ".zip";private static final String APK_SUFFIX = ".apk";/** class definition context */private final ClassLoader definingContext;/** list of dex/resource (class path) elements */private final Element[] dexElements;/** list of native library directory elements */private final File[] nativeLibraryDirectories;public DexPathList(ClassLoader definingContext, String dexPath,String libraryPath, File optimizedDirectory) {if (definingContext == null) {throw new NullPointerException("definingContext == null");}if (dexPath == null) {throw new NullPointerException("dexPath == null");}if (optimizedDirectory != null) {// 對于PathClassloader來說optimizedDirectory是恒等于null的if (!optimizedDirectory.exists()) {throw new IllegalArgumentException("optimizedDirectory doesn't exist: "+ optimizedDirectory);}if (!(optimizedDirectory.canRead()&& optimizedDirectory.canWrite())) {throw new IllegalArgumentException("optimizedDirectory not readable/writable: "+ optimizedDirectory);}}// classLoaderthis.definingContext = definingContext;// 把每個(gè)dex文件的相關(guān)信息存儲(chǔ)到Element這個(gè)對象中this.dexElements =makeDexElements(splitDexPath(dexPath), optimizedDirectory);// 本地so文件路徑this.nativeLibraryDirectories = splitLibraryPath(libraryPath);}//...... }

首先是通過splitDexPath()得到含有dex文件的jar/zip/dex/apk集合:

/*** Splits the given dex path string into elements using the path* separator, pruning out any elements that do not refer to existing* and readable files. (That is, directories are not included in the* result.)*/private static ArrayList<File> splitDexPath(String path) {return splitPaths(path, null, false);}/*** Splits the given path strings into file elements using the path* separator, combining the results and filtering out elements* that don't exist, aren't readable, or aren't either a regular* file or a directory (as specified). Either string may be empty* or {@code null}, in which case it is ignored. If both strings* are empty or {@code null}, or all elements get pruned out, then* this returns a zero-element list.*/private static ArrayList<File> splitPaths(String path1, String path2,boolean wantDirectories) {ArrayList<File> result = new ArrayList<File>();splitAndAdd(path1, wantDirectories, result);splitAndAdd(path2, wantDirectories, result);return result;}/*** Helper for {@link #splitPaths}, which does the actual splitting* and filtering and adding to a result.*/private static void splitAndAdd(String path, boolean wantDirectories,ArrayList<File> resultList) {if (path == null) {return;}// 路徑分離String[] strings = path.split(Pattern.quote(File.pathSeparator));for (String s : strings) {File file = new File(s);if (! (file.exists() && file.canRead())) {continue;}/** Note: There are other entities in filesystems than* regular files and directories.*/if (wantDirectories) {if (!file.isDirectory()) {continue;}} else {if (!file.isFile()) {continue;}}resultList.add(file);}}

然后通過makeDexElements()來把dex信息保存到Element對象中:

/*** Makes an array of dex/resource path elements, one per element of* the given array.*/private static Element[] makeDexElements(ArrayList<File> files,File optimizedDirectory) {ArrayList<Element> elements = new ArrayList<Element>();/** Open all files and load the (direct or contained) dex files* up front.*/for (File file : files) {ZipFile zip = null;DexFile dex = null;String name = file.getName();if (name.endsWith(DEX_SUFFIX)) {// 直接是.dex文件// Raw dex file (not inside a zip/jar).try {dex = loadDexFile(file, optimizedDirectory);} catch (IOException ex) {System.logE("Unable to load dex file: " + file, ex);}} else if (name.endsWith(APK_SUFFIX) || name.endsWith(JAR_SUFFIX)|| name.endsWith(ZIP_SUFFIX)) {// 其他三種格式文件:都是壓縮包啊try {zip = new ZipFile(file);} catch (IOException ex) {/** Note: ZipException (a subclass of IOException)* might get thrown by the ZipFile constructor* (e.g. if the file isn't actually a zip/jar* file).*/System.logE("Unable to open zip file: " + file, ex);}try {dex = loadDexFile(file, optimizedDirectory);} catch (IOException ignored) {/** IOException might get thrown "legitimately" by* the DexFile constructor if the zip file turns* out to be resource-only (that is, no* classes.dex file in it). Safe to just ignore* the exception here, and let dex == null.*/}} else {System.logW("Unknown file type for: " + file);}// 格式符合要求,放到數(shù)組中if ((zip != null) || (dex != null)) {// 依靠DexFile創(chuàng)建Element對象elements.add(new Element(file, zip, dex));}}return elements.toArray(new Element[elements.size()]);} /*** Constructs a {@code DexFile} instance, as appropriate depending* on whether {@code optimizedDirectory} is {@code null}.* .dex文件轉(zhuǎn)成DexFile對象*/private static DexFile loadDexFile(File file, File optimizedDirectory)throws IOException {if (optimizedDirectory == null) {return new DexFile(file);} else {String optimizedPath = optimizedPathFor(file, optimizedDirectory);return DexFile.loadDex(file.getPath(), optimizedPath, 0);}}/*** Converts a dex/jar file path and an output directory to an* output file path for an associated optimized dex file.* 主要是對非.dex結(jié)尾的文件加上.dex后綴,統(tǒng)一為.dex后綴* 沒有使用.odex為后綴,是因?yàn)闃?gòu)建系統(tǒng)會(huì)把.odex文件認(rèn)為是只含有資源的文件*/private static String optimizedPathFor(File path,File optimizedDirectory) {/** Get the filename component of the path, and replace the* suffix with ".dex" if that's not already the suffix.** We don't want to use ".odex", because the build system uses* that for files that are paired with resource-only jar* files. If the VM can assume that there's no classes.dex in* the matching jar, it doesn't need to open the jar to check* for updated dependencies, providing a slight performance* boost at startup. The use of ".dex" here matches the use on* files in /data/dalvik-cache.*/String fileName = path.getName();if (!fileName.endsWith(DEX_SUFFIX)) {int lastDot = fileName.lastIndexOf(".");if (lastDot < 0) {fileName += DEX_SUFFIX;} else {StringBuilder sb = new StringBuilder(lastDot + 4);sb.append(fileName, 0, lastDot);sb.append(DEX_SUFFIX);fileName = sb.toString();}}File result = new File(optimizedDirectory, fileName);return result.getPath();}

以上,是android中類加載過程,從中可以發(fā)現(xiàn),android中的classloader能加載的文件格式只能為zip/apk/dex/jar,加載目標(biāo)是把dex文件轉(zhuǎn)化成一個(gè)Element對象,以供后續(xù)查找使用。

4、MultiDex.install(Context)

/*** Patches the application context class loader by appending extra dex files* loaded from the application apk. This method should be called in the* attachBaseContext of your {@link Application}, see* {@link MultiDexApplication} for more explanation and an example.** @param context application context.* @throws RuntimeException if an error occurred preventing the classloader* extension.*/public static void install(Context context) {Log.i(TAG, "install");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) {// 最低版本是4throw new RuntimeException("Multi dex installation failed. SDK " + Build.VERSION.SDK_INT+ " is unsupported. Min SDK version is " + MIN_SDK_VERSION + ".");}try {// 獲取到應(yīng)用的相關(guān)信息ApplicationInfo applicationInfo = getApplicationInfo(context);if (applicationInfo == null) {// Looks like running on a test Context, so just return without patching.return;}synchronized (installedApk) {// dataDir = /data/data/com.example.android.animationsdemo// apkPath = sourceDir = /data/app/com.example.android.animationsdemo-1.apkString apkPath = applicationInfo.sourceDir;if (installedApk.contains(apkPath)) {return;}installedApk.add(apkPath);if (Build.VERSION.SDK_INT > MAX_SUPPORTED_SDK_VERSION) {Log.w(TAG, "MultiDex is not guaranteed to work in SDK version "+ Build.VERSION.SDK_INT + ": SDK version higher than "+ MAX_SUPPORTED_SDK_VERSION + " should be backed by "+ "runtime with built-in multidex capabilty but it's not the "+ "case here: java.vm.version=\""+ System.getProperty("java.vm.version") + "\"");}/* The patched class loader is expected to be a descendant of* dalvik.system.BaseDexClassLoader. We modify its* dalvik.system.DexPathList pathList field to append additional DEX* file entries.*/ClassLoader loader;try {//dalvik.system.PathClassLoaderloader = context.getClassLoader();} catch (RuntimeException e) {/* Ignore those exceptions so that we don't break tests relying on Context like* a android.test.mock.MockContext or a android.content.ContextWrapper with a* null base Context.*/Log.w(TAG, "Failure while trying to obtain Context class loader. " +"Must be running in test mode. Skip patching.", e);return;}if (loader == null) {// Note, the context class loader is null when running Robolectric tests. Log.e(TAG,"Context class loader is null. Must be running in test mode. "+ "Skip patching.");return;}try {// 清空放置dex文件的文件夾 clearOldDexDir(context);} catch (Throwable t) {Log.w(TAG, "Something went wrong when trying to clear old MultiDex extraction, "+ "continuing without cleaning.", t);}// 創(chuàng)建放置dex文件的文件夾 // dexDir = /data/data/com.example.android.animationsdemo/code_cache/secondary-dexesFile dexDir = getDexDir(context, applicationInfo);// 得到APK中所有dex對應(yīng)的zip文件List<File> files = MultiDexExtractor.load(context, applicationInfo, dexDir, false);// if (checkValidZipFiles(files)) {// 加載dex到 installSecondaryDexes(loader, dexDir, files);} else {// 強(qiáng)制再轉(zhuǎn)換一次Log.w(TAG, "Files were not valid zip files. Forcing a reload.");// Try again, but this time force a reload of the zip file.files = MultiDexExtractor.load(context, applicationInfo, dexDir, true);if (checkValidZipFiles(files)) {installSecondaryDexes(loader, dexDir, files);} else {// Second time didn't work, give upthrow new RuntimeException("Zip files were not valid.");}}}} catch (Exception e) {Log.e(TAG, "Multidex installation failure", e);throw new RuntimeException("Multi dex installation failed (" + e.getMessage() + ").");}Log.i(TAG, "install done");}

整個(gè)加載過程主要分為3步驟:clearOldDexDir():清理過期數(shù)據(jù)MultiDexExtractor.load:把dex轉(zhuǎn)換成對應(yīng)的zip文件installSecondaryDexes:dex轉(zhuǎn)換成對應(yīng)的Element對象清理過期文件 :刪除/data/data/(pkg)/files/secondary-dexes路徑下的所有文件和該文件夾作者:紫苓鏈接:http://www.jianshu.com/p/dd90d7e7c691來源:簡書著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。

private static void clearOldDexDir(Context context) throws Exception {// dexDir = /data/data/com.example.android.animationsdemo/files/secondary-dexesFile dexDir = new File(context.getFilesDir(), OLD_SECONDARY_FOLDER_NAME);if (dexDir.isDirectory()) {Log.i(TAG, "Clearing old secondary dex dir (" + dexDir.getPath() + ").");File[] files = dexDir.listFiles();if (files == null) {Log.w(TAG, "Failed to list secondary dex dir content (" + dexDir.getPath() + ").");return;}for (File oldFile : files) {Log.i(TAG, "Trying to delete old file " + oldFile.getPath() + " of size "+ oldFile.length());if (!oldFile.delete()) {Log.w(TAG, "Failed to delete old file " + oldFile.getPath());} else {Log.i(TAG, "Deleted old file " + oldFile.getPath());}}if (!dexDir.delete()) {Log.w(TAG, "Failed to delete secondary dex dir " + dexDir.getPath());} else {Log.i(TAG, "Deleted old secondary dex dir " + dexDir.getPath());}}}

把dex文件轉(zhuǎn)換成對應(yīng)的zip文件?:classesN.dex-->/data/data/(包名)/code_cache/secondary-dexes/(包名)-1.apk.classesN.zip

static List<File> load(Context context, ApplicationInfo applicationInfo, File dexDir,boolean forceReload) throws IOException {Log.i(TAG, "MultiDexExtractor.load(" + applicationInfo.sourceDir + ", " + forceReload + ")");// sourceApk = /data/app/com.example.android.animationsdemo-1.apkfinal File sourceApk = new File(applicationInfo.sourceDir);long currentCrc = getZipCrc(sourceApk);// Validity check and extraction must be done only while the lock file has been taken.File lockFile = new File(dexDir, LOCK_FILENAME);RandomAccessFile lockRaf = new RandomAccessFile(lockFile, "rw");FileChannel lockChannel = null;FileLock cacheLock = null;List<File> files;IOException releaseLockException = null;try {lockChannel = lockRaf.getChannel();Log.i(TAG, "Blocking on lock " + lockFile.getPath());cacheLock = lockChannel.lock();Log.i(TAG, lockFile.getPath() + " locked");if (!forceReload && !isModified(context, sourceApk, currentCrc)) {// APK信息沒有發(fā)生變化try {// dex對應(yīng)的zip文件已經(jīng)存在files = loadExistingExtractions(context, sourceApk, dexDir);} catch (IOException ioe) {Log.w(TAG, "Failed to reload existing extracted secondary dex files,"+ " falling back to fresh extraction", ioe);files = performExtractions(sourceApk, dexDir);putStoredApkInfo(context,getTimeStamp(sourceApk), currentCrc, files.size() + 1);}} else {// second dex 發(fā)生了改變Log.i(TAG, "Detected that extraction must be performed.");files = performExtractions(sourceApk, dexDir);// 保存APK相關(guān)信息:時(shí)間戳、總dex數(shù)等putStoredApkInfo(context, getTimeStamp(sourceApk), currentCrc, files.size() + 1);}} finally {if (cacheLock != null) {try {cacheLock.release();} catch (IOException e) {Log.e(TAG, "Failed to release lock on " + lockFile.getPath());// Exception while releasing the lock is bad, we want to report it, but not at// the price of overriding any already pending exception.releaseLockException = e;}}if (lockChannel != null) {closeQuietly(lockChannel);}closeQuietly(lockRaf);}if (releaseLockException != null) {throw releaseLockException;}Log.i(TAG, "load found " + files.size() + " secondary dex files");return files;}/*** 把APK文件中的classesN.dex(N >= 2)轉(zhuǎn)換成對應(yīng)的zip文件,保存到集合中* zip文件:/data/data/(包名)/code_cache/secondary-dexes/(包名)-1.apk.classesN.zip*/private static List<File> performExtractions(File sourceApk, File dexDir)throws IOException {// extractedFilePrefix = com.example.android.animationsdemo-1.apk.classesfinal String extractedFilePrefix = sourceApk.getName() + EXTRACTED_NAME_EXT;// 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);List<File> files = new ArrayList<File>();final ZipFile apk = new ZipFile(sourceApk);try {int secondaryNumber = 2;// dexFile = classes2.dexZipEntry dexFile = apk.getEntry(DEX_PREFIX + secondaryNumber + DEX_SUFFIX);while (dexFile != null) {// fileName = com.example.android.animationsdemo-1.apk.classes2.zipString fileName = extractedFilePrefix + secondaryNumber + EXTRACTED_SUFFIX;// extractedFile = /data/data/com.example.android.animationsdemo/code_cache/secondary-dexes/com.example.android.animationsdemo-1.apk.classes2.zipFile extractedFile = new File(dexDir, fileName);files.add(extractedFile);Log.i(TAG, "Extraction is needed for file " + extractedFile);int numAttempts = 0;boolean isExtractionSuccessful = false;while (numAttempts < MAX_EXTRACT_ATTEMPTS && !isExtractionSuccessful) {// 最多嘗試3次來把classesN.dex轉(zhuǎn)成對應(yīng)的zip文件numAttempts++;// Create a zip file (extractedFile) containing only the secondary dex file// (dexFile) from the apk.// 把classesN.dex文件轉(zhuǎn)化為zip文件 extract(apk, dexFile, extractedFile, extractedFilePrefix);// Verify that the extracted file is indeed a zip file.isExtractionSuccessful = verifyZipFile(extractedFile);// Log the sha1 of the extracted zip fileLog.i(TAG, "Extraction " + (isExtractionSuccessful ? "success" : "failed") +" - length " + extractedFile.getAbsolutePath() + ": " +extractedFile.length());if (!isExtractionSuccessful) {// Delete the extracted file extractedFile.delete();if (extractedFile.exists()) {Log.w(TAG, "Failed to delete corrupted secondary dex '" +extractedFile.getPath() + "'");}}}if (!isExtractionSuccessful) {throw new IOException("Could not create zip file " +extractedFile.getAbsolutePath() + " for secondary dex (" +secondaryNumber + ")");}secondaryNumber++;dexFile = apk.getEntry(DEX_PREFIX + secondaryNumber + DEX_SUFFIX);}} finally {try {apk.close();} catch (IOException e) {Log.w(TAG, "Failed to close resource", e);}}return files;}/*** apk = /data/app/com.example.android.animationsdemo-1.apk* dexFile = classesN.dex* extractTo = /data/data/com.example.android.animationsdemo/code_cache/secondary-dexes/com.example.android.animationsdemo-1.apk.classesN.zip* extractedFilePrefix = com.example.android.animationsdemo-1.apk.classes* 把classesN.dex文件轉(zhuǎn)化為zip文件*/private static void extract(ZipFile apk, ZipEntry dexFile, File extractTo,String extractedFilePrefix) throws IOException, FileNotFoundException {InputStream in = apk.getInputStream(dexFile);ZipOutputStream out = null;// tmp = /data/data/com.example.android.animationsdemo/code_cache/secondary-dexes/com.example.android.animationsdemo-1.apk.classes.zipFile tmp = File.createTempFile(extractedFilePrefix, EXTRACTED_SUFFIX,extractTo.getParentFile());Log.i(TAG, "Extracting " + tmp.getPath());try {// 把classesN.dex寫到tmp這個(gè)zip文件中,然后指定這個(gè)zip文件的下一個(gè)ZipEntry為classes.dex// 最后把zip文件重命名為extractToout = 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();}Log.i(TAG, "Renaming to " + extractTo.getPath());if (!tmp.renameTo(extractTo)) {throw new IOException("Failed to rename \"" + tmp.getAbsolutePath() +"\" to \"" + extractTo.getAbsolutePath() + "\"");}} finally {closeQuietly(in);tmp.delete(); // return status ignored }}

加載dex信息到內(nèi)存中

private static void installSecondaryDexes(ClassLoader loader, File dexDir, List<File> files)throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException,InvocationTargetException, NoSuchMethodException, IOException {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);}}} /*** Installer for platform versions 19.*/private static final class V19 {private static void install(ClassLoader loader, List<File> additionalClassPathEntries,File optimizedDirectory)throws IllegalArgumentException, IllegalAccessException,NoSuchFieldException, InvocationTargetException, NoSuchMethodException {/* The patched class loader is expected to be a descendant of* dalvik.system.BaseDexClassLoader. We modify its* dalvik.system.DexPathList pathList field to append additional DEX* file entries.*/// 通過反射的方式獲取PathClassLoader中的pathList屬性Field pathListField = findField(loader, "pathList");Object dexPathList = pathListField.get(loader);// 通過反射的方式重新給DexPathList中的dexElements重新賦值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);}Field suppressedExceptionsField =findField(dexPathList, "dexElementsSuppressedExceptions");IOException[] dexElementsSuppressedExceptions =(IOException[]) suppressedExceptionsField.get(dexPathList);if (dexElementsSuppressedExceptions == null) {dexElementsSuppressedExceptions =suppressedExceptions.toArray(new IOException[suppressedExceptions.size()]);} else {IOException[] combined =new IOException[suppressedExceptions.size() +dexElementsSuppressedExceptions.length];suppressedExceptions.toArray(combined);System.arraycopy(dexElementsSuppressedExceptions, 0, combined,suppressedExceptions.size(), dexElementsSuppressedExceptions.length);dexElementsSuppressedExceptions = combined;}suppressedExceptionsField.set(dexPathList, dexElementsSuppressedExceptions);}}/*** A wrapper around* {@code private static final dalvik.system.DexPathList#makeDexElements}.*/private static Object[] makeDexElements(Object dexPathList, ArrayList<File> files, File optimizedDirectory,ArrayList<IOException> suppressedExceptions)throws IllegalAccessException, InvocationTargetException,NoSuchMethodException {// 通過反射的方式調(diào)用dalvik.system.DexPathList.makeDexElements(),得到classesN.dex對應(yīng)的ElementMethod makeDexElements =findMethod(dexPathList, "makeDexElements", ArrayList.class, File.class,ArrayList.class);return (Object[]) makeDexElements.invoke(dexPathList, files, optimizedDirectory,suppressedExceptions);}} private static void expandFieldArray(Object instance, String fieldName,Object[] extraElements) throws NoSuchFieldException, IllegalArgumentException,IllegalAccessException {// 找到DexPathList類中的dexElements屬性,它是Element[]Field jlrField = findField(instance, fieldName);// 目前內(nèi)存具有的Elements,應(yīng)該只有classes.dex對應(yīng)的ElementObject[] original = (Object[]) jlrField.get(instance);// classesN.dex對應(yīng)的ElementObject[] 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);// 給instance對象中的屬性jlrField重新賦值為combinedjlrField.set(instance, combined);}

?

?

總結(jié)

以上是生活随笔為你收集整理的Multidex实现简要分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。