jbudorick | d79351a | 2015-08-26 06:36:59 +0900 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.base.multidex; |
| 6 | |
| 7 | import android.content.Context; |
| 8 | import android.os.Build; |
| 9 | import android.os.Process; |
| 10 | import android.support.multidex.MultiDex; |
| 11 | |
| 12 | import org.chromium.base.Log; |
| 13 | import org.chromium.base.VisibleForTesting; |
| 14 | |
| 15 | import java.lang.reflect.InvocationTargetException; |
| 16 | |
| 17 | /** |
| 18 | * Performs multidex installation for non-isolated processes. |
| 19 | */ |
| 20 | public class ChromiumMultiDex { |
| 21 | |
dgn | 894055e | 2015-10-14 19:29:49 +0900 | [diff] [blame] | 22 | private static final String TAG = "base_multidex"; |
jbudorick | d79351a | 2015-08-26 06:36:59 +0900 | [diff] [blame] | 23 | |
| 24 | /** |
jbudorick | 42cca5e | 2015-11-21 08:19:23 +0900 | [diff] [blame^] | 25 | * Installs secondary dexes if possible/necessary. |
jbudorick | d79351a | 2015-08-26 06:36:59 +0900 | [diff] [blame] | 26 | * |
| 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 | * |
jbudorick | 42cca5e | 2015-11-21 08:19:23 +0900 | [diff] [blame^] | 30 | * 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 | * |
jbudorick | d79351a | 2015-08-26 06:36:59 +0900 | [diff] [blame] | 36 | * @param context The application context. |
| 37 | */ |
| 38 | @VisibleForTesting |
jbudorick | 42cca5e | 2015-11-21 08:19:23 +0900 | [diff] [blame^] | 39 | #if defined(MULTIDEX_CONFIGURATION_Debug) |
jbudorick | d79351a | 2015-08-26 06:36:59 +0900 | [diff] [blame] | 40 | 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 | } |
jbudorick | 562aab6 | 2015-11-13 09:44:58 +0900 | [diff] [blame] | 64 | #else |
| 65 | public static void install(Context context) { |
| 66 | } |
| 67 | #endif |
jbudorick | d79351a | 2015-08-26 06:36:59 +0900 | [diff] [blame] | 68 | |
| 69 | } |