1667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu/*
2667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * Copyright (C) 2013 The Android Open Source Project
3667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu *
4667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * Licensed under the Apache License, Version 2.0 (the "License");
5667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * you may not use this file except in compliance with the License.
6667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * You may obtain a copy of the License at
7667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu *
8667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu *      http://www.apache.org/licenses/LICENSE-2.0
9667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu *
10667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * Unless required by applicable law or agreed to in writing, software
11667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * distributed under the License is distributed on an "AS IS" BASIS,
12667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * See the License for the specific language governing permissions and
14667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * limitations under the License.
15667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */
16667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
17667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chupackage android.support.multidex;
18667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
19dd3cc22f2fbc8ea4dd5fa88978dc8d1ae5034bd9Yohann Rousselimport android.app.Application;
20667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport android.content.Context;
21667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport android.content.pm.ApplicationInfo;
22667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport android.os.Build;
23667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport android.util.Log;
24667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
25602c6ca8cae4718ba8ff9f65e53305d002479359Yohann Rousselimport dalvik.system.DexFile;
26602c6ca8cae4718ba8ff9f65e53305d002479359Yohann Roussel
27667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.io.File;
28667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.io.IOException;
29667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.lang.reflect.Array;
30667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.lang.reflect.Field;
31667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.lang.reflect.InvocationTargetException;
32667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.lang.reflect.Method;
33667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.util.ArrayList;
34667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.util.Arrays;
35667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.util.HashSet;
36667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.util.List;
37667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.util.ListIterator;
38667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.util.Set;
397b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Rousselimport java.util.regex.Matcher;
407b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Rousselimport java.util.regex.Pattern;
4166f379f774e06e650375750d18b1695ddb853371Maurice Chuimport java.util.zip.ZipFile;
42667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
43667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu/**
448be7c7a5a6377745054dcd3f1176a7f4cb96b153Sebastien Hertz * MultiDex patches {@link Context#getClassLoader() the application context class
45667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * loader} in order to load classes from more than one dex file. The primary
46dd3cc22f2fbc8ea4dd5fa88978dc8d1ae5034bd9Yohann Roussel * {@code classes.dex} must contain the classes necessary for calling this
47dd3cc22f2fbc8ea4dd5fa88978dc8d1ae5034bd9Yohann Roussel * class methods. Secondary dex files named classes2.dex, classes3.dex... found
48dd3cc22f2fbc8ea4dd5fa88978dc8d1ae5034bd9Yohann Roussel * in the application apk will be added to the classloader after first call to
49667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * {@link #install(Context)}.
50667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu *
51667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * <p/>
527b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel * This library provides compatibility for platforms with API level 4 through 20. This library does
537b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel * nothing on newer versions of the platform which provide built-in support for secondary dex files.
54667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */
55667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chupublic final class MultiDex {
56667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
57667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    static final String TAG = "MultiDex";
58667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
59590a07e63868f0a1da311ff22b4a9f35eb48a865Yohann Roussel    private static final String OLD_SECONDARY_FOLDER_NAME = "secondary-dexes";
60590a07e63868f0a1da311ff22b4a9f35eb48a865Yohann Roussel
61606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel    private static final String CODE_CACHE_NAME = "code_cache";
62606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel
63606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel    private static final String CODE_CACHE_SECONDARY_FOLDER_NAME = "secondary-dexes";
64667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
657b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel    private static final int MAX_SUPPORTED_SDK_VERSION = 20;
66667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
67667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    private static final int MIN_SDK_VERSION = 4;
68667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
697b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel    private static final int VM_WITH_MULTIDEX_VERSION_MAJOR = 2;
707b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel
717b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel    private static final int VM_WITH_MULTIDEX_VERSION_MINOR = 1;
727b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel
73667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    private static final Set<String> installedApk = new HashSet<String>();
74667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
757b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel    private static final boolean IS_VM_MULTIDEX_CAPABLE =
767b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel            isVMMultidexCapable(System.getProperty("java.vm.version"));
777b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel
78667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    private MultiDex() {}
79667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
80667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    /**
81667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * Patches the application context class loader by appending extra dex files
82dd3cc22f2fbc8ea4dd5fa88978dc8d1ae5034bd9Yohann Roussel     * loaded from the application apk. This method should be called in the
83dd3cc22f2fbc8ea4dd5fa88978dc8d1ae5034bd9Yohann Roussel     * attachBaseContext of your {@link Application}, see
84dd3cc22f2fbc8ea4dd5fa88978dc8d1ae5034bd9Yohann Roussel     * {@link MultiDexApplication} for more explanation and an example.
85667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     *
86667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * @param context application context.
87667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * @throws RuntimeException if an error occurred preventing the classloader
88667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     *         extension.
89667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     */
90667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    public static void install(Context context) {
91602c6ca8cae4718ba8ff9f65e53305d002479359Yohann Roussel        Log.i(TAG, "install");
927b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel        if (IS_VM_MULTIDEX_CAPABLE) {
937b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel            Log.i(TAG, "VM has multidex support, MultiDex support library is disabled.");
947b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel            return;
957b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel        }
96667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
97667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        if (Build.VERSION.SDK_INT < MIN_SDK_VERSION) {
98667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            throw new RuntimeException("Multi dex installation failed. SDK " + Build.VERSION.SDK_INT
99667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    + " is unsupported. Min SDK version is " + MIN_SDK_VERSION + ".");
100667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        }
101667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
102667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        try {
103d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel            ApplicationInfo applicationInfo = getApplicationInfo(context);
104667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            if (applicationInfo == null) {
105d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel                // Looks like running on a test Context, so just return without patching.
106667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                return;
107667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            }
108667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
109667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            synchronized (installedApk) {
110667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                String apkPath = applicationInfo.sourceDir;
111667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                if (installedApk.contains(apkPath)) {
112667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    return;
113667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                }
114667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                installedApk.add(apkPath);
115667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
1167b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                if (Build.VERSION.SDK_INT > MAX_SUPPORTED_SDK_VERSION) {
1177b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                    Log.w(TAG, "MultiDex is not guaranteed to work in SDK version "
1187b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                            + Build.VERSION.SDK_INT + ": SDK version higher than "
1197b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                            + MAX_SUPPORTED_SDK_VERSION + " should be backed by "
1207b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                            + "runtime with built-in multidex capabilty but it's not the "
1217b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                            + "case here: java.vm.version=\""
1227b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                            + System.getProperty("java.vm.version") + "\"");
123667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                }
124667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
125667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                /* The patched class loader is expected to be a descendant of
126667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                 * dalvik.system.BaseDexClassLoader. We modify its
127667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                 * dalvik.system.DexPathList pathList field to append additional DEX
128667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                 * file entries.
129667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                 */
130667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                ClassLoader loader;
131667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                try {
132667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    loader = context.getClassLoader();
133667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                } catch (RuntimeException e) {
134667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    /* Ignore those exceptions so that we don't break tests relying on Context like
135667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                     * a android.test.mock.MockContext or a android.content.ContextWrapper with a
136667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                     * null base Context.
137667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                     */
138667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    Log.w(TAG, "Failure while trying to obtain Context class loader. " +
139667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                            "Must be running in test mode. Skip patching.", e);
140667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    return;
141667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                }
142667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                if (loader == null) {
143667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    // Note, the context class loader is null when running Robolectric tests.
144667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    Log.e(TAG,
145667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                            "Context class loader is null. Must be running in test mode. "
146667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                            + "Skip patching.");
147667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    return;
148667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                }
149667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
150590a07e63868f0a1da311ff22b4a9f35eb48a865Yohann Roussel                try {
151590a07e63868f0a1da311ff22b4a9f35eb48a865Yohann Roussel                  clearOldDexDir(context);
152590a07e63868f0a1da311ff22b4a9f35eb48a865Yohann Roussel                } catch (Throwable t) {
153590a07e63868f0a1da311ff22b4a9f35eb48a865Yohann Roussel                  Log.w(TAG, "Something went wrong when trying to clear old MultiDex extraction, "
154590a07e63868f0a1da311ff22b4a9f35eb48a865Yohann Roussel                      + "continuing without cleaning.", t);
155590a07e63868f0a1da311ff22b4a9f35eb48a865Yohann Roussel                }
156590a07e63868f0a1da311ff22b4a9f35eb48a865Yohann Roussel
157606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel                File dexDir = getDexDir(context, applicationInfo);
1589958145a97586375966bc133c900ead0e1550f2aYohann Roussel                List<? extends File> files =
1599958145a97586375966bc133c900ead0e1550f2aYohann Roussel                    MultiDexExtractor.load(context, applicationInfo, dexDir, false);
160fe10f9fd25a4d7bb287d36b3730aa0d605115053Yohann Roussel                installSecondaryDexes(loader, dexDir, files);
161667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            }
162667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
163667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        } catch (Exception e) {
164667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            Log.e(TAG, "Multidex installation failure", e);
165667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            throw new RuntimeException("Multi dex installation failed (" + e.getMessage() + ").");
166667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        }
167602c6ca8cae4718ba8ff9f65e53305d002479359Yohann Roussel        Log.i(TAG, "install done");
168667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    }
169667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
17087738871b50552a98a083f8e840089e8889b3e2aJon Noack    private static ApplicationInfo getApplicationInfo(Context context) {
171d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel        try {
17287738871b50552a98a083f8e840089e8889b3e2aJon Noack            /* Due to package install races it is possible for a process to be started from an old
17387738871b50552a98a083f8e840089e8889b3e2aJon Noack             * apk even though that apk has been replaced. Querying for ApplicationInfo by package
17487738871b50552a98a083f8e840089e8889b3e2aJon Noack             * name may return information for the new apk, leading to a runtime with the old main
17587738871b50552a98a083f8e840089e8889b3e2aJon Noack             * dex file and new secondary dex files. This leads to various problems like
17687738871b50552a98a083f8e840089e8889b3e2aJon Noack             * ClassNotFoundExceptions. Using context.getApplicationInfo() should result in the
17787738871b50552a98a083f8e840089e8889b3e2aJon Noack             * process having a consistent view of the world (even if it is of the old world). The
17887738871b50552a98a083f8e840089e8889b3e2aJon Noack             * package install races are eventually resolved and old processes are killed.
17987738871b50552a98a083f8e840089e8889b3e2aJon Noack             */
18087738871b50552a98a083f8e840089e8889b3e2aJon Noack            return context.getApplicationInfo();
181d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel        } catch (RuntimeException e) {
182d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel            /* Ignore those exceptions so that we don't break tests relying on Context like
183d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel             * a android.test.mock.MockContext or a android.content.ContextWrapper with a null
184d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel             * base Context.
185d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel             */
186d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel            Log.w(TAG, "Failure while trying to obtain ApplicationInfo from Context. " +
187d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel                    "Must be running in test mode. Skip patching.", e);
188d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel            return null;
189d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel        }
190d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel    }
191d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel
1927b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel    /**
1937b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel     * Identifies if the current VM has a native support for multidex, meaning there is no need for
1947b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel     * additional installation by this library.
1957b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel     * @return true if the VM handles multidex
1967b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel     */
1977b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel    /* package visible for test */
1987b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel    static boolean isVMMultidexCapable(String versionString) {
1997b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel        boolean isMultidexCapable = false;
2007b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel        if (versionString != null) {
2017b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel            Matcher matcher = Pattern.compile("(\\d+)\\.(\\d+)(\\.\\d+)?").matcher(versionString);
2027b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel            if (matcher.matches()) {
2037b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                try {
2047b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                    int major = Integer.parseInt(matcher.group(1));
2057b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                    int minor = Integer.parseInt(matcher.group(2));
2067b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                    isMultidexCapable = (major > VM_WITH_MULTIDEX_VERSION_MAJOR)
2077b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                            || ((major == VM_WITH_MULTIDEX_VERSION_MAJOR)
2087b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                                    && (minor >= VM_WITH_MULTIDEX_VERSION_MINOR));
2097b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                } catch (NumberFormatException e) {
2107b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                    // let isMultidexCapable be false
2117b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                }
2127b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel            }
2137b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel        }
2147b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel        Log.i(TAG, "VM with version " + versionString +
2157b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                (isMultidexCapable ?
2167b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                        " has multidex support" :
2177b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel                        " does not have multidex support"));
2187b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel        return isMultidexCapable;
2197b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel    }
2207b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel
2219958145a97586375966bc133c900ead0e1550f2aYohann Roussel    private static void installSecondaryDexes(ClassLoader loader, File dexDir,
2229958145a97586375966bc133c900ead0e1550f2aYohann Roussel        List<? extends File> files)
223cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu            throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException,
224cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu            InvocationTargetException, NoSuchMethodException, IOException {
225cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu        if (!files.isEmpty()) {
226cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu            if (Build.VERSION.SDK_INT >= 19) {
227cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu                V19.install(loader, files, dexDir);
228cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu            } else if (Build.VERSION.SDK_INT >= 14) {
229cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu                V14.install(loader, files, dexDir);
230cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu            } else {
231cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu                V4.install(loader, files);
232cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu            }
233cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu        }
234cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu    }
235cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu
236cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu    /**
237667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * Locates a given field anywhere in the class inheritance hierarchy.
238667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     *
239667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * @param instance an object to search the field into.
240667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * @param name field name
241667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * @return a field object
242667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * @throws NoSuchFieldException if the field cannot be located
243667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     */
244667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    private static Field findField(Object instance, String name) throws NoSuchFieldException {
245667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
246667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            try {
247667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                Field field = clazz.getDeclaredField(name);
248667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
249667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
250667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                if (!field.isAccessible()) {
251667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    field.setAccessible(true);
252667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                }
253667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
254667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                return field;
255667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            } catch (NoSuchFieldException e) {
256667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                // ignore and search next
257667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            }
258667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        }
259667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
260667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass());
261667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    }
262667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
263667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    /**
264667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * Locates a given method anywhere in the class inheritance hierarchy.
265667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     *
266667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * @param instance an object to search the method into.
267667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * @param name method name
268667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * @param parameterTypes method parameter types
269667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * @return a method object
270667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * @throws NoSuchMethodException if the method cannot be located
271667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     */
272667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    private static Method findMethod(Object instance, String name, Class<?>... parameterTypes)
273667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            throws NoSuchMethodException {
274667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
275667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            try {
276667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                Method method = clazz.getDeclaredMethod(name, parameterTypes);
277667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
278667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
279667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                if (!method.isAccessible()) {
280667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    method.setAccessible(true);
281667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                }
282667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
283667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                return method;
284667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            } catch (NoSuchMethodException e) {
285667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                // ignore and search next
286667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            }
287667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        }
288667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
289667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        throw new NoSuchMethodException("Method " + name + " with parameters " +
290667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                Arrays.asList(parameterTypes) + " not found in " + instance.getClass());
291667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    }
292667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
293667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    /**
294667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * Replace the value of a field containing a non null array, by a new array containing the
295667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * elements of the original array plus the elements of extraElements.
296667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * @param instance the instance whose field is to be modified.
297667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * @param fieldName the field to modify.
298667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * @param extraElements elements to append at the end of the array.
299667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     */
300667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    private static void expandFieldArray(Object instance, String fieldName,
301667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            Object[] extraElements) throws NoSuchFieldException, IllegalArgumentException,
302667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            IllegalAccessException {
303667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        Field jlrField = findField(instance, fieldName);
304667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        Object[] original = (Object[]) jlrField.get(instance);
305667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        Object[] combined = (Object[]) Array.newInstance(
306667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                original.getClass().getComponentType(), original.length + extraElements.length);
307667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        System.arraycopy(original, 0, combined, 0, original.length);
308667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        System.arraycopy(extraElements, 0, combined, original.length, extraElements.length);
309667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        jlrField.set(instance, combined);
310667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    }
311667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
312d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel    private static void clearOldDexDir(Context context) throws Exception {
313590a07e63868f0a1da311ff22b4a9f35eb48a865Yohann Roussel        File dexDir = new File(context.getFilesDir(), OLD_SECONDARY_FOLDER_NAME);
314d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel        if (dexDir.isDirectory()) {
315d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel            Log.i(TAG, "Clearing old secondary dex dir (" + dexDir.getPath() + ").");
316d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel            File[] files = dexDir.listFiles();
317d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel            if (files == null) {
318d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel                Log.w(TAG, "Failed to list secondary dex dir content (" + dexDir.getPath() + ").");
319d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel                return;
320d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel            }
321d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel            for (File oldFile : files) {
322d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel                Log.i(TAG, "Trying to delete old file " + oldFile.getPath() + " of size "
323d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel                        + oldFile.length());
324d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel                if (!oldFile.delete()) {
325d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel                    Log.w(TAG, "Failed to delete old file " + oldFile.getPath());
326d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel                } else {
327d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel                    Log.i(TAG, "Deleted old file " + oldFile.getPath());
328d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel                }
329d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel            }
330d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel            if (!dexDir.delete()) {
331d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel                Log.w(TAG, "Failed to delete secondary dex dir " + dexDir.getPath());
332d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel            } else {
333d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel                Log.i(TAG, "Deleted old secondary dex dir " + dexDir.getPath());
334d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel            }
335d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel        }
336d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel    }
337d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel
338606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel    private static File getDexDir(Context context, ApplicationInfo applicationInfo)
339606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel            throws IOException {
340606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel        File cache = new File(applicationInfo.dataDir, CODE_CACHE_NAME);
341606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel        try {
342606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel            mkdirChecked(cache);
343606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel        } catch (IOException e) {
344606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel            /* If we can't emulate code_cache, then store to filesDir. This means abandoning useless
345606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel             * files on disk if the device ever updates to android 5+. But since this seems to
346606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel             * happen only on some devices running android 2, this should cause no pollution.
347606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel             */
348606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel            cache = new File(context.getFilesDir(), CODE_CACHE_NAME);
349606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel            mkdirChecked(cache);
350606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel        }
351606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel        File dexDir = new File(cache, CODE_CACHE_SECONDARY_FOLDER_NAME);
352606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel        mkdirChecked(dexDir);
353606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel        return dexDir;
354606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel    }
355606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel
356606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel    private static void mkdirChecked(File dir) throws IOException {
357606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel        dir.mkdir();
358606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel        if (!dir.isDirectory()) {
359606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel            File parent = dir.getParentFile();
360606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel            if (parent == null) {
361606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel                Log.e(TAG, "Failed to create dir " + dir.getPath() + ". Parent file is null.");
362606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel            } else {
363606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel                Log.e(TAG, "Failed to create dir " + dir.getPath() +
364606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel                        ". parent file is a dir " + parent.isDirectory() +
365606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel                        ", a file " + parent.isFile() +
366606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel                        ", exists " + parent.exists() +
367606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel                        ", readable " + parent.canRead() +
368606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel                        ", writable " + parent.canWrite());
369606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel            }
370606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel            throw new IOException("Failed to create directory " + dir.getPath());
371606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel        }
372606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel    }
373606af94785cb96d418d87fe5a90bb2e09ccfa97fYohann Roussel
374667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    /**
375667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * Installer for platform versions 19.
376667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     */
377667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    private static final class V19 {
378667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
3799958145a97586375966bc133c900ead0e1550f2aYohann Roussel        private static void install(ClassLoader loader,
3809958145a97586375966bc133c900ead0e1550f2aYohann Roussel                List<? extends File> additionalClassPathEntries,
381667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                File optimizedDirectory)
382667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                        throws IllegalArgumentException, IllegalAccessException,
383667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                        NoSuchFieldException, InvocationTargetException, NoSuchMethodException {
384667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            /* The patched class loader is expected to be a descendant of
385667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu             * dalvik.system.BaseDexClassLoader. We modify its
386667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu             * dalvik.system.DexPathList pathList field to append additional DEX
387667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu             * file entries.
388667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu             */
389667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            Field pathListField = findField(loader, "pathList");
390667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            Object dexPathList = pathListField.get(loader);
391667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>();
392667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            expandFieldArray(dexPathList, "dexElements", makeDexElements(dexPathList,
393667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    new ArrayList<File>(additionalClassPathEntries), optimizedDirectory,
394667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    suppressedExceptions));
395667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            if (suppressedExceptions.size() > 0) {
39688117c37e2c3f8601f295b74a3e804877afb78eeYohann Roussel                for (IOException e : suppressedExceptions) {
39788117c37e2c3f8601f295b74a3e804877afb78eeYohann Roussel                    Log.w(TAG, "Exception in makeDexElement", e);
39888117c37e2c3f8601f295b74a3e804877afb78eeYohann Roussel                }
399667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                Field suppressedExceptionsField =
40074e66b8013b5b9002f67e53825c189a18597b1e8Yohann Roussel                        findField(dexPathList, "dexElementsSuppressedExceptions");
401667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                IOException[] dexElementsSuppressedExceptions =
40274e66b8013b5b9002f67e53825c189a18597b1e8Yohann Roussel                        (IOException[]) suppressedExceptionsField.get(dexPathList);
403667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
404667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                if (dexElementsSuppressedExceptions == null) {
405667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    dexElementsSuppressedExceptions =
406667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                            suppressedExceptions.toArray(
407667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                                    new IOException[suppressedExceptions.size()]);
408667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                } else {
409667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    IOException[] combined =
410667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                            new IOException[suppressedExceptions.size() +
411667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                                            dexElementsSuppressedExceptions.length];
412667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    suppressedExceptions.toArray(combined);
413667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    System.arraycopy(dexElementsSuppressedExceptions, 0, combined,
414667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                            suppressedExceptions.size(), dexElementsSuppressedExceptions.length);
415667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    dexElementsSuppressedExceptions = combined;
416667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                }
417667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
41874e66b8013b5b9002f67e53825c189a18597b1e8Yohann Roussel                suppressedExceptionsField.set(dexPathList, dexElementsSuppressedExceptions);
419667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            }
420667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        }
421667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
422667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        /**
423667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu         * A wrapper around
424667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu         * {@code private static final dalvik.system.DexPathList#makeDexElements}.
425667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu         */
426667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        private static Object[] makeDexElements(
427667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                Object dexPathList, ArrayList<File> files, File optimizedDirectory,
428667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                ArrayList<IOException> suppressedExceptions)
429667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                        throws IllegalAccessException, InvocationTargetException,
430667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                        NoSuchMethodException {
431667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            Method makeDexElements =
432667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    findMethod(dexPathList, "makeDexElements", ArrayList.class, File.class,
433667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                            ArrayList.class);
434667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
435667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            return (Object[]) makeDexElements.invoke(dexPathList, files, optimizedDirectory,
436667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    suppressedExceptions);
437667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        }
438667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    }
439667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
440667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    /**
441667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * Installer for platform versions 14, 15, 16, 17 and 18.
442667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     */
443667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    private static final class V14 {
444667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
4459958145a97586375966bc133c900ead0e1550f2aYohann Roussel        private static void install(ClassLoader loader,
4469958145a97586375966bc133c900ead0e1550f2aYohann Roussel                List<? extends File> additionalClassPathEntries,
447667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                File optimizedDirectory)
448667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                        throws IllegalArgumentException, IllegalAccessException,
449667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                        NoSuchFieldException, InvocationTargetException, NoSuchMethodException {
450667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            /* The patched class loader is expected to be a descendant of
451667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu             * dalvik.system.BaseDexClassLoader. We modify its
452667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu             * dalvik.system.DexPathList pathList field to append additional DEX
453667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu             * file entries.
454667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu             */
455667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            Field pathListField = findField(loader, "pathList");
456667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            Object dexPathList = pathListField.get(loader);
457667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            expandFieldArray(dexPathList, "dexElements", makeDexElements(dexPathList,
458667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    new ArrayList<File>(additionalClassPathEntries), optimizedDirectory));
459667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        }
460667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
461667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        /**
462667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu         * A wrapper around
463667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu         * {@code private static final dalvik.system.DexPathList#makeDexElements}.
464667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu         */
465667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        private static Object[] makeDexElements(
466667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                Object dexPathList, ArrayList<File> files, File optimizedDirectory)
467667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                        throws IllegalAccessException, InvocationTargetException,
468667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                        NoSuchMethodException {
469667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            Method makeDexElements =
470667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    findMethod(dexPathList, "makeDexElements", ArrayList.class, File.class);
471667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
472667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            return (Object[]) makeDexElements.invoke(dexPathList, files, optimizedDirectory);
473667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        }
474667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    }
475667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
476667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    /**
477667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     * Installer for platform versions 4 to 13.
478667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu     */
479667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    private static final class V4 {
4809958145a97586375966bc133c900ead0e1550f2aYohann Roussel        private static void install(ClassLoader loader,
4819958145a97586375966bc133c900ead0e1550f2aYohann Roussel                List<? extends File> additionalClassPathEntries)
482667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                        throws IllegalArgumentException, IllegalAccessException,
483667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                        NoSuchFieldException, IOException {
484667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            /* The patched class loader is expected to be a descendant of
485667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu             * dalvik.system.DexClassLoader. We modify its
48652eafa01b1e61e410cb4c5609eacee93c2a3e853Yohann Roussel             * fields mPaths, mFiles, mZips and mDexs to append additional DEX
487667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu             * file entries.
488667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu             */
489667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            int extraSize = additionalClassPathEntries.size();
490667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
491667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            Field pathField = findField(loader, "path");
492667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
493667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            StringBuilder path = new StringBuilder((String) pathField.get(loader));
494667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            String[] extraPaths = new String[extraSize];
495667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            File[] extraFiles = new File[extraSize];
49666f379f774e06e650375750d18b1695ddb853371Maurice Chu            ZipFile[] extraZips = new ZipFile[extraSize];
497667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            DexFile[] extraDexs = new DexFile[extraSize];
4989958145a97586375966bc133c900ead0e1550f2aYohann Roussel            for (ListIterator<? extends File> iterator = additionalClassPathEntries.listIterator();
499667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                    iterator.hasNext();) {
500667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                File additionalEntry = iterator.next();
501667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                String entryPath = additionalEntry.getAbsolutePath();
502667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                path.append(':').append(entryPath);
503667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                int index = iterator.previousIndex();
504667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                extraPaths[index] = entryPath;
505667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                extraFiles[index] = additionalEntry;
50666f379f774e06e650375750d18b1695ddb853371Maurice Chu                extraZips[index] = new ZipFile(additionalEntry);
507667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu                extraDexs[index] = DexFile.loadDex(entryPath, entryPath + ".dex", 0);
508667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            }
509667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
510667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            pathField.set(loader, path.toString());
511667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            expandFieldArray(loader, "mPaths", extraPaths);
512667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            expandFieldArray(loader, "mFiles", extraFiles);
51366f379f774e06e650375750d18b1695ddb853371Maurice Chu            expandFieldArray(loader, "mZips", extraZips);
514667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu            expandFieldArray(loader, "mDexs", extraDexs);
515667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu        }
516667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu    }
517667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu
518667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu}
519