1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.multidex;
18
19import android.app.Application;
20import android.content.Context;
21import android.content.pm.ApplicationInfo;
22import android.os.Build;
23import android.util.Log;
24
25import dalvik.system.DexFile;
26
27import java.io.File;
28import java.io.IOException;
29import java.lang.reflect.Array;
30import java.lang.reflect.Field;
31import java.lang.reflect.InvocationTargetException;
32import java.lang.reflect.Method;
33import java.util.ArrayList;
34import java.util.Arrays;
35import java.util.HashSet;
36import java.util.List;
37import java.util.ListIterator;
38import java.util.Set;
39import java.util.regex.Matcher;
40import java.util.regex.Pattern;
41import java.util.zip.ZipFile;
42
43/**
44 * MultiDex patches {@link Context#getClassLoader() the application context class
45 * loader} in order to load classes from more than one dex file. The primary
46 * {@code classes.dex} must contain the classes necessary for calling this
47 * class methods. Secondary dex files named classes2.dex, classes3.dex... found
48 * in the application apk will be added to the classloader after first call to
49 * {@link #install(Context)}.
50 *
51 * <p/>
52 * This library provides compatibility for platforms with API level 4 through 20. This library does
53 * nothing on newer versions of the platform which provide built-in support for secondary dex files.
54 */
55public final class MultiDex {
56
57    static final String TAG = "MultiDex";
58
59    private static final String OLD_SECONDARY_FOLDER_NAME = "secondary-dexes";
60
61    private static final String CODE_CACHE_NAME = "code_cache";
62
63    private static final String CODE_CACHE_SECONDARY_FOLDER_NAME = "secondary-dexes";
64
65    private static final int MAX_SUPPORTED_SDK_VERSION = 20;
66
67    private static final int MIN_SDK_VERSION = 4;
68
69    private static final int VM_WITH_MULTIDEX_VERSION_MAJOR = 2;
70
71    private static final int VM_WITH_MULTIDEX_VERSION_MINOR = 1;
72
73    private static final Set<String> installedApk = new HashSet<String>();
74
75    private static final boolean IS_VM_MULTIDEX_CAPABLE =
76            isVMMultidexCapable(System.getProperty("java.vm.version"));
77
78    private MultiDex() {}
79
80    /**
81     * Patches the application context class loader by appending extra dex files
82     * loaded from the application apk. This method should be called in the
83     * attachBaseContext of your {@link Application}, see
84     * {@link MultiDexApplication} for more explanation and an example.
85     *
86     * @param context application context.
87     * @throws RuntimeException if an error occurred preventing the classloader
88     *         extension.
89     */
90    public static void install(Context context) {
91        Log.i(TAG, "install");
92        if (IS_VM_MULTIDEX_CAPABLE) {
93            Log.i(TAG, "VM has multidex support, MultiDex support library is disabled.");
94            return;
95        }
96
97        if (Build.VERSION.SDK_INT < MIN_SDK_VERSION) {
98            throw new RuntimeException("Multi dex installation failed. SDK " + Build.VERSION.SDK_INT
99                    + " is unsupported. Min SDK version is " + MIN_SDK_VERSION + ".");
100        }
101
102        try {
103            ApplicationInfo applicationInfo = getApplicationInfo(context);
104            if (applicationInfo == null) {
105                // Looks like running on a test Context, so just return without patching.
106                return;
107            }
108
109            synchronized (installedApk) {
110                String apkPath = applicationInfo.sourceDir;
111                if (installedApk.contains(apkPath)) {
112                    return;
113                }
114                installedApk.add(apkPath);
115
116                if (Build.VERSION.SDK_INT > MAX_SUPPORTED_SDK_VERSION) {
117                    Log.w(TAG, "MultiDex is not guaranteed to work in SDK version "
118                            + Build.VERSION.SDK_INT + ": SDK version higher than "
119                            + MAX_SUPPORTED_SDK_VERSION + " should be backed by "
120                            + "runtime with built-in multidex capabilty but it's not the "
121                            + "case here: java.vm.version=\""
122                            + System.getProperty("java.vm.version") + "\"");
123                }
124
125                /* The patched class loader is expected to be a descendant of
126                 * dalvik.system.BaseDexClassLoader. We modify its
127                 * dalvik.system.DexPathList pathList field to append additional DEX
128                 * file entries.
129                 */
130                ClassLoader loader;
131                try {
132                    loader = context.getClassLoader();
133                } catch (RuntimeException e) {
134                    /* Ignore those exceptions so that we don't break tests relying on Context like
135                     * a android.test.mock.MockContext or a android.content.ContextWrapper with a
136                     * null base Context.
137                     */
138                    Log.w(TAG, "Failure while trying to obtain Context class loader. " +
139                            "Must be running in test mode. Skip patching.", e);
140                    return;
141                }
142                if (loader == null) {
143                    // Note, the context class loader is null when running Robolectric tests.
144                    Log.e(TAG,
145                            "Context class loader is null. Must be running in test mode. "
146                            + "Skip patching.");
147                    return;
148                }
149
150                try {
151                  clearOldDexDir(context);
152                } catch (Throwable t) {
153                  Log.w(TAG, "Something went wrong when trying to clear old MultiDex extraction, "
154                      + "continuing without cleaning.", t);
155                }
156
157                File dexDir = getDexDir(context, applicationInfo);
158                List<? extends File> files =
159                    MultiDexExtractor.load(context, applicationInfo, dexDir, false);
160                installSecondaryDexes(loader, dexDir, files);
161            }
162
163        } catch (Exception e) {
164            Log.e(TAG, "Multidex installation failure", e);
165            throw new RuntimeException("Multi dex installation failed (" + e.getMessage() + ").");
166        }
167        Log.i(TAG, "install done");
168    }
169
170    private static ApplicationInfo getApplicationInfo(Context context) {
171        try {
172            /* Due to package install races it is possible for a process to be started from an old
173             * apk even though that apk has been replaced. Querying for ApplicationInfo by package
174             * name may return information for the new apk, leading to a runtime with the old main
175             * dex file and new secondary dex files. This leads to various problems like
176             * ClassNotFoundExceptions. Using context.getApplicationInfo() should result in the
177             * process having a consistent view of the world (even if it is of the old world). The
178             * package install races are eventually resolved and old processes are killed.
179             */
180            return context.getApplicationInfo();
181        } catch (RuntimeException e) {
182            /* Ignore those exceptions so that we don't break tests relying on Context like
183             * a android.test.mock.MockContext or a android.content.ContextWrapper with a null
184             * base Context.
185             */
186            Log.w(TAG, "Failure while trying to obtain ApplicationInfo from Context. " +
187                    "Must be running in test mode. Skip patching.", e);
188            return null;
189        }
190    }
191
192    /**
193     * Identifies if the current VM has a native support for multidex, meaning there is no need for
194     * additional installation by this library.
195     * @return true if the VM handles multidex
196     */
197    /* package visible for test */
198    static boolean isVMMultidexCapable(String versionString) {
199        boolean isMultidexCapable = false;
200        if (versionString != null) {
201            Matcher matcher = Pattern.compile("(\\d+)\\.(\\d+)(\\.\\d+)?").matcher(versionString);
202            if (matcher.matches()) {
203                try {
204                    int major = Integer.parseInt(matcher.group(1));
205                    int minor = Integer.parseInt(matcher.group(2));
206                    isMultidexCapable = (major > VM_WITH_MULTIDEX_VERSION_MAJOR)
207                            || ((major == VM_WITH_MULTIDEX_VERSION_MAJOR)
208                                    && (minor >= VM_WITH_MULTIDEX_VERSION_MINOR));
209                } catch (NumberFormatException e) {
210                    // let isMultidexCapable be false
211                }
212            }
213        }
214        Log.i(TAG, "VM with version " + versionString +
215                (isMultidexCapable ?
216                        " has multidex support" :
217                        " does not have multidex support"));
218        return isMultidexCapable;
219    }
220
221    private static void installSecondaryDexes(ClassLoader loader, File dexDir,
222        List<? extends File> files)
223            throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException,
224            InvocationTargetException, NoSuchMethodException, IOException {
225        if (!files.isEmpty()) {
226            if (Build.VERSION.SDK_INT >= 19) {
227                V19.install(loader, files, dexDir);
228            } else if (Build.VERSION.SDK_INT >= 14) {
229                V14.install(loader, files, dexDir);
230            } else {
231                V4.install(loader, files);
232            }
233        }
234    }
235
236    /**
237     * Locates a given field anywhere in the class inheritance hierarchy.
238     *
239     * @param instance an object to search the field into.
240     * @param name field name
241     * @return a field object
242     * @throws NoSuchFieldException if the field cannot be located
243     */
244    private static Field findField(Object instance, String name) throws NoSuchFieldException {
245        for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
246            try {
247                Field field = clazz.getDeclaredField(name);
248
249
250                if (!field.isAccessible()) {
251                    field.setAccessible(true);
252                }
253
254                return field;
255            } catch (NoSuchFieldException e) {
256                // ignore and search next
257            }
258        }
259
260        throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass());
261    }
262
263    /**
264     * Locates a given method anywhere in the class inheritance hierarchy.
265     *
266     * @param instance an object to search the method into.
267     * @param name method name
268     * @param parameterTypes method parameter types
269     * @return a method object
270     * @throws NoSuchMethodException if the method cannot be located
271     */
272    private static Method findMethod(Object instance, String name, Class<?>... parameterTypes)
273            throws NoSuchMethodException {
274        for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
275            try {
276                Method method = clazz.getDeclaredMethod(name, parameterTypes);
277
278
279                if (!method.isAccessible()) {
280                    method.setAccessible(true);
281                }
282
283                return method;
284            } catch (NoSuchMethodException e) {
285                // ignore and search next
286            }
287        }
288
289        throw new NoSuchMethodException("Method " + name + " with parameters " +
290                Arrays.asList(parameterTypes) + " not found in " + instance.getClass());
291    }
292
293    /**
294     * Replace the value of a field containing a non null array, by a new array containing the
295     * elements of the original array plus the elements of extraElements.
296     * @param instance the instance whose field is to be modified.
297     * @param fieldName the field to modify.
298     * @param extraElements elements to append at the end of the array.
299     */
300    private static void expandFieldArray(Object instance, String fieldName,
301            Object[] extraElements) throws NoSuchFieldException, IllegalArgumentException,
302            IllegalAccessException {
303        Field jlrField = findField(instance, fieldName);
304        Object[] original = (Object[]) jlrField.get(instance);
305        Object[] combined = (Object[]) Array.newInstance(
306                original.getClass().getComponentType(), original.length + extraElements.length);
307        System.arraycopy(original, 0, combined, 0, original.length);
308        System.arraycopy(extraElements, 0, combined, original.length, extraElements.length);
309        jlrField.set(instance, combined);
310    }
311
312    private static void clearOldDexDir(Context context) throws Exception {
313        File dexDir = new File(context.getFilesDir(), OLD_SECONDARY_FOLDER_NAME);
314        if (dexDir.isDirectory()) {
315            Log.i(TAG, "Clearing old secondary dex dir (" + dexDir.getPath() + ").");
316            File[] files = dexDir.listFiles();
317            if (files == null) {
318                Log.w(TAG, "Failed to list secondary dex dir content (" + dexDir.getPath() + ").");
319                return;
320            }
321            for (File oldFile : files) {
322                Log.i(TAG, "Trying to delete old file " + oldFile.getPath() + " of size "
323                        + oldFile.length());
324                if (!oldFile.delete()) {
325                    Log.w(TAG, "Failed to delete old file " + oldFile.getPath());
326                } else {
327                    Log.i(TAG, "Deleted old file " + oldFile.getPath());
328                }
329            }
330            if (!dexDir.delete()) {
331                Log.w(TAG, "Failed to delete secondary dex dir " + dexDir.getPath());
332            } else {
333                Log.i(TAG, "Deleted old secondary dex dir " + dexDir.getPath());
334            }
335        }
336    }
337
338    private static File getDexDir(Context context, ApplicationInfo applicationInfo)
339            throws IOException {
340        File cache = new File(applicationInfo.dataDir, CODE_CACHE_NAME);
341        try {
342            mkdirChecked(cache);
343        } catch (IOException e) {
344            /* If we can't emulate code_cache, then store to filesDir. This means abandoning useless
345             * files on disk if the device ever updates to android 5+. But since this seems to
346             * happen only on some devices running android 2, this should cause no pollution.
347             */
348            cache = new File(context.getFilesDir(), CODE_CACHE_NAME);
349            mkdirChecked(cache);
350        }
351        File dexDir = new File(cache, CODE_CACHE_SECONDARY_FOLDER_NAME);
352        mkdirChecked(dexDir);
353        return dexDir;
354    }
355
356    private static void mkdirChecked(File dir) throws IOException {
357        dir.mkdir();
358        if (!dir.isDirectory()) {
359            File parent = dir.getParentFile();
360            if (parent == null) {
361                Log.e(TAG, "Failed to create dir " + dir.getPath() + ". Parent file is null.");
362            } else {
363                Log.e(TAG, "Failed to create dir " + dir.getPath() +
364                        ". parent file is a dir " + parent.isDirectory() +
365                        ", a file " + parent.isFile() +
366                        ", exists " + parent.exists() +
367                        ", readable " + parent.canRead() +
368                        ", writable " + parent.canWrite());
369            }
370            throw new IOException("Failed to create directory " + dir.getPath());
371        }
372    }
373
374    /**
375     * Installer for platform versions 19.
376     */
377    private static final class V19 {
378
379        private static void install(ClassLoader loader,
380                List<? extends File> additionalClassPathEntries,
381                File optimizedDirectory)
382                        throws IllegalArgumentException, IllegalAccessException,
383                        NoSuchFieldException, InvocationTargetException, NoSuchMethodException {
384            /* The patched class loader is expected to be a descendant of
385             * dalvik.system.BaseDexClassLoader. We modify its
386             * dalvik.system.DexPathList pathList field to append additional DEX
387             * file entries.
388             */
389            Field pathListField = findField(loader, "pathList");
390            Object dexPathList = pathListField.get(loader);
391            ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>();
392            expandFieldArray(dexPathList, "dexElements", makeDexElements(dexPathList,
393                    new ArrayList<File>(additionalClassPathEntries), optimizedDirectory,
394                    suppressedExceptions));
395            if (suppressedExceptions.size() > 0) {
396                for (IOException e : suppressedExceptions) {
397                    Log.w(TAG, "Exception in makeDexElement", e);
398                }
399                Field suppressedExceptionsField =
400                        findField(dexPathList, "dexElementsSuppressedExceptions");
401                IOException[] dexElementsSuppressedExceptions =
402                        (IOException[]) suppressedExceptionsField.get(dexPathList);
403
404                if (dexElementsSuppressedExceptions == null) {
405                    dexElementsSuppressedExceptions =
406                            suppressedExceptions.toArray(
407                                    new IOException[suppressedExceptions.size()]);
408                } else {
409                    IOException[] combined =
410                            new IOException[suppressedExceptions.size() +
411                                            dexElementsSuppressedExceptions.length];
412                    suppressedExceptions.toArray(combined);
413                    System.arraycopy(dexElementsSuppressedExceptions, 0, combined,
414                            suppressedExceptions.size(), dexElementsSuppressedExceptions.length);
415                    dexElementsSuppressedExceptions = combined;
416                }
417
418                suppressedExceptionsField.set(dexPathList, dexElementsSuppressedExceptions);
419            }
420        }
421
422        /**
423         * A wrapper around
424         * {@code private static final dalvik.system.DexPathList#makeDexElements}.
425         */
426        private static Object[] makeDexElements(
427                Object dexPathList, ArrayList<File> files, File optimizedDirectory,
428                ArrayList<IOException> suppressedExceptions)
429                        throws IllegalAccessException, InvocationTargetException,
430                        NoSuchMethodException {
431            Method makeDexElements =
432                    findMethod(dexPathList, "makeDexElements", ArrayList.class, File.class,
433                            ArrayList.class);
434
435            return (Object[]) makeDexElements.invoke(dexPathList, files, optimizedDirectory,
436                    suppressedExceptions);
437        }
438    }
439
440    /**
441     * Installer for platform versions 14, 15, 16, 17 and 18.
442     */
443    private static final class V14 {
444
445        private static void install(ClassLoader loader,
446                List<? extends File> additionalClassPathEntries,
447                File optimizedDirectory)
448                        throws IllegalArgumentException, IllegalAccessException,
449                        NoSuchFieldException, InvocationTargetException, NoSuchMethodException {
450            /* The patched class loader is expected to be a descendant of
451             * dalvik.system.BaseDexClassLoader. We modify its
452             * dalvik.system.DexPathList pathList field to append additional DEX
453             * file entries.
454             */
455            Field pathListField = findField(loader, "pathList");
456            Object dexPathList = pathListField.get(loader);
457            expandFieldArray(dexPathList, "dexElements", makeDexElements(dexPathList,
458                    new ArrayList<File>(additionalClassPathEntries), optimizedDirectory));
459        }
460
461        /**
462         * A wrapper around
463         * {@code private static final dalvik.system.DexPathList#makeDexElements}.
464         */
465        private static Object[] makeDexElements(
466                Object dexPathList, ArrayList<File> files, File optimizedDirectory)
467                        throws IllegalAccessException, InvocationTargetException,
468                        NoSuchMethodException {
469            Method makeDexElements =
470                    findMethod(dexPathList, "makeDexElements", ArrayList.class, File.class);
471
472            return (Object[]) makeDexElements.invoke(dexPathList, files, optimizedDirectory);
473        }
474    }
475
476    /**
477     * Installer for platform versions 4 to 13.
478     */
479    private static final class V4 {
480        private static void install(ClassLoader loader,
481                List<? extends File> additionalClassPathEntries)
482                        throws IllegalArgumentException, IllegalAccessException,
483                        NoSuchFieldException, IOException {
484            /* The patched class loader is expected to be a descendant of
485             * dalvik.system.DexClassLoader. We modify its
486             * fields mPaths, mFiles, mZips and mDexs to append additional DEX
487             * file entries.
488             */
489            int extraSize = additionalClassPathEntries.size();
490
491            Field pathField = findField(loader, "path");
492
493            StringBuilder path = new StringBuilder((String) pathField.get(loader));
494            String[] extraPaths = new String[extraSize];
495            File[] extraFiles = new File[extraSize];
496            ZipFile[] extraZips = new ZipFile[extraSize];
497            DexFile[] extraDexs = new DexFile[extraSize];
498            for (ListIterator<? extends File> iterator = additionalClassPathEntries.listIterator();
499                    iterator.hasNext();) {
500                File additionalEntry = iterator.next();
501                String entryPath = additionalEntry.getAbsolutePath();
502                path.append(':').append(entryPath);
503                int index = iterator.previousIndex();
504                extraPaths[index] = entryPath;
505                extraFiles[index] = additionalEntry;
506                extraZips[index] = new ZipFile(additionalEntry);
507                extraDexs[index] = DexFile.loadDex(entryPath, entryPath + ".dex", 0);
508            }
509
510            pathField.set(loader, path.toString());
511            expandFieldArray(loader, "mPaths", extraPaths);
512            expandFieldArray(loader, "mFiles", extraFiles);
513            expandFieldArray(loader, "mZips", extraZips);
514            expandFieldArray(loader, "mDexs", extraDexs);
515        }
516    }
517
518}
519