blob: 5761a454541079045caf4e26fcd5a386e1513e6f [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
7import android.content.Context;
8import android.os.Build;
9import android.os.Process;
10import android.support.multidex.MultiDex;
11
12import org.chromium.base.Log;
13import org.chromium.base.VisibleForTesting;
14
15import java.lang.reflect.InvocationTargetException;
16
17/**
18 * Performs multidex installation for non-isolated processes.
19 */
20public class ChromiumMultiDex {
21
dgn894055e2015-10-14 19:29:49 +090022 private static final String TAG = "base_multidex";
jbudorickd79351a2015-08-26 06:36:59 +090023
24 /**
jbudorick42cca5e2015-11-21 08:19:23 +090025 * Installs secondary dexes if possible/necessary.
jbudorickd79351a2015-08-26 06:36:59 +090026 *
27 * Isolated processes (e.g. renderer processes) can't load secondary dex files on
28 * K and below, so we don't even try in that case.
29 *
jbudorick42cca5e2015-11-21 08:19:23 +090030 * In release builds, this is a no-op because:
31 * - multidex isn't necessary in release builds because we run proguard there and
32 * thus aren't threatening to hit the dex limit; and
33 * - calling MultiDex.install, even in the absence of secondary dexes, causes a
34 * significant regression in start-up time (crbug.com/525695).
35 *
jbudorickd79351a2015-08-26 06:36:59 +090036 * @param context The application context.
37 */
38 @VisibleForTesting
jbudorick42cca5e2015-11-21 08:19:23 +090039#if defined(MULTIDEX_CONFIGURATION_Debug)
jbudorickd79351a2015-08-26 06:36:59 +090040 public static void install(Context context) {
41 try {
42 // TODO(jbudorick): Back out this version check once support for K & below works.
43 // http://crbug.com/512357
44 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && processIsIsolated()) {
45 Log.i(TAG, "Skipping multidex installation: inside isolated process.");
46 } else {
47 MultiDex.install(context);
48 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 }
57 }
58
59 // Calls Process.isIsolated, a private Android API.
60 private static boolean processIsIsolated()
61 throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
62 return (boolean) Process.class.getMethod("isIsolated").invoke(null);
63 }
jbudorick562aab62015-11-13 09:44:58 +090064#else
65 public static void install(Context context) {
66 }
67#endif
jbudorickd79351a2015-08-26 06:36:59 +090068
69}