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; |
8 import android.os.Build; | 10 import android.os.Build; |
9 import android.os.Process; | 11 import android.os.Process; |
10 import android.support.multidex.MultiDex; | 12 import android.support.multidex.MultiDex; |
11 | 13 |
12 import org.chromium.base.Log; | 14 import org.chromium.base.Log; |
13 import org.chromium.base.VisibleForTesting; | 15 import org.chromium.base.VisibleForTesting; |
14 | 16 |
15 import java.lang.reflect.InvocationTargetException; | 17 import java.lang.reflect.InvocationTargetException; |
18 import java.lang.reflect.Method; | |
16 | 19 |
17 /** | 20 /** |
18 * Performs multidex installation for non-isolated processes. | 21 * Performs multidex installation for non-isolated processes. |
19 */ | 22 */ |
20 public class ChromiumMultiDex { | 23 public class ChromiumMultiDex { |
21 | 24 |
22 private static final String TAG = "base_multidex"; | 25 private static final String TAG = "base_multidex"; |
23 | 26 |
24 /** | 27 /** |
25 * Installs secondary dexes if possible/necessary. | 28 * Installs secondary dexes if possible/necessary. |
26 * | 29 * |
27 * Isolated processes (e.g. renderer processes) can't load secondary dex fi les on | 30 * 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. | 31 * K and below, so we don't even try in that case. |
29 * | 32 * |
30 * In release builds, this is a no-op because: | 33 * In release builds, this is a no-op because: |
31 * - multidex isn't necessary in release builds because we run proguard t here and | 34 * - 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 | 35 * thus aren't threatening to hit the dex limit; and |
33 * - calling MultiDex.install, even in the absence of secondary dexes, ca uses a | 36 * - calling MultiDex.install, even in the absence of secondary dexes, ca uses a |
34 * significant regression in start-up time (crbug.com/525695). | 37 * significant regression in start-up time (crbug.com/525695). |
35 * | 38 * |
36 * @param context The application context. | 39 * @param context The application context. |
37 */ | 40 */ |
38 @VisibleForTesting | 41 @VisibleForTesting |
39 #if defined(MULTIDEX_CONFIGURATION_Debug) | 42 #if defined(MULTIDEX_CONFIGURATION_Debug) |
40 public static void install(Context context) { | 43 public static void install(Context context) { |
41 try { | 44 // 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. | 45 // http://crbug.com/512357 |
43 // http://crbug.com/512357 | 46 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP |
44 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && processI sIsolated()) { | 47 && !isBrowserApplicationProcess(context)) { |
45 Log.i(TAG, "Skipping multidex installation: inside isolated proc ess."); | 48 Log.i(TAG, "Skipping multidex installation: inside non browser proce ss."); |
46 } else { | 49 } else { |
47 MultiDex.install(context); | 50 MultiDex.install(context); |
48 Log.i(TAG, "Completed multidex installation."); | 51 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 } | 52 } |
57 } | 53 } |
58 | 54 |
59 // Calls Process.isIsolated, a private Android API. | 55 // Determines whether the current context is for the main browser applicatio n. |
60 private static boolean processIsIsolated() | 56 private static boolean isBrowserApplicationProcess(Context context) { |
61 throws NoSuchMethodException, IllegalAccessException, InvocationTarg etException { | 57 try { |
62 return (boolean) Process.class.getMethod("isIsolated").invoke(null); | 58 Method isIsolatedMethod = |
59 android.os.Process.class.getMethod("isIsolated"); | |
60 Object retVal = isIsolatedMethod.invoke(null); | |
61 if (retVal != null && retVal instanceof Boolean && ((Boolean) retVal )) { | |
62 return false; | |
63 } | |
64 } catch (IllegalAccessException | IllegalArgumentException | |
65 | InvocationTargetException | NoSuchMethodException e) { | |
66 // Ignore and fall back to checking the app processes. | |
67 } | |
68 | |
69 try { | |
nyquist
2015/11/25 00:12:52
Nit: Could you extract most of this block out to a
| |
70 String currentProcessName = null; | |
71 int pid = android.os.Process.myPid(); | |
72 | |
73 ActivityManager manager = | |
74 (ActivityManager) context.getSystemService(Context.ACTIVITY_ SERVICE); | |
75 for (RunningAppProcessInfo processInfo : manager.getRunningAppProces ses()) { | |
76 if (processInfo.pid == pid) { | |
77 currentProcessName = processInfo.processName; | |
78 break; | |
79 } | |
80 } | |
81 | |
82 return currentProcessName != null && !currentProcessName.contains(": "); | |
nyquist
2015/11/25 00:12:52
Could you add a comment explaining the significanc
| |
83 } catch (SecurityException ex) { | |
84 return false; | |
85 } | |
63 } | 86 } |
64 #else | 87 #else |
65 public static void install(Context context) { | 88 public static void install(Context context) { |
66 } | 89 } |
67 #endif | 90 #endif |
68 | 91 |
69 } | 92 } |
OLD | NEW |