| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.base.multidex; | 5 package org.chromium.base.multidex; |
| 6 | 6 |
| 7 import android.app.ActivityManager; | 7 import android.app.ActivityManager; |
| 8 import android.app.ActivityManager.RunningAppProcessInfo; | 8 import android.app.ActivityManager.RunningAppProcessInfo; |
| 9 import android.content.Context; | 9 import android.content.Context; |
| 10 import android.content.pm.ApplicationInfo; | 10 import android.content.pm.ApplicationInfo; |
| 11 import android.content.pm.PackageManager; | 11 import android.content.pm.PackageManager; |
| 12 import android.os.Build; | 12 import android.os.Build; |
| 13 import android.os.Process; | |
| 14 import android.support.multidex.MultiDex; | 13 import android.support.multidex.MultiDex; |
| 15 | 14 |
| 16 import org.chromium.base.Log; | 15 import org.chromium.base.Log; |
| 17 import org.chromium.base.VisibleForTesting; | 16 import org.chromium.base.VisibleForTesting; |
| 18 | 17 |
| 19 import java.lang.reflect.InvocationTargetException; | 18 import java.lang.reflect.InvocationTargetException; |
| 20 import java.lang.reflect.Method; | 19 import java.lang.reflect.Method; |
| 21 | 20 |
| 22 /** | 21 /** |
| 23 * Performs multidex installation for non-isolated processes. | 22 * Performs multidex installation for non-isolated processes. |
| 24 */ | 23 */ |
| 25 public class ChromiumMultiDex { | 24 public class ChromiumMultiDexInstaller { |
| 26 | 25 |
| 27 private static final String TAG = "base_multidex"; | 26 private static final String TAG = "base_multidex"; |
| 28 | 27 |
| 29 /** | 28 /** |
| 30 * Suffix for the meta-data tag in the AndroidManifext.xml that determines w
hether loading | 29 * Suffix for the meta-data tag in the AndroidManifext.xml that determines w
hether loading |
| 31 * secondary dexes should be skipped for a given process name. | 30 * secondary dexes should be skipped for a given process name. |
| 32 */ | 31 */ |
| 33 private static final String IGNORE_MULTIDEX_KEY = ".ignore_multidex"; | 32 private static final String IGNORE_MULTIDEX_KEY = ".ignore_multidex"; |
| 34 | 33 |
| 35 /** | 34 /** |
| 36 * Installs secondary dexes if possible/necessary. | 35 * Installs secondary dexes if possible/necessary. |
| 37 * | 36 * |
| 38 * Isolated processes (e.g. renderer processes) can't load secondary dex fi
les on | 37 * Isolated processes (e.g. renderer processes) can't load secondary dex fi
les on |
| 39 * K and below, so we don't even try in that case. | 38 * K and below, so we don't even try in that case. |
| 40 * | 39 * |
| 41 * In release builds, this is a no-op because: | 40 * In release builds of app apks (as opposed to test apks), this is a no-op
because: |
| 42 * - multidex isn't necessary in release builds because we run proguard t
here and | 41 * - multidex isn't necessary in release builds because we run proguard t
here and |
| 43 * thus aren't threatening to hit the dex limit; and | 42 * thus aren't threatening to hit the dex limit; and |
| 44 * - calling MultiDex.install, even in the absence of secondary dexes, ca
uses a | 43 * - calling MultiDex.install, even in the absence of secondary dexes, ca
uses a |
| 45 * significant regression in start-up time (crbug.com/525695). | 44 * significant regression in start-up time (crbug.com/525695). |
| 46 * | 45 * |
| 47 * @param context The application context. | 46 * @param context The application context. |
| 48 */ | 47 */ |
| 49 @VisibleForTesting | 48 @VisibleForTesting |
| 50 #if defined(MULTIDEX_CONFIGURATION_Debug) | |
| 51 public static void install(Context context) { | 49 public static void install(Context context) { |
| 50 if (!ChromiumMultiDex.isMultidexEnabled()) return; |
| 51 |
| 52 // TODO(jbudorick): Back out this version check once support for K & bel
ow works. | 52 // TODO(jbudorick): Back out this version check once support for K & bel
ow works. |
| 53 // http://crbug.com/512357 | 53 // http://crbug.com/512357 |
| 54 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP | 54 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP |
| 55 && !shouldInstallMultiDex(context)) { | 55 && !shouldInstallMultiDex(context)) { |
| 56 Log.i(TAG, "Skipping multidex installation: not needed for process."
); | 56 Log.i(TAG, "Skipping multidex installation: not needed for process."
); |
| 57 } else { | 57 } else { |
| 58 MultiDex.install(context); | 58 MultiDex.install(context); |
| 59 Log.i(TAG, "Completed multidex installation."); | 59 Log.i(TAG, "Completed multidex installation."); |
| 60 } | 60 } |
| 61 } | 61 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 76 | 76 |
| 77 return currentProcessName; | 77 return currentProcessName; |
| 78 } catch (SecurityException ex) { | 78 } catch (SecurityException ex) { |
| 79 return null; | 79 return null; |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 | 82 |
| 83 // Determines whether MultiDex should be installed for the current process.
Isolated | 83 // Determines whether MultiDex should be installed for the current process.
Isolated |
| 84 // Processes should skip MultiDex as they can not actually access the files
on disk. | 84 // Processes should skip MultiDex as they can not actually access the files
on disk. |
| 85 // Privileged processes need ot have all of their dependencies in the MainDe
x for | 85 // Privileged processes need ot have all of their dependencies in the MainDe
x for |
| 86 // performance reasons. | 86 // performance reasons. |
| 87 private static boolean shouldInstallMultiDex(Context context) { | 87 private static boolean shouldInstallMultiDex(Context context) { |
| 88 try { | 88 try { |
| 89 Method isIsolatedMethod = | 89 Method isIsolatedMethod = |
| 90 android.os.Process.class.getMethod("isIsolated"); | 90 android.os.Process.class.getMethod("isIsolated"); |
| 91 Object retVal = isIsolatedMethod.invoke(null); | 91 Object retVal = isIsolatedMethod.invoke(null); |
| 92 if (retVal != null && retVal instanceof Boolean && ((Boolean) retVal
)) { | 92 if (retVal != null && retVal instanceof Boolean && ((Boolean) retVal
)) { |
| 93 return false; | 93 return false; |
| 94 } | 94 } |
| 95 } catch (IllegalAccessException | IllegalArgumentException | 95 } catch (IllegalAccessException | IllegalArgumentException |
| 96 | InvocationTargetException | NoSuchMethodException e) { | 96 | InvocationTargetException | NoSuchMethodException e) { |
| 97 // Ignore and fall back to checking the app processes. | 97 // Ignore and fall back to checking the app processes. |
| 98 } | 98 } |
| 99 | 99 |
| 100 String currentProcessName = getProcessName(context); | 100 String currentProcessName = getProcessName(context); |
| 101 if (currentProcessName == null) return true; | 101 if (currentProcessName == null) return true; |
| 102 | 102 |
| 103 PackageManager packageManager = context.getPackageManager(); | 103 PackageManager packageManager = context.getPackageManager(); |
| 104 try { | 104 try { |
| 105 ApplicationInfo appInfo = packageManager.getApplicationInfo(context.
getPackageName(), | 105 ApplicationInfo appInfo = packageManager.getApplicationInfo(context.
getPackageName(), |
| 106 PackageManager.GET_META_DATA); | 106 PackageManager.GET_META_DATA); |
| 107 if (appInfo == null || appInfo.metaData == null) return true; | 107 if (appInfo == null || appInfo.metaData == null) return true; |
| 108 return !appInfo.metaData.getBoolean(currentProcessName + IGNORE_MULT
IDEX_KEY, false); | 108 return !appInfo.metaData.getBoolean(currentProcessName + IGNORE_MULT
IDEX_KEY, false); |
| 109 } catch (PackageManager.NameNotFoundException e) { | 109 } catch (PackageManager.NameNotFoundException e) { |
| 110 return true; | 110 return true; |
| 111 } | 111 } |
| 112 } | 112 } |
| 113 #else | |
| 114 public static void install(Context context) { | |
| 115 } | |
| 116 #endif | |
| 117 | 113 |
| 118 } | 114 } |
| OLD | NEW |