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; | |
8 import android.app.ActivityManager.RunningAppProcessInfo; | |
7 import android.content.Context; | 9 import android.content.Context; |
10 import android.content.pm.ApplicationInfo; | |
11 import android.content.pm.PackageManager; | |
8 import android.os.Build; | 12 import android.os.Build; |
9 import android.os.Process; | 13 import android.os.Process; |
10 import android.support.multidex.MultiDex; | 14 import android.support.multidex.MultiDex; |
11 | 15 |
12 import org.chromium.base.Log; | 16 import org.chromium.base.Log; |
13 import org.chromium.base.VisibleForTesting; | 17 import org.chromium.base.VisibleForTesting; |
14 | 18 |
15 import java.lang.reflect.InvocationTargetException; | 19 import java.lang.reflect.InvocationTargetException; |
20 import java.lang.reflect.Method; | |
16 | 21 |
17 /** | 22 /** |
18 * Performs multidex installation for non-isolated processes. | 23 * Performs multidex installation for non-isolated processes. |
19 */ | 24 */ |
20 public class ChromiumMultiDex { | 25 public class ChromiumMultiDex { |
21 | 26 |
22 private static final String TAG = "base_multidex"; | 27 private static final String TAG = "base_multidex"; |
23 | 28 |
29 private static final String IGNORE_MULTIDEX_KEY = ".ignore_multidex"; | |
nyquist
2015/11/25 18:57:20
Could you add a comment here referring to the valu
Ted C
2015/11/25 19:14:11
Done.
| |
30 | |
24 /** | 31 /** |
25 * Installs secondary dexes if possible/necessary. | 32 * Installs secondary dexes if possible/necessary. |
26 * | 33 * |
27 * Isolated processes (e.g. renderer processes) can't load secondary dex fi les on | 34 * Isolated processes (e.g. renderer processes) can't load secondary dex fi les on |
28 * K and below, so we don't even try in that case. | 35 * K and below, so we don't even try in that case. |
29 * | 36 * |
30 * In release builds, this is a no-op because: | 37 * In release builds, this is a no-op because: |
31 * - multidex isn't necessary in release builds because we run proguard t here and | 38 * - multidex isn't necessary in release builds because we run proguard t here and |
32 * thus aren't threatening to hit the dex limit; and | 39 * thus aren't threatening to hit the dex limit; and |
33 * - calling MultiDex.install, even in the absence of secondary dexes, ca uses a | 40 * - calling MultiDex.install, even in the absence of secondary dexes, ca uses a |
34 * significant regression in start-up time (crbug.com/525695). | 41 * significant regression in start-up time (crbug.com/525695). |
35 * | 42 * |
36 * @param context The application context. | 43 * @param context The application context. |
37 */ | 44 */ |
38 @VisibleForTesting | 45 @VisibleForTesting |
39 #if defined(MULTIDEX_CONFIGURATION_Debug) | 46 #if defined(MULTIDEX_CONFIGURATION_Debug) |
40 public static void install(Context context) { | 47 public static void install(Context context) { |
41 try { | 48 // TODO(jbudorick): Back out this version check once support for K & bel ow works. |
42 // TODO(jbudorick): Back out this version check once support for K & below works. | 49 // http://crbug.com/512357 |
43 // http://crbug.com/512357 | 50 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP |
44 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && processI sIsolated()) { | 51 && !shouldInstallMultiDex(context)) { |
jbudorick
2015/11/25 18:13:35
One other thing to consider: we could potentially
Ted C
2015/11/25 18:17:15
Maybe, but right now it doesn't seem we need anyth
| |
45 Log.i(TAG, "Skipping multidex installation: inside isolated proc ess."); | 52 Log.i(TAG, "Skipping multidex installation: not needed for process." ); |
46 } else { | 53 } else { |
47 MultiDex.install(context); | 54 MultiDex.install(context); |
48 Log.i(TAG, "Completed multidex installation."); | 55 Log.i(TAG, "Completed multidex installation."); |
49 } | |
50 } catch (NoSuchMethodException e) { | |
51 Log.wtf(TAG, "Failed multidex installation", e); | |
52 } catch (IllegalAccessException e) { | |
53 Log.wtf(TAG, "Failed multidex installation", e); | |
54 } catch (InvocationTargetException e) { | |
55 Log.wtf(TAG, "Failed multidex installation", e); | |
56 } | 56 } |
57 } | 57 } |
58 | 58 |
59 // Calls Process.isIsolated, a private Android API. | 59 private static String getProcessName(Context context) { |
60 private static boolean processIsIsolated() | 60 try { |
61 throws NoSuchMethodException, IllegalAccessException, InvocationTarg etException { | 61 String currentProcessName = null; |
62 return (boolean) Process.class.getMethod("isIsolated").invoke(null); | 62 int pid = android.os.Process.myPid(); |
63 | |
64 ActivityManager manager = | |
65 (ActivityManager) context.getSystemService(Context.ACTIVITY_ SERVICE); | |
66 for (RunningAppProcessInfo processInfo : manager.getRunningAppProces ses()) { | |
67 if (processInfo.pid == pid) { | |
68 currentProcessName = processInfo.processName; | |
69 break; | |
70 } | |
71 } | |
72 | |
73 return currentProcessName; | |
74 } catch (SecurityException ex) { | |
75 return null; | |
76 } | |
77 } | |
78 | |
79 // Determines whether MultiDex should be installed for the current process. Isolated | |
80 // Processes should skip MultiDex as they can not actually access the files on disk. | |
81 // Privileged processes need ot have all of their dependencies in the MainDe x for | |
82 // performance reasons. | |
83 private static boolean shouldInstallMultiDex(Context context) { | |
84 try { | |
85 Method isIsolatedMethod = | |
86 android.os.Process.class.getMethod("isIsolated"); | |
87 Object retVal = isIsolatedMethod.invoke(null); | |
88 if (retVal != null && retVal instanceof Boolean && ((Boolean) retVal )) { | |
89 return false; | |
90 } | |
91 } catch (IllegalAccessException | IllegalArgumentException | |
92 | InvocationTargetException | NoSuchMethodException e) { | |
93 // Ignore and fall back to checking the app processes. | |
94 } | |
95 | |
96 String currentProcessName = getProcessName(context); | |
97 if (currentProcessName == null) return true; | |
98 | |
99 PackageManager packageManager = context.getPackageManager(); | |
100 try { | |
101 ApplicationInfo appInfo = packageManager.getApplicationInfo(context. getPackageName(), | |
102 PackageManager.GET_META_DATA); | |
103 return !appInfo.metaData.getBoolean(currentProcessName + IGNORE_MULT IDEX_KEY, false); | |
104 } catch (PackageManager.NameNotFoundException e) { | |
105 return true; | |
106 } | |
63 } | 107 } |
64 #else | 108 #else |
65 public static void install(Context context) { | 109 public static void install(Context context) { |
66 } | 110 } |
67 #endif | 111 #endif |
68 | 112 |
69 } | 113 } |
OLD | NEW |