blob: 441294c1c07d4d899598cf8674ba606aa5c50148 [file] [log] [blame]
jbudorickd79351a2015-08-26 06:36:59 +09001// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.base.multidex;
6
tedchocc1dad412015-11-26 06:16:11 +09007import android.app.ActivityManager;
8import android.app.ActivityManager.RunningAppProcessInfo;
jbudorickd79351a2015-08-26 06:36:59 +09009import android.content.Context;
tedchocc1dad412015-11-26 06:16:11 +090010import android.content.pm.ApplicationInfo;
11import android.content.pm.PackageManager;
jbudorickd79351a2015-08-26 06:36:59 +090012import android.os.Build;
13import android.os.Process;
14import android.support.multidex.MultiDex;
15
16import org.chromium.base.Log;
17import org.chromium.base.VisibleForTesting;
18
19import java.lang.reflect.InvocationTargetException;
tedchocc1dad412015-11-26 06:16:11 +090020import java.lang.reflect.Method;
jbudorickd79351a2015-08-26 06:36:59 +090021
22/**
23 * Performs multidex installation for non-isolated processes.
24 */
25public class ChromiumMultiDex {
26
dgn894055e2015-10-14 19:29:49 +090027 private static final String TAG = "base_multidex";
jbudorickd79351a2015-08-26 06:36:59 +090028
29 /**
tedchocc1dad412015-11-26 06:16:11 +090030 * Suffix for the meta-data tag in the AndroidManifext.xml that determines whether loading
31 * secondary dexes should be skipped for a given process name.
32 */
33 private static final String IGNORE_MULTIDEX_KEY = ".ignore_multidex";
34
35 /**
jbudorick42cca5e2015-11-21 08:19:23 +090036 * Installs secondary dexes if possible/necessary.
jbudorickd79351a2015-08-26 06:36:59 +090037 *
38 * Isolated processes (e.g. renderer processes) can't load secondary dex files on
39 * K and below, so we don't even try in that case.
40 *
jbudorick42cca5e2015-11-21 08:19:23 +090041 * In release builds, this is a no-op because:
42 * - multidex isn't necessary in release builds because we run proguard there and
43 * thus aren't threatening to hit the dex limit; and
44 * - calling MultiDex.install, even in the absence of secondary dexes, causes a
45 * significant regression in start-up time (crbug.com/525695).
46 *
jbudorickd79351a2015-08-26 06:36:59 +090047 * @param context The application context.
48 */
49 @VisibleForTesting
jbudorick42cca5e2015-11-21 08:19:23 +090050#if defined(MULTIDEX_CONFIGURATION_Debug)
jbudorickd79351a2015-08-26 06:36:59 +090051 public static void install(Context context) {
tedchocc1dad412015-11-26 06:16:11 +090052 // TODO(jbudorick): Back out this version check once support for K & below works.
53 // http://crbug.com/512357
54 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP
55 && !shouldInstallMultiDex(context)) {
56 Log.i(TAG, "Skipping multidex installation: not needed for process.");
57 } else {
58 MultiDex.install(context);
59 Log.i(TAG, "Completed multidex installation.");
jbudorickd79351a2015-08-26 06:36:59 +090060 }
61 }
62
tedchocc1dad412015-11-26 06:16:11 +090063 private static String getProcessName(Context context) {
64 try {
65 String currentProcessName = null;
66 int pid = android.os.Process.myPid();
67
68 ActivityManager manager =
69 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
70 for (RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
71 if (processInfo.pid == pid) {
72 currentProcessName = processInfo.processName;
73 break;
74 }
75 }
76
77 return currentProcessName;
78 } catch (SecurityException ex) {
79 return null;
80 }
81 }
82
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.
85 // Privileged processes need ot have all of their dependencies in the MainDex for
86 // performance reasons.
87 private static boolean shouldInstallMultiDex(Context context) {
88 try {
89 Method isIsolatedMethod =
90 android.os.Process.class.getMethod("isIsolated");
91 Object retVal = isIsolatedMethod.invoke(null);
92 if (retVal != null && retVal instanceof Boolean && ((Boolean) retVal)) {
93 return false;
94 }
95 } catch (IllegalAccessException | IllegalArgumentException
96 | InvocationTargetException | NoSuchMethodException e) {
97 // Ignore and fall back to checking the app processes.
98 }
99
100 String currentProcessName = getProcessName(context);
101 if (currentProcessName == null) return true;
102
103 PackageManager packageManager = context.getPackageManager();
104 try {
105 ApplicationInfo appInfo = packageManager.getApplicationInfo(context.getPackageName(),
106 PackageManager.GET_META_DATA);
107 return !appInfo.metaData.getBoolean(currentProcessName + IGNORE_MULTIDEX_KEY, false);
108 } catch (PackageManager.NameNotFoundException e) {
109 return true;
110 }
jbudorickd79351a2015-08-26 06:36:59 +0900111 }
jbudorick562aab62015-11-13 09:44:58 +0900112#else
113 public static void install(Context context) {
114 }
115#endif
jbudorickd79351a2015-08-26 06:36:59 +0900116
117}