forked from crosswalk-project/chromium-crosswalk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Android] Rework multidex and enable multidex for unit_tests_apk. (RE…
…LAND 2) this is a reland of https://codereview.chromium.org/1590243003/ BUG=272790 [email protected],[email protected],[email protected] Review URL: https://codereview.chromium.org/1594653002 Cr-Commit-Position: refs/heads/master@{#369906}
- Loading branch information
Showing
17 changed files
with
276 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
base/android/java/src/org/chromium/base/multidex/ChromiumMultiDexInstaller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright 2015 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package org.chromium.base.multidex; | ||
|
||
import android.app.ActivityManager; | ||
import android.app.ActivityManager.RunningAppProcessInfo; | ||
import android.content.Context; | ||
import android.content.pm.ApplicationInfo; | ||
import android.content.pm.PackageManager; | ||
import android.os.Build; | ||
import android.support.multidex.MultiDex; | ||
|
||
import org.chromium.base.Log; | ||
import org.chromium.base.VisibleForTesting; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* Performs multidex installation for non-isolated processes. | ||
*/ | ||
public class ChromiumMultiDexInstaller { | ||
|
||
private static final String TAG = "base_multidex"; | ||
|
||
/** | ||
* Suffix for the meta-data tag in the AndroidManifext.xml that determines whether loading | ||
* secondary dexes should be skipped for a given process name. | ||
*/ | ||
private static final String IGNORE_MULTIDEX_KEY = ".ignore_multidex"; | ||
|
||
/** | ||
* Installs secondary dexes if possible/necessary. | ||
* | ||
* Isolated processes (e.g. renderer processes) can't load secondary dex files on | ||
* K and below, so we don't even try in that case. | ||
* | ||
* In release builds of app apks (as opposed to test apks), this is a no-op because: | ||
* - multidex isn't necessary in release builds because we run proguard there and | ||
* thus aren't threatening to hit the dex limit; and | ||
* - calling MultiDex.install, even in the absence of secondary dexes, causes a | ||
* significant regression in start-up time (crbug.com/525695). | ||
* | ||
* @param context The application context. | ||
*/ | ||
@VisibleForTesting | ||
public static void install(Context context) { | ||
if (!ChromiumMultiDex.isMultidexEnabled()) return; | ||
|
||
// TODO(jbudorick): Back out this version check once support for K & below works. | ||
// http://crbug.com/512357 | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP | ||
&& !shouldInstallMultiDex(context)) { | ||
Log.i(TAG, "Skipping multidex installation: not needed for process."); | ||
} else { | ||
MultiDex.install(context); | ||
Log.i(TAG, "Completed multidex installation."); | ||
} | ||
} | ||
|
||
private static String getProcessName(Context context) { | ||
try { | ||
String currentProcessName = null; | ||
int pid = android.os.Process.myPid(); | ||
|
||
ActivityManager manager = | ||
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); | ||
for (RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) { | ||
if (processInfo.pid == pid) { | ||
currentProcessName = processInfo.processName; | ||
break; | ||
} | ||
} | ||
|
||
return currentProcessName; | ||
} catch (SecurityException ex) { | ||
return null; | ||
} | ||
} | ||
|
||
// Determines whether MultiDex should be installed for the current process. Isolated | ||
// Processes should skip MultiDex as they can not actually access the files on disk. | ||
// Privileged processes need ot have all of their dependencies in the MainDex for | ||
// performance reasons. | ||
private static boolean shouldInstallMultiDex(Context context) { | ||
try { | ||
Method isIsolatedMethod = | ||
android.os.Process.class.getMethod("isIsolated"); | ||
Object retVal = isIsolatedMethod.invoke(null); | ||
if (retVal != null && retVal instanceof Boolean && ((Boolean) retVal)) { | ||
return false; | ||
} | ||
} catch (IllegalAccessException | IllegalArgumentException | ||
| InvocationTargetException | NoSuchMethodException e) { | ||
// Ignore and fall back to checking the app processes. | ||
} | ||
|
||
String currentProcessName = getProcessName(context); | ||
if (currentProcessName == null) return true; | ||
|
||
PackageManager packageManager = context.getPackageManager(); | ||
try { | ||
ApplicationInfo appInfo = packageManager.getApplicationInfo(context.getPackageName(), | ||
PackageManager.GET_META_DATA); | ||
if (appInfo == null || appInfo.metaData == null) return true; | ||
return !appInfo.metaData.getBoolean(currentProcessName + IGNORE_MULTIDEX_KEY, false); | ||
} catch (PackageManager.NameNotFoundException e) { | ||
return true; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.